如果找不到产品,如何使 Woocommerce 简码显示通知

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

目前,如果我们使用像 [products paginate="true" limit="30" on_sale="true"] 这样的短代码,但没有要显示的产品,则不会返回任何内容。没有No products founds这样的信息。我想修改这个功能

我找到了一个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';
    }
});

我应该换一种方式吗?有更好、更灵活的方法吗?如果可以,怎么做?

自从 @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. 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.