简体   繁体   中英

Get an archive of all downloadable products in Woocommerce

I have a Woocommerce store with both physical and downloadable products, all created as simple products.

I can't seem to find a way to display an archive that displays all my downloadable products on one page. Does anyone know how to do this?

Thanks in advance, Johannes

Updated - There are many ways.

1) Woocommerce Shortcode:

It is the best and easiest way and you can use it in any page of your choice. The following code will allow you to filter downloadable products in Woocommerce shortcodes:

add_filter( 'woocommerce_shortcode_products_query', 'custom_shortcode_products_query', 10, 3 );
function custom_shortcode_products_query( $query_args, $atts, $loop_name ) {
    if( ! is_admin() && 'downloadable' === $atts['class'] )
        $query_args['meta_query'][] = array(
            'key'     => '_downloadable',
            'value'   => 'yes',
            'compare' => '=',
        );

    return $query_args;
}

Code goes in function.php file of your active child theme (or theme). Tested and works.

USAGE:

[products class='downloadable']

Or in php code

echo do_shortcode("[products class='downloadable']");

Reference: Woocommerce Shortcodes documentation


2) The query string alternative:

The following code will allow you to display in shop page the downloadable products for a specific query string like: www.yoursite.com/shop/?downloadable=1

The code:

add_filter( 'woocommerce_product_query_meta_query', 'downloadable_products', 30, 2 );
function downloadable_products( $meta_query, $query ) {
    // Only on archive pages
    if( is_admin() || ! is_shop() )
        return $meta_query; // exit

    if( isset($_GET['downloadable']) || $_GET['downloadable'] == '1' ){
        $meta_query[] = array(
            'key'     => '_downloadable',
            'value'   => 'yes',
            'compare' => '=',
        );
    }
    return $meta_query;
}

Code goes in function.php file of your active child theme (or theme). Tested and works.

Reference: Exclude downloadable products from shop pages in Woocommerce

如果您不想动手使用代码,请尝试在产品中添加“可下载”类别,然后在页面上使用短代码[products category="downloadable"]

You didn't look very far...

Above is a very simple way to display products which are downloadable.

Edit: Downvoted because I didn't spoon feed?

$products = wc_get_products( array(
    'downloadable' => true

) );

OR

$products = new WC_Product_Query( array(
    'downloadable' => false
) );

$products = $query->get_products();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM