If you want to update all variations prices of a variable product in WooCommerce? Depending on where and how you are using this code, you should try to include global $post; first to be able to use the WP_Post object $post.
Then you could try to use this customised version of your code instead:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | global $post; $regular_price = 13; // Only for product post type if( $post->post_type == 'product' ) $product = wc_get_product( $post->ID ); // An instance of the WC_Product object // Only for variable products if( $product->is_type('variable') ){ foreach( $product->get_available_variations() as $variation_values ){ $variation_id = $variation_values['variation_id']; // variation id // Updating active price and regular price update_post_meta( $variation_id, '_regular_price', $regular_price ); update_post_meta( $variation_id, '_price', $regular_price ); wc_delete_product_transients( $variation_id ); // Clear/refresh the variation cache } // Clear/refresh the variable product cache wc_delete_product_transients( $post->ID ); } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.