Update all variations prices of a variable product in Woocommerce
First in your code
‘
or’
should be replaced by'
. Also if used$post-ID
should be replaced by$post->ID
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 customized version of your code instead:
global $post;$regular_price = 13;// Only for product post typeif( $post->post_type == 'product' ) $product = wc_get_product( $post->ID ); // An instance of the WC_Product object// Only for variable productsif( $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 );}
This code is tested on WooCommerce version 3+ and works