Woocommerce Products Widget for a specific category

送分小仙女□ 提交于 2019-12-12 04:43:59

问题


How can I make the Woocommerce Products Widget display Products of a specific Category only? Current options are on-sale, featured, and all.


回答1:


This can be done with a custom function hooked in woocommerce_products_widget_query_args filter hook. You will have to set inside it, in the array, your product category (or your product categories if more than one).

Here is the code:

add_filter( 'woocommerce_products_widget_query_args', function( $query_args ){
    // Set HERE your product category slugs 
    $categories = array( 'music', 'posters' );

    $query_args['tax_query'] = array( array(
        'taxonomy' => 'product_cat',
        'field'    => 'slug',
        'terms'    => $categories,
    ));

    return $query_args;
}, 10, 1 );

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested in WooCommerce 3+ and works



来源:https://stackoverflow.com/questions/46184153/woocommerce-products-widget-for-a-specific-category

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!