set_sale_price in WooCommerce

I need to set a WooCommerce sales price in a function. I’m using set_sale_price – but I don’t think it is working.

I’m trying it two different ways:
Method one:

update_post_meta( $theID, ‘_sale_price’, $theFinalSalePrice );

and Method two:

$editedProduct = wc_get_product( $theID );
$editedProduct->set_sale_price($theFinalSalePrice);

Method One does update the sales price – however, from what I understand, using this method prevents the product from “clearing the cache” and therefore it does not appear in the shortcode which should show on_sale products.

Method Two should be the correct method which will cause that product to appear in the shortcode output – however it fails to actually set the _sale_price.

So, if I use method one, and I create a manual loop that shows sale items, then the product will show up in the loop output, but not in the output of the products shortcode (with on_sale set to true). If I use method two, it shows up in neither, because the sale price was never set.

I can only assume that I am misunderstanding how to properly use the set_sale_price method. I’m hoping someone will recognize my error. Thanks for the help.

Read more here:: set_sale_price in WooCommerce

Leave a Reply

Your email address will not be published. Required fields are marked *