PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, May 9, 2022

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

 May 09, 2022     php, product, woocommerce, wordpress     No comments   

Issue

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?


Solution

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.



Answered By - kanlukasz
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing