Динамически изменять шаг изменения продукта - PullRequest
1 голос
/ 07 июня 2019

Я хочу изменить количество входных аргументов 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 динамически (возможно, это не такправильный путь)

Спасибо за помощь

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...