admin管理员组

文章数量:1303389

I have a variable product called flyer in woocommerce, and I want to set its default quantity input value to 500 as its min quantity. This is what I have done so far, but the default quantity is still 1.

add_filter( 'woocommerce_available_variation', 'jk_woocommerce_available_variation' ); // Variations
function jk_woocommerce_available_variation( $args ) {

    if( has_term('flyer') )

       $args['max_qty'] = 10000;        // Maximum value (variations)
       $args['min_qty'] = 500;
       $args['input_value']=500;

    return $args;
}

I have a variable product called flyer in woocommerce, and I want to set its default quantity input value to 500 as its min quantity. This is what I have done so far, but the default quantity is still 1.

add_filter( 'woocommerce_available_variation', 'jk_woocommerce_available_variation' ); // Variations
function jk_woocommerce_available_variation( $args ) {

    if( has_term('flyer') )

       $args['max_qty'] = 10000;        // Maximum value (variations)
       $args['min_qty'] = 500;
       $args['input_value']=500;

    return $args;
}
Share Improve this question edited May 22, 2019 at 16:07 rudtek 6,3535 gold badges30 silver badges52 bronze badges asked May 22, 2019 at 14:49 mehdi lagmehdi lag 233 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

On woocommerce_available_variation hook the $args array doesn't include input_value argument.

Also You are not using has_term() conditional function in the right way for product categories.

So try the following code instead:

add_filter( 'woocommerce_available_variation', 'customizing_available_variation', 10, 3 ); // Variations
function customizing_available_variation( $args, $product, $variation ) {
    if( has_term( 'flyer', 'product_cat', $product->get_id() ) ) {
        $args['max_qty']     = 10000;        // Maximum value (variations)
        $args['min_qty']     = 500;
        // $args['input_value'] = 500; // This argument doesn't exist here
    }
    return $args;
}

add_filter( 'woocommerce_quantity_input_args', 'customizing_quantity_input_args', 10, 2 );
function customizing_quantity_input_args( $args, $product ) {
    if( $product->is_type('variable') && has_term( 'flyer', 'product_cat', $product->get_id() ) ) {
        $args['max_value']   = 10000; // Maximum value (variable product)
        $args['min_value']   = 500;
        $args['input_value'] = 500;
    }
    return $args;
}

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

本文标签: Set a default quantity input value for a variable product category in WooCommerce