Change the text without modifying the template file.
I was working on a client site where some of the categories were deliberately empty (she hires out designer dresses and that’s simply not happening during the corona virus pandemic). The default message is ‘No products were found matching your selection.‘ which is cold and robotic.
In a template file
The text is in the woocommerce/templates/loop/no-products-found.php file but there’s no apply_filters() call to allow me to change the text with code. I found that the template file is called by the wc_no_products_found() function which is called by the ‘woocommerce_no_products_found‘ action.
I could copy the template file into my theme where it will be automatically picked up by WooCommerce. This is not an option because my code is in a plugin. The second option is to copy the file into the plugin directory and add code to make WooCommerce use that file.
The third option is to use remove_action() and add_action() to use my own code instead of a template file at all.
The code
Potential gotcha (though very low risk)
The current version of the template file has not been changed since WooCommerce 2.0.0 (March 2013) so it’s a very stable file that is unlikely to change. WooCommerce uses the version number in the template file to highlight when the file was updated. If I copied version 2.0.0 of the file into a child theme then WooCommerce would highlight when the original and copy have different version numbers.
With the code above there is no copy of the template file to compare things to so there is a tiny risk that the code could unknowingly get out of sync with the original file. As I said, this is a very low risk for this template file.
Enhancements
In the code I check whether we are in a product category. The ‘woocommerce_no_products_found‘ action in the archive-product.php template file which is used for product category archives and the shop page. The customised code won’t be changed for the shop page (and if you have no products in the shop page you’ve got big problems!).
You could add further checks to display different messages for different categories e.g. if you have products and services you could change the text from “We do not currently have any %s products for sale.” to something like “We are not able to provide a %s service at this time.“
This remove_action/add_action method of changing a template file could be applied to other files. The loop-start.php and loop-end.php files open and close the ul lists so there would be the opportunity to add some extra markup in those areas.
Most of the WooCommerce template files are not trivial so this method is probably not suitable to them.
Thanks for this. It works great. Can i tweak this message for the empty category pages to also show for the empty product tags pages and all empty attributes archive pages?
And for the empty search terms show like [No products matched your search for “search term”. Please check your spelling or use fewer keywords ]
@Lawrence – You could add extra tests to the filter function. Use
is_product_tag()
to customise the text for an empty tag page.For the search text, this comes from the theme. In Storefront it is in content-none.php file – text is “Sorry, but nothing matched your search terms. Please try again with some different keywords.” To change this you need to create a child theme. It may be different for the theme you are using.