简体   繁体   中英

How to make Woocommerce shortcodes display a notice if no products found

Currently, if we use a shortcode like [products paginate="true" limit="30" on_sale="true"] , but there are no products to display, nothing is returned. There is no information like No products founds . I would like to modify this functionality

I found an woocommerce_shortcode_products_query_results filter that at first glance can change it, but I'm not sure if it's the right way to modify it

add_filter('woocommerce_shortcode_products_query_results', function ($results) {
    if (!empty($results->ids)) {
        return $results;
    } else {
        echo 'No products found';
    }
});

Should I do it differently? There is a better, more flexible way? If so, how?

Since @CBroe posted his answer as a comment, I decided to copy it as the answer as it is correct and it will be easier for other users to find it:

No, that filter is used to manipulate the database query that selects the products in the first place. I think woocommerce_shortcode_products_loop_no_results is what you should hook into. https://github.com/woocommerce/woocommerce/blob/master/includes/shortcodes/class-wc-shortcode-products.php#L680 Output whatever message you want to display when there were no products found there.

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