wordpress 在 Woocommerce 中隐藏“缺货”产品
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24480982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hide 'out of stock' products in Woocommerce
提问by janlindso
Under "Products" and "Inventory" I have checked the following setting: "Hide out of stock items from the catalog"
在“产品”和“库存”下,我检查了以下设置:“隐藏目录中的缺货商品”
Now all sold out products are hidden in the archive/category view. So far so good.
现在所有售罄的产品都隐藏在存档/类别视图中。到现在为止还挺好。
The problem is that the hidden (out of stock) products are counted per page. So if there are 3 products that are sold out on the first page, only the ones in stock are showing (6).
问题是隐藏的(缺货)产品是按页面计算的。因此,如果第一页上有 3 种产品售罄,则仅显示有库存的产品 (6)。
It also seems that these "hidden" products still are searchable as well, and visible through the different widgets.
这些“隐藏”的产品似乎也仍然可以搜索,并且可以通过不同的小部件看到。
Any ideas how to fix this? I mean to REALLY hide products that are out of stock. Or do I need to manuallly remove them?
任何想法如何解决这一问题?我的意思是真正隐藏缺货的产品。还是我需要手动删除它们?
采纳答案by janlindso
Note to self: Always read the changelog from developer.
自我注意:始终阅读开发人员的更改日志。
Found the answer here: http://develop.woothemes.com/woocommerce/2014/02/solving-common-issues-after-updating-to-woocommerce-2-1/#category-counts-incorrect
在这里找到答案:http: //develop.woothemes.com/woocommerce/2014/02/solving-common-issues-after-updating-to-woocommerce-2-1/#category-counts-incorrect
In case the product counts for categories are showing a too high or too low number, after updating to WooCommerce 2.1 there is an easy workaround.
Go to the ‘Tools' tab inside the WooCommerce > System Status of your WordPress administration panel. Here you first use the ‘Recount terms' button and after that use the ‘Clear transients' button. This will force the system to recount all the products the next time a category is loaded.
如果类别的产品计数显示过高或过低的数字,更新到 WooCommerce 2.1 后,有一个简单的解决方法。
转到 WooCommerce > WordPress 管理面板的系统状态中的“工具”选项卡。在这里,您首先使用“重新计算术语”按钮,然后使用“清除瞬变”按钮。这将强制系统在下次加载类别时重新计算所有产品。
Update: Also remember that it is not enough to change stock quantity to 0. You must also set "Stock status" to "Out of stock". If not the product will be counted in the shop, even if there are no products in stock.
更新:还要记住,将库存数量更改为 0 是不够的。您还必须将“库存状态”设置为“缺货”。如果没有,即使没有库存产品,该产品也将被计算在商店中。
回答by patrickzdb
You can try adding this to your theme's functions.php file:
您可以尝试将其添加到主题的 functions.php 文件中:
add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $q ) {
if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;
if ( ! is_admin() ) {
$q->set( 'meta_query', array(array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => 'NOT IN'
)));
}
remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
}
I modified the code from this URL: http://www.wptaskforce.com/how-to-exclude-one-or-more-category-in-woocommerce-shop-page/
我从这个 URL 修改了代码:http: //www.wptaskforce.com/how-to-exclude-one-or-more-category-in-woocommerce-shop-page/
Saved here again just in case that site goes offline: (this code excludes certain product categories)
再次保存在这里以防网站离线:(此代码不包括某些产品类别)
add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $q ) {
if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;
if ( ! is_admin() ) {
$q->set( 'tax_query', array(array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'PUT YOUR CATEGORY HERE' ), // Don't display products in the membership category on the shop page . For multiple category , separate it with comma.
'operator' => 'NOT IN'
)));
}
remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
}
回答by klaudia
I found easier way, if anybody is still looking for hiding out of stock products in woocommerce, follow these easy steps without editing html !
我找到了更简单的方法,如果有人仍在寻找在 woocommerce 中隐藏缺货的产品,请按照这些简单的步骤操作,而无需编辑 html!
- Go to WooCommerce -> Settings
- Go to Inventory
- There's a checkbox that says something about our problem, however it goes in english :-) you'll find what you need
- Save
- 转到 WooCommerce -> 设置
- 去库存
- 有一个复选框说明了我们的问题,但它是英文的:-) 你会找到你需要的
- 节省
回答by albaiti
that will only work if you are using the official woocommerce shortcodes , but if you creating a page with visual composer and using customized plugins or 3rd party plugins or shortcodes , the first step is to for the query that run from the loop then you modify it to something like this
这仅在您使用官方 woocommerce 简码时才有效,但如果您使用可视化编辑器创建页面并使用自定义插件或第 3 方插件或简码,则第一步是针对从循环运行的查询,然后对其进行修改像这样的事情
$params = array(
'posts_per_page' => 5,
'post_type' => array('product', 'product_variation'),
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'instock'
)
)
);
the most important part that you have to be sure of is
你必须确定的最重要的部分是
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'instock'
)
)