为Woocommerce中的产品变体设置不同的输入数量值

问题描述 投票:1回答:1

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

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

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

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

任何帮助将不胜感激。

php jquery wordpress woocommerce variations
1个回答
0
投票

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

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

代码:

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文件中。经过测试和工作。


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

与您的示例中一样,可以针对变体的“颜色”产品属性值进行此操作。您需要在函数中定义“颜色”产品属性分类,即pa_color

所以你将用这个替换第一个函数:

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;
}

你将保持第二个功能。

代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。

© www.soinside.com 2019 - 2024. All rights reserved.