Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 606 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Update all variations prices of a variable product in Woocommerce

#1
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 );
}

This doesn't seem to work:

(‘post_parent’ => $product->get_id())

Neither does this:

($variation_ID = $variation->ID;).
Reply

#2
>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 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 );
}

This code is tested on WooCommerce version 3+ and works
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through