[英]Set all variations out of stock if one variation is out of stock in WooCommerce
We run a self-storage system with WooCommerce and we use WooCommerce Subscriptions plugin.我们使用 WooCommerce 运行自存储系统,并使用 WooCommerce 订阅插件。 Our storage units is a unique product with a Variable Subscription.我们的存储单元是具有可变订阅的独特产品。 Each variation has a different billing period (1 month, 3 months, 6 months and 12 months).每个变体都有不同的计费周期(1 个月、3 个月、6 个月和 12 个月)。 I need the whole parent product, or at least all variations, to be out of stock if one variation is out of stock.如果一个变体缺货,我需要整个父产品或至少所有变体都缺货。
I didn't find any related setting and I didn't find how to make that possible yet.我没有找到任何相关的设置,也没有找到如何实现这一点。
Any help is appreciated.任何帮助表示赞赏。
The following will make all variations out of stock (for specific variable product(s)), when one variation is out of stock (also works with WooCommerce subscriptions) :当一个变体缺货时(也适用于 WooCommerce 订阅),以下内容将使所有变体缺货(针对特定可变产品) :
add_filter('woocommerce_available_variation', 'set_all_variations_out_of_stock', 10, 3 );
function set_all_variations_out_of_stock( $data, $product, $variation ) {
// Set the Id(s) of the related variable product(s) below in the array
if( in_array( $product->get_id(), array(738) ) ){
$out_of_stock = false; // initializing
// Loop through children variations of the parent variable product
foreach( $product->get_visible_children() as $_variation_id ) {
if( $_variation_id != $data['variation_id'] ) {
$_variation = wc_get_product($_variation_id);
if( ! $_variation->is_in_stock() ) {
$out_of_stock = true; // Flag as out of stock
break;
}
}
}
if ( $out_of_stock ) {
$data['availability_html'] = '<p class="stock out-of-stock">'. __('Out of stock', 'woocommerce') .'</p>';
$data['is_in_stock'] = false;
}
}
return $data;
}
Code goes in function.php file of your active child theme (active theme).代码位于活动子主题(活动主题)的 function.php 文件中。 Tested and works.测试和工作。
Important note:重要的提示:
Stock can be managed on the parent variable product.可以在父变量产品上管理库存。
- Enable stock management on the variable product (on Inventory tab) and set the stock there.在可变产品上启用库存管理(在库存选项卡上)并在那里设置库存。
- Disable stock management on each variation for this variable product.禁用此可变产品的每个变体的库存管理。
You are done.你完成了。 The stock management is now handled on the variable product.现在对可变产品进行库存管理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.