首页 文章

使用自定义元数据隐藏“缺货”产品在Woocommerce中

提问于
浏览
1

我添加了一个名为 external_stock 的自定义元字段,其中WP All Import每隔3小时导入我们供应商提供的所有产品库存 . 我们在实际商店中获得的产品数量正在正常库存字段中输入 .

我想要实现的是,正常库存和 external_stock 均为0的产品不会在网上商店中显示 .

我已经编辑了一个插件,每当我们的股票为0但外部股票> 0时,产品页面显示'在x天内可用',当两个股票都是0时,它将显示'缺货',但客户仍然可以订购'Out Of Stock'产品,这就是我想隐藏它们的原因 .

1 回答

  • 1

    Woocommerce的更新3

    自Woocommerce 3以来,产品 stock status 不再被设置为产品元数据 .

    它现在是 handle by product_visibility 自定义分类法 under outofstock term .

    因此,您需要使用Tax查询来隐藏缺货产品:

    add_action( 'woocommerce_product_query', 'action_product_query', 10, 2 );
    function action_product_query( $q, $query ) {
        // Get any existing Tax query
        $tax_query = $q->get( 'tax_query');
    
        // Define an additional tax query 
        $tax_query = array(
            'taxonomy' => 'product_visibility',
            'field'    => 'slug',
            'terms'   => array('outofstock'),
            'compare' => 'NOT IN',
        );
    
        // Set the new merged tax query
        $q->set( 'meta_query', $tax_query );
    }
    

    代码位于活动子主题(或活动主题)的function.php文件中 . 经过测试和工作 .

    对于包含特定元数据的产品,您将使用:

    add_action( 'woocommerce_product_query', 'action_product_query', 10, 2 );
    function action_product_query( $q, $query ) {
        // Get any existing Tax query
        $tax_query = $q->get( 'tax_query');
    
        // Get any existing meta query
        $meta_query = $q->get( 'meta_query');
    
        // Define an additional tax query 
        $tax_query = array(
            'taxonomy' => 'product_visibility',
            'field'    => 'slug',
            'terms'   => array('outofstock'),
            'compare' => 'NOT IN',
        );
    
        // Define an additional meta query 
        $meta_query = array(
            'key'     => 'external_stock',
            'value'   => '0', //  <===  Set here your desired value (if needed)
            'compare' => '>', //  <===  Set Here the correct compare argument (if needed)
        );
    
        // Set the new merged tax query
        $q->set( 'tax_query', $tax_query );
    
        // Set the new merged meta query
        $q->set( 'meta_query', $meta_query );
    }
    

    原始答案:

    您可以尝试在 woocommerce_product_query 动作钩子中挂钩的这个自定义函数:

    add_action( 'woocommerce_product_query', 'action_product_query', 10, 2 );
    function action_product_query( $q, $query ) {
        // Get any existing meta query
        $meta_query = $q->get( 'meta_query');
    
        // Define an additional meta query 
        $q->set( 'meta_query', array( array(
            'key'     => '_stock_status',
            'value'   => 'outofstock',
            'compare' => 'NOT LIKE',
        ) ) );
    
        // Set the new merged meta query
        $q->set( 'meta_query', $meta_query );
    }
    

    代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中 .

    代码经过测试和运行 .

    它将从商店和档案页面中删除所有“缺货”产品 . 但它不会隐藏可变产品的单个产品页面中的“缺货”变化 .

    对于您的自定义 meta_key external_stock ,您必须以这种方式添加它:

    add_action( 'woocommerce_product_query', 'action_product_query', 10, 2 );
    function action_product_query( $q, $query ) {
        // Get any existing meta query
        $meta_query = $q->get( 'meta_query');
    
        $meta_query = array( 
            'relation' => 'AND', // can be also 'OR'
            array(
                'key'     => '_stock_status',
                'value'   => 'outofstock',
                'compare' => 'NOT LIKE',
            ),
            array(
                'key'     => 'external_stock',
                'value'   => '0', //  <===  Set here your desired value (if needed)
                'compare' => '>', //  <===  Set Here the correct compare argument (if needed)
        ) );
    
        // Set the new merged meta query
        $q->set( 'meta_query', $meta_query );
    }
    

    这是未经测试的,需要由您进行设置和测试


    官方文件:WordPress Class Reference WP_Query - Custom Field Parameters

相关问题