且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从 WooCommerce 目录中隐藏具有特定库存状态的所有产品

更新时间:2023-11-30 12:50:58

您应该更好地使用专用的 woocommerce_product_query_meta_query 过滤器钩子,如下所示,从您的商店中隐藏所有具有特定库存状态的产品(适用于自定义库存状态):

You should better use dedicated woocommerce_product_query_meta_query filter hook as follows, to hide, from your store, all products that have a specific stock status (works with custom stock status):

add_action( 'woocommerce_product_query_meta_query', 'custom_product_query_meta_query', 1000 );
function custom_product_query_meta_query( $meta_query ) {
    if ( ! is_admin() ) {
        $meta_query[] = array(
            'key'     => '_stock_status',
            'value'   => 'noproduzione',
            'compare' => '!=',
        );
    }
    return $meta_query;
}

代码位于活动子主题(或活动主题)的functions.php 文件中.自 WooCommerce 3 以来的所有版本都经过测试并适用.

Code goes in functions.php file of the active child theme (or active theme). Tested and works on all versions since WooCommerce 3.