How to update each product variation of a variable product in WooCommerce How to update each product variation of a variable product in WooCommerce wordpress wordpress

How to update each product variation of a variable product in WooCommerce


First your $dataCSV (external info) should require to be converted in a multidimensional explicit formatted array, instead of a string…

Then you can loop through each variation ID of the parent variable product this way (and update data):

<?phpif ( $product->is_type( 'variable' ) ) {        $dataCSV = "36,2.0" , "37,3.0" , "39,4.0"; // <== This requires to be a multidimensional array        $attribute_label_name = 'Tallas';        // Loop through the variation IDs    foreach( $product->get_children() as $key => $variation_id ) {        // Get an instance of the WC_Product_Variation Object        $variation = wc_get_product( $variation_id );                // Get the variation attaribute "size" value         $size = $product->get_attribute($attribute_label_name);                // ------------------------------        // Then in between your code HERE … / …        // ------------------------------                // Set the stock quantity        $variation->set_stock_quantity($stock_quantity);        // Set the stock status        $variation->set_stock_status('instock');        // Set price        $variation->set_regular_price($price);        $variation->set_price($price);        // Save data (refresh cached data)        $variation->save();    }}?>