1

i want to change the quantity input args STEP based on an variation attribute. For exemple : variable product with attribute pa_conditionnement. So, two variations : pa_conditionnement(cc12) = 12 and pa_conditionnement(cc6) = 6

When a choose cc12, I would like min and step = 12 and if a choose cc3 min and step = 6

I arrived to change the min quantity based on the the attribute but not the step

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
}

step in woocommerce_available_variation have no effect and I don't arrive to do something in woocommerce_quantity_input_args dynamically (maybe it's not the right way)

Thank for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.