I need to get all variation id and update price in loop. Simple query and loop looks like:
$params = array(
‘posts_per_page’ => -1,
‘post_type’ => ‘product_variation’,
‘post_parent’ => $product->get_id() // tried $post-ID
);
$variations = get_posts( $params );
foreach ( $variations as $variation ) {
$variation_ID = $variation->ID; // tried $post-ID, $product->get_id()
$regular_price=34;
update_post_meta( $variation_ID, ‘_regular_price’, (float)$regular_price );
update_post_meta( $variation_ID, ‘_price’, (float)$regular_price );
}
I think not working this:
(‘post_parent’ => $product->get_id())
or this:
($variation_ID = $variation->ID;).
Depending on where and how you are using this code, you should try to include
global $post;
first to be able to use theWP_Post
object$post
.Then you could try to use this customized version of your code instead:
This code is tested on WooCommerce version 3+ and works