简体   繁体   English

如果 WooCommerce 中的一个变体缺货,则将所有变体设置为缺货

[英]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.可以在父变量产品上管理库存。

  1. Enable stock management on the variable product (on Inventory tab) and set the stock there.在可变产品上启用库存管理(在库存选项卡上)并在那里设置库存。
  2. 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.

相关问题 如果 WooCommerce 中的所有变体都缺货,则显示已售罄的灰色按钮 - Display a Sold out greyed button if all variations are out of stock in WooCommerce 如果所有变体都缺货,则从目录中隐藏 WooCommerce 可变产品 - Hide WooCommerce variable product from catalog if all variations are out of stock 显示和隐藏缺货变化 woocommerce - Showing and hiding out of stock variations woocommerce 在 Woocommerce 中的产品变体旁边显示“缺货” - Show “Out Of Stock” next to product variation in Woocommerce 显示在 WooCommerce 可变产品上售罄,当所有变体都缺货时 - Display sold out on WooCommerce variable product when all variations are out of stock 根据Woocommerce中的订单数将所有产品状态设置为“无货” - Set all products status “out of stock” based on orders count in Woocommerce 显示用于 woocommerce 缺货产品变体的自定义 div 块 - Display a custom div block for woocommerce out of stock product variations 向显示为色板的变体添加“缺货”类 - WooCommerce - Adding an “out-of-stock” class to variations shown as swatches - WooCommerce 通过 WooCommerce 中的自定义库存数量减少来灰显“缺货”产品变化 - Greying out “out-of-stock” product variations with custom stock quantity reduction in WooCommerce 在Woocommerce中向显示“缺货”的变体选项添加自定义类 - Add a custom class to variation options displaying “Out Of Stock” in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM