动态更改步骤产品变化

时间:2019-06-07 10:04:45

标签: woocommerce

我想根据变化属性来更改数量输入参数STEP。 例如:具有属性pa_conditionnement的变量乘积。 因此,有两种变体:pa_conditionnement(cc12)= 12和pa_conditionnement(cc6)= 6

当选择cc12时,我希望min和step = 12,如果选择cc3 min和step = 6

我到达是根据属性而不是步骤更改最小数量

add_filter( 'woocommerce_quantity_input_args', 'jk_woocommerce_quantity_input_args', 10, 2 );
function jk_woocommerce_quantity_input_args( $args, $product) {

  $args['step'] = $qty; // Quantity steps

  return $args;
}


add_filter( 'woocommerce_available_variation', 'custom_variation_min_max_qty', 10, 3 );
function custom_variation_min_max_qty( $data, $args, $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_conditionnement';

        foreach($data['attributes'] as $attribute => $value_slug ){
            if( $attribute == 'attribute_' . $taxonomy ) {
                // set your color slugs below with the correct quantity
                if ( strcasecmp($value_slug, 'cbo12') == 0 )
                {
                    $qty = 12; // Set the quantity for "Red" color
                    break;
                }
                elseif ( strcasecmp($value_slug, 'cbo6') == 0 )
                {
                    $qty = 6; // Set the quantity for "Blue" color
                    break;
                }
                elseif ( strcasecmp($value_slug, 'cbo3') == 0)
                {
                    $qty = 3; // Set the quantity for "Green" color
                    break;
                }
                elseif ( strcasecmp($value_slug, 'cbo2') == 0)
                {
                    $qty = 2; // Set the quantity for "Green" color
                    break;
                }
                elseif ( strcasecmp($value_slug, 'cc12') == 0 )
                {
                    $qty = 12; // Set the quantity for "Green" color
                    break;
                }
                elseif ( strcasecmp($value_slug, 'cc6') == 0)
                {
                    $qty = 6; // Set the quantity for "Green" color
                    break;
                }
            }
        }
    //}

    if( isset($qty) ) {
        $data['min_qty'] = $qty; // Minimum value (default = 1)
        $data['input_value'] = $qty; // Start from this value (default = 1)
        $data['min_value']   = $qty; // Min value (default = 0)
        $data['step']   = $qty; // Min value (default = 0)
    }

    return $data;
}

add_action( 'woocommerce_after_single_variation',  'change_variation_input_quantity_script' );
function change_variation_input_quantity_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
}

woocommerce_available_variation中的步骤无效,并且我没有动态地在woocommerce_quantity_input_args中执行任何操作 (也许这不是正确的方法)

感谢您的帮助

0 个答案:

没有答案