skip to Main Content

I’m trying to update max quantity for all variations of my variable product to ie 345.

$available_variations = $product->get_available_variations();


foreach ($available_variations as $variation) 
{ 
$variation_ID = $variation->ID;
update_post_meta( $variation_ID , 'max_qty', 345 );

}

It doesn’t happen.

2

Answers


  1. You are not using the correct way to get the variation ID and the product meta key max_qty doesn’t exist in WooCommerce database on wp_postmeta table for product_variation post type.

    Instead you can use the following filter hook to set a specific maximum quantity to product variations:

    add_filter( 'woocommerce_available_variation', 'wc_available_variation_max_qty', 10, 3 );
    function wc_available_variation_max_qty( $data, $product, $variation ) {
        $data['max_qty'] = 345;
    
        return $data;
    }
    

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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search