隐藏“缺货"Woocommerce 中带有自定义元数据的产品 [英] Hide "out of stock" products with custom meta data In Woocommerce

查看:42
本文介绍了隐藏“缺货"Woocommerce 中带有自定义元数据的产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working on a WooCommerce webshop at the moment and I've added a custom meta field named external_stock where WP All Import imports the stock that is available at our supplier for all of our products every 3 hours. The amount of products we've got in our actual store is being entered in the normal stock field.

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

What I'm trying to achieve is that the products from which the normal stock and the external_stock are both 0 are not being displayed in the webshop.

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

I've already edited a plugin in a way that whenever our stock is 0 but the external stock is > 0 the product page displays 'Available within x days' and when both stocks are 0 it will display 'Out Of Stock', but customers can still order the 'Out Of Stock' products, and that's why I want to hide them.

推荐答案

Woocommerce 3 的更新

从 Woocommerce 3 开始,产品库存状态不再设置为产品元数据.

Since Woocommerce 3, product stock status is not anymore set as product meta data.

现在通过product_visibility自定义分类outofstock术语下处理.

It's now handle by product_visibility custom taxonomy under outofstock term.

因此,您需要改用税务查询,以隐藏缺货产品:

So you will need to use a Tax query instead, to hide out of stock products:

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( 'tax_query', $tax_query );
}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

您可以在 if 语句中使用任何 WooCommerce 条件标签来定位例如特定的产品类别或产品标签存档页面.

You can use any WooCommerce conditional tag in an if statement to target for example specific product category or product tag archive pages.


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


For products containing specific meta data, you will use:

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 );
}


原答案:


Original answer:

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

You could try this custom function hooked in woocommerce_product_query action hook:

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 文件或任何插件文件中.

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

代码已经过测试并且可以正常工作.

Code is tested and works.

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

It will remove all "out of stock" products from shop and archives pages. But it will not hide "out of stock" variations in single product pages for variable products.

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

For your custom meta_key external_stock, you will have to add it this way:

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 );
}

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

This is untested and need to be set and tested by you

官方文档:WordPress 类参考 WP_Query - 自定义字段参数

这篇关于隐藏“缺货"Woocommerce 中带有自定义元数据的产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆