3

I need to update the sale price programmatically, on variable product and all his variations.

What kind of meta field do I need to add?

I'm trying to update main product such as:

update_post_meta($post_id, '_regular_price', '100');
update_post_meta($post_id, '_price', '50');
update_post_meta($post_id, '_sale_price', '50');

and then I update every single variations

update_post_meta($variation_id, '_regular_price', '100');
update_post_meta($variation_id, '_price', '50');
update_post_meta($variation_id, '_sale_price', '50');
update_post_meta($variation_id, 'attribute_pa_taglia', $term_slug);
update_post_meta($variation_id, '_stock', $stock);
update_post_meta($variation_id, '_stock_status', 'instock');
update_post_meta($variation_id, '_manage_stock', 'yes');

Back end: product detail, everything ok enter image description here

However backend (product list) and frontend get me old price

enter image description here

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
mariobros
  • 859
  • 4
  • 12
  • 31

4 Answers4

3

Furthermore, I've find other solution that work same:

$product_variable = new WC_Product_Variable($post_id);
$product_variable->sync($post_id);
wc_delete_product_transients($post_id);
mariobros
  • 859
  • 4
  • 12
  • 31
3

For April 2021, this is the code:

        $variation = wc_get_product_object( 'variation', $variation_id );
        $variation->set_props(
                array(
                    'regular_price' => $price,
                    'sale_price' => $sale_price,
                     )
            );
        $variation->save();

Source: function save_variations( $post_id, $post )

Gabriel Reguly
  • 340
  • 2
  • 9
2

Update: Prices are also cached in transient wp_options table.

Let say your product ID is 222, you will have that transients meta_keys in wp_options table (for this product ID):

'_transient_timeout_wc_product_children_22'
'_transient_wc_product_children_22'
'_transient_timeout_wc_var_prices_222' // <=== <=== HERE
'_transient_wc_var_prices_222'    // <=== <=== <=== HERE

What you can try to do is to update expiration date meta_value to an outdated timestamp, this way:

// Set here your product ID
$main_product_id = 222
$transcient_product_meta_key = '_transient_wc_var_prices_'. $main_product_id;
update_option( $transcient_product_meta_key, strtotime("-12 hours") );
wp_cache_delete ( 'alloptions', 'options' ); // Refresh caches

This way you will force the system to rebuild this outdated cached transient.


Additionally you should try to add/update in your parent product ID (the main product where variation are set) these:

// Set here your Main product ID (for example the last variation ID of your product)
$post_id = 22;

// Set here your variation ID (for example the last variation ID of your product)
$variation_id = 24;

// Here your Regular price
$reg_price = 100;
// Here your Sale price
$sale_price = 50;

update_post_meta($post_id, '_min_variation_price', $sale_price);
update_post_meta($post_id, '_max_variation_price', $sale_price);
update_post_meta($post_id, '_min_variation_regular_price', $reg_price);
update_post_meta($post_id, '_max_variation_regular_price', $reg_price);
update_post_meta($post_id, '_min_variation_sale_price', $sale_price);
update_post_meta($post_id, '_max_variation_sale_price', $sale_price); 

update_post_meta($post_id, '_min_price_variation_id', $variation_id);
update_post_meta($post_id, '_max_price_variation_id', $variation_id);
update_post_meta($post_id, '_min_regular_price_variation_id', $variation_id);
update_post_meta($post_id, '_max_regular_price_variation_id', $variation_id);
update_post_meta($post_id, '_min_sale_price_variation_id', $variation_id);
update_post_meta($post_id, '_max_sale_price_variation_id', $variation_id);

// Optionally
wc_delete_product_transients($variation_id);
Community
  • 1
  • 1
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • thanks, but it's not enough. Still not work. Could be setting a parameters in woo tables? – mariobros Jan 09 '17 at 16:13
  • I've tried already to clear WC Transients but doesn't work. If I clear sale price programmatically...product continues to appear discounted (and viceversa, if I set sale price...discount doesn't appear)...seems a cache problem but i didn't use any cache plugin ! – mariobros Jan 10 '17 at 08:46
  • Yes, that's good for me. Furthermore, I've find other solution that work same $product_variable = new WC_Product_Variable($post_id); $product_variable->sync($post_id); wc_delete_product_transients($post_id); I voted your answer ;-) – mariobros Jan 10 '17 at 11:54
  • Ok, I will follow your advice. I update soon. ps: How I vote? – mariobros Jan 10 '17 at 14:37
  • I need your help [here](http://stackoverflow.com/questions/43805478/how-to-update-sale-price-woocommerce3-0-variable-product) – Prafulla Kumar Sahu May 05 '17 at 13:03
  • thank you @LoicTheAztec I got it solved, still I will send you a request on skype may be in future we can work on something . – Prafulla Kumar Sahu May 08 '17 at 06:04
0

If you take a look at the Rest Api Controllers of the latest version of Woocommerce you can see that it is as easy as:

function set_price_to_variation($var_id, $price, $sale) {
    if ( !empty($var_id) ) {
        $variation = wc_get_product($var_id);
        $variation->set_regular_price($price);
        $variation->set_sale_price($sale);
        $variation->save();
    }
    return $variation;
}

Ref: \includes\rest-api\Controllers\Version3\class-wc-rest-product-variations-controller.php

GeorgeP
  • 784
  • 10
  • 26