Exclude a category from the woocommerce product shortcode.

woocommerce
woocommerce

Woocommerce is no doubt the best plugin for any ecommerce site in WordPress. The best thing about it is the level of customizations it allows to let the user meets its need. Woocommerce is a powerful plugin which comes with loads of actions and hooks to customize its default behavior.Woocommerce lives up to its baseline which reads “WooCommerce is a powerful, extendable eCommerce plugin that helps you sell anything. Beautifully.”

In this blog we will discuss about the filter using which we can customize the products shown in the page using products shortcode.

Woocommerce allows the user to use shortcodes, one of it is products. Using this products shortcode the user can add the list of products in a page other than the default shop page. The shortcode [products ] has three default parameters, the values of which can be modified basing on the use.

This shows all the available products from all the categories with the number of columns chosen which defaults to 4, orderby ‘title’ in ascending order by default.

But if one wants to not exclude one or more than one categories of products from the product shortcode page, then we can do it by using the “woocommerce_shortcode_products_query” filter which is not available in the older versions of woocommerce  (another reason to keep updating your plugins, themes or WordPress)

Like always I will advise to make the below codes to the functions.php file of your child theme to keep the changes preserved.

This will add the query to the products shortcode query in the ‘products’ page and will exclude all the products from the clothing and music category.

Another heads up, woocommerce uses the filter for all the shortcodes available like product category, product categories etc, so using a conditional statement like I have used ( is_page ) above is advisable.

facebook-profile-picture

Maruti Mohanty

I am a happy go lucky WordPress Developer, who loves biking, movies and learning different languages

   

You may also like...

2 Responses

  1. Mudasar says:

    Hi,

    Thank you for providing this information. I am trying to show best selling products from one specific category. I do not want to show other category products in best selling products list.

    I have tried to modify your code to suit my need and put it into the functions.php of my child theme. This is what I changed:

    function se_customize_product_shortcode( $args, $atts ) {
    if ( is_page( ‘best_selling_products’ ) ) {
    $args[‘tax_query’] = array(
    array(
    ‘taxonomy’ => ‘product_cat’,
    ‘field’ => ‘slug’,
    ‘terms’ => array( ‘personalised-cookies’, ‘Flavour My Cookie’, ‘ready-to-bake-cookie-dough’ ),
    ‘operator’ => ‘NOT IN’
    )
    );
    }

    return $args;
    }
    add_filter( ‘woocommerce_shortcode_products_query’, ‘se_customize_product_shortcode’, 10, 2 );

    When I use the shortcode “best_selling_products”, it still shows products from all of the categories. What am I doing wrong? Does this function need to be called from somewhere?

    Thanks.

    Regards

    Mudasar

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: