69058

Add sale price programmatically to product variations

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 <img src=https://www.e-learn.cn/content/wangluowenzhang/"https://i.stack.imgur.com/Exs9d.jpg" alt="enter image description here">

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

<img src=https://www.e-learn.cn/content/wangluowenzhang/"https://i.stack.imgur.com/X3Y1I.jpg" alt="enter image description here">

Answer1:

<strong>Update: Prices are also cached in transient wp_options table.</strong>

Let say your product ID is <strong>222</strong>, 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 <strong>meta_value</strong> 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.

<hr>

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);

Answer2:

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);

Recommend

  • Java counting method? [closed]
  • Custom stock options doesn't show up in bulk edit (Woocommerce 3.2.1)
  • Cannot update Stock Item Quantity for a Product in Magento 1.6.2
  • Adding Product Programmatically in woocommerce
  • How to define “global” find conditions for model in CakePHP?
  • SQL query to include specific WooCommerce tag
  • combining one observable with latest from another observable
  • In SQL How is it possible to use the Sum Function in an Update
  • How to code MVC Web Api Post method for file upload
  • EXCEL Multiple Ranges - need different answers for each range
  • Using hibernate 2nd level cache or query cache for lazy fetch queries
  • passing parameter from aspx to javascript issue
  • XMLHttpRequest in IE-7
  • Transaction coordinator for wcf distributed transactions
  • how to handle onLoad Event on img tag and others
  • Memory usage differs greatly (and strangely) between frontend and backend
  • How to distinguish field that requires null=True when blank=True is set in Django models?
  • Find substring in string using locale
  • Teradata Database 3130 Response limit exceeded?
  • Majority function in SQL
  • Deploy same Javascript webapp build to different environments
  • Show records ordered with maximum price first in PHP & MySQL
  • PWA with multiple pages
  • Invalid access key error using credentials redeemed from an amazon open id token
  • retrieve vertices with no linked edge in arangodb
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Understanding cpu registers
  • How do I configure my settings file to work with unit tests?
  • Turn off referential integrity in Derby? is it possible?
  • Add sale price programmatically to product variations
  • Unable to use reactive element in my shiny app
  • How do I use LINQ to get all the Items that have a particular SubItem?