F
F
FRC_Volen2019-04-05 18:00:52
WooCommerce
FRC_Volen, 2019-04-05 18:00:52

How to update price for Woocommerce variable product?

Hello,
I can't update prices for a variable product from my meta box
, I use the following code to update prices for a product and its variations

update_post_meta( $post->ID, '_regular_price', (float)$meta_value  );
update_post_meta( $post->ID, '_price', (float)$meta_value  );

But the old price is shown in the store and in the product card,
5ca76cfd9b55b478861919.jpeg
and the new price is already in the basket
5ca76d11e673e845514709.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pychev Anatoly, 2019-04-05
@FRC_Volen

Woocomerce actively uses transients. This is a temporary WordPress cache.
For your changes to take effect immediately, you need to delete the transient for this product

// очищаем кэш
  wc_delete_product_transients($product_id);

I also somehow searched and found it in the source code, somewhere very deep.
But to be honest, your approach is wrong, because. Woocommerce, when changing the price of a variation, also corrects the meta field of the product itself. And you work bypassing woo, directly with the database. Unfortunately, I won’t share the correct code, I didn’t get around to rewriting it, look it up on the net. I think we need to work with the WC_Product class and its derivatives.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question