[英]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.目前,如果我们使用
[products paginate="true" limit="30" on_sale="true"]
的短代码,但没有要显示的产品,则不会返回任何内容。 There is no information like No products founds
.没有类似
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我发现了一个
woocommerce_shortcode_products_query_results
过滤器,乍一看可以更改它,但我不确定它是否是修改它的正确方法
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:由于@CBroe将他的答案作为评论发布,因此我决定将其复制为答案,因为它是正确的,其他用户更容易找到它:
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.我认为
woocommerce_shortcode_products_loop_no_results
是您应该关注的。 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.https://github.com/woocommerce/woocommerce/blob/master/includes/shortcodes/class-wc-shortcode-products.php#L680 Output 您想在没有找到产品时显示的任何消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.