在 Woocommerce 中为产品变体设置不同的输入数量值 [英] Set different input quantity values for product variations in Woocommerce

查看:8
本文介绍了在 Woocommerce 中为产品变体设置不同的输入数量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 woocommerce 中创建了一个可变产品,它有 3 个产品变体.

I created a variable product in woocommerce that has 3 product variations.

我的问题是每个产品变体的销售数量必须是固定的.

My problem is that the quantity to sell must be fixed for each product variation.

例如:
2 红色,每个 10,00 欧元
3 蓝色,每个 12,00 欧元
6 绿色,每个 16,00 欧元

for example:
2 Red for 10,00€ each
3 Blue for 12,00€ each
6 Green for 16,00€ each

因此客户必须订购 3 个红色或 6 个蓝色或 12 个绿色(不多不少).我知道如何管理最小和最大数量,但我不知道如何设置默认数量值.

So the customer MUST order 3 red or 6 blue or 12 green (not more not less). I know how to manage the minimum and maximum quantities but I don't know how to set the default quantity value.

任何帮助将不胜感激.

推荐答案

在以下两个函数中,您需要先设置父变量产品ID,并在第一个函数上设置每个变量ID对应的固定数量.

In the following two functions, you will need to set first the parent variable product ID and on the first function each variation ID with the corresponding fixed quantity.

>

要根据每个选定的变体动态设置输入数量,唯一的方法是使用 Javascript (Jquery).这是在第二个函数中完成的.

To set the dynamically the input quantity depending on each selected variation, the only way is to use Javascript (Jquery). This is done in The 2nd function.

代码:

add_filter( 'woocommerce_available_variation', 'custom_variation_min_max_qty', 10, 3 );
function custom_variation_min_max_qty( $data, $product, $variation ) {
    // Your variable product ID
    $variable_id = 73;

    if( $product->get_id() == $variable_id ) {
        // Set 1st variation ID
        if( $variation->get_id() == 1015 ){
            $qty = 3; // Set the quantity
        } 
        // Set 2nd variation ID
        elseif( $variation->get_id() == 1014 ){
            $qty = 6; // Set the quantity
        }
        // Set 3rd variation ID
        elseif( $variation->get_id() == 1013 ){
            $qty = 12; // Set the quantity
        }
    }

    if( isset($qty) ) {
        $data['min_qty'] = $qty;
        $data['max_qty'] = $qty;
    }

    return $data;
}

add_action( 'woocommerce_after_single_variation',  'change_variation_input_quantity_script' );
function change_variation_input_quantity_script() {
    global $product;

    // Your variable product ID
    $variable_id = 73;

    if( $product->get_id() != $variable_id ) return;

    // Output Javascript
    ?>
    <!-- JS Thankyou Script -->
    <script type="text/javascript">
    jQuery(function($) {
        var a = 'div.quantity > input.qty';
        // On load
        setTimeout(function(){
            $(a).val($(a).prop('min'));
        }, 300);

        // On change / select a variation
        $('.variations_form select').on( 'blur', function(){
            if( $('input[name="variation_id"]').val() > 0 )
                $(a).val($(a).prop('min'));
        })

    });
    </script>
    <?php
}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

可以根据特定的产品属性值自动进行正确的变体检测...

It's possible to automate the right variation detection based on specific product attribute values...

就像在您的示例中一样,可以定位变体的颜色产品属性值.您需要在函数中定义颜色"产品属性分类法,即pa_color.

Like in your example, this can be done targeting the Color product attribute value of a variation. You will need to define in the function, the "Color" product attribute taxonomy which is pa_color.

因此您将用这个函数替换第一个函数:

So you will replace the first function by this one:

add_filter( 'woocommerce_available_variation', 'custom_variation_min_max_qty', 10, 3 );
function custom_variation_min_max_qty( $data, $product, $variation ) {
    // Your variable product ID
    $variable_id = 73;

    if( $product->get_id() == $variable_id ) {
        // Define your product attribute (always start with "pa_" + the slug)
        $taxonomy = 'pa_color';

        foreach($data['attributes'] as $attribute => $value_slug ){
            if( $attribute == 'attribute_' . $taxonomy ) {
                // set your color slugs below with the correct quantity
                if ( $value_slug == 'red' ) 
                {
                    $qty = 3; // Set the quantity for "Red" color
                    break;
                }
                elseif ( $value_slug == 'blue' )
                {
                    $qty = 6; // Set the quantity for "Blue" color
                    break;
                }
                elseif ( $value_slug == 'green' )
                {
                    $qty = 12; // Set the quantity for "Green" color
                    break;
                }
            }
        }
    }

    if( isset($qty) ) {
        $data['min_qty'] = $qty;
        $data['max_qty'] = $qty;
    }

    return $data;
}

您将保持第二个函数的原样.

You will keep The second function as its is.

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于在 Woocommerce 中为产品变体设置不同的输入数量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆