How can I make the Woocommerce Products Widget display Products of a specific Category only? Current options are on-sale, featured, and all.
0
votes
Do you know where are those values coming from? Code or Database? I would bet that it is DB and you will need to make an addition into the DB. Which can probably be done through the admin site.
– Tyler Christian
@TylerChristian - They are in the code. OP will have to create your own widget, modify code, or pay for one of the existing "products by category" widgets that are out there.
– random_user_name
This is a site designed for developers, asking developer-related questions. If you want help developing / coding this, you'll need to show what you tried, and what didn't work about it. Please read the "How to Ask" portion of the help.
– random_user_name
1 Answers
2
votes
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