简体   繁体   English

Woocommerce - 是否可以根据该类别下产品的库存状态隐藏产品类别?

[英]Woocommerce - Is it possible to hide a product category based on the stock status of the products that are under this category?

I tried to hide a category from my shop based on the factor that this category does not contain even 1 product that is in stock.我试图从我的商店中隐藏一个类别,因为该类别甚至不包含 1 个库存产品。 If even 1 product is in stock then the category will appear.如果甚至有 1 件产品有货,那么该类别将出现。

I tried something like the code below with no avail:我尝试了类似下面的代码但无济于事:

function hide_out_of_stock_categories( $list_args ){
 if ( ! $product->managing_stock() && ! $product->is_in_stock() ){
            $list_args[ 'hide_empty' ] = 1;
            return $list_args;
}
}

I need to search first for the products inside the category and then check if the product stock status is in stock or out of stock, but I am missing something here.我需要先搜索类别内的产品,然后检查产品库存状态是有货还是缺货,但我在这里遗漏了一些东西。

To hide or remove the empty categories listed here, add the following code lines at the end of your theme's functions.php file.要隐藏或删除此处列出的空类别,请在主题 functions.php 文件的末尾添加以下代码行。

function woo_hide_product_categories_widget( $list_args ){
 
      $list_args[ 'hide_empty' ] = 1;
 
      return $list_args;
 
}
 
add_filter( 'woocommerce_product_categories_widget_args', 'woo_hide_product_categories_widget' )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM