1
votes

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.

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

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.

Any help will be appreciated.

1

1 Answers

1
votes

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.

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.

The code:

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
}

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...

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.

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