[英]WooCommerce: filter products by attribute and hide product if variation is out of stock
I use the built in WooCommerce filter by attribute widget which works for what I need, but my issue is, that if I filter on a size, I get all products with that size as an attribute, also when they are out of stock...我使用内置的 WooCommerce 过滤器按属性小部件,它适用于我需要的东西,但我的问题是,如果我过滤尺寸,我会得到所有具有该尺寸的产品作为属性,即使它们缺货时也是如此。 .
My products are created as a variable product with variations on size and color我的产品是作为可变产品创建的,具有不同的尺寸和颜色
What I want, is to filter a size and only display products in that size that is in stock.我想要的是过滤尺寸并仅显示该尺寸的库存产品。
It may not be possible for variable products and their variations (but only for other product types) .对于可变产品及其变体(但仅适用于其他产品类型)可能是不可能的。
It can only works for variable products when stock management is handled by the product variation itself (but not by the variations).当库存管理由产品变体本身(而不是变体)处理时,它仅适用于可变产品。
Note that WooCommerce product query doesn't handle post type 'product_variation', but only post type 'product'.请注意,WooCommerce 产品查询不处理帖子类型“product_variation”,而只处理帖子类型“产品”。
To exclude out of stock products using widget filters (except for variable products) you can use:要使用小部件过滤器排除缺货产品(可变产品除外),您可以使用:
add_filter( 'woocommerce_product_query_tax_query', 'filter_product_query_tax_query' );
function filter_product_query_tax_query( $tax_query ){
if ( ! is_admin() && isset($_GET['filter_color']) && ! empty($_GET['filter_color']) ) {
// Exclude products "out of stock"
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => array('outofstock'),
'operator' => 'NOT IN'
);
}
return $tax_query;
}
Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的functions.php文件。 Tested and works.
测试和工作。
I've used this code for my stores to hide product sizes that are not in stock whilst filtering.我已将此代码用于我的商店,以在过滤时隐藏没有库存的产品尺寸。
add_action( 'woocommerce_before_shop_loop_item_title', 'scr_out_of_stock_variations_loop' );
function scr_out_of_stock_variations_loop(){
global $product;
if ( $product->is_type('variable')) { // if variation product is out of stock
$available = $product->get_available_variations();
if ( $available )foreach ( $available as $instockvar ) {
if ( isset($instockvar['attributes']['attribute_pa_size'] ) ) {
if(isset($_GET['filter_size'])){
$destostock = $_GET['filter_size'];
if($destostock ==$instockvar['attributes']['attribute_pa_size']){
if($instockvar['is_in_stock']){
global $product;
$id = $product->get_id();
echo "<style>.post-$id{display: block !important}</style>";
}else{
global $product;
$id = $product->get_id();
echo "<style>.post-$id{display: none}</style>";
}
}
}
}
}
}
}
filter_size will be converted in array if many filter being selected.如果选择了多个过滤器,filter_size 将转换为数组。 As for product variations, best if its stored in an array as well and use array_intersect to search if one of the selected item available in stock.
至于产品变体,最好将其存储在一个数组中,并使用 array_intersect 搜索所选项目之一是否有库存。
add_filter( 'woocommerce_product_is_visible', 'hide_product_with_outofstock_variation', 10, 2 );
if(!function_exists('hide_product_with_outofstock_variation')){
function hide_product_with_outofstock_variation( $is_visible, $id ) {
if(isset($_GET['filter_size'])){
$filter_size = explode(',',$_GET['filter_size']);
$product = wc_get_product($id);
if ( $product->is_type('variable')) { // if variation product is out of stock
$available = $product->get_available_variations();
$available_size = array();
if ( $available )foreach ( $available as $instockvar ) {
array_push($available_size, $instockvar['attributes']['attribute_pa_size']);
}
if (count(array_intersect($filter_size, $available_size)) === 0) {
$is_visible = false;
}else{
$is_visible = true;
}
}
}
return $is_visible;
}
}
This code works for me这段代码对我有用
You can hide out of stock products by checking the option of "Out of stock visibility" on the Settings page:您可以通过检查设置页面上的“缺货可见性”选项来隐藏缺货产品:
Woocommerce > Settings > Product > Inventory Woocommerce > 设置 > 产品 > 库存
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.