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:
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.
Edit: I have found two pieces of info that might help me solve this: In the wc_product_meta_lookup table, there is a value "onsale" which was set to 0 in a product which did have a sale price, but did not show up in the sale listings. That value was set to 1 in a different product which did have a sale price and did show up in the sale listings. So, I set it to 1 in the first product, to indicate, I assume, that it is "onsale."
This did not result in the product showing up in the sale listing. But - after more searching, I found that if I
delete_transient( ‘wc_products_onsale’ ); at the beginning of my function, that this newly edited product does show up in sale listings.
So, it seems like I can solve the problem by setting the onsale value for each newly onsale product to 1, and then
delete_transient( ‘wc_products_onsale’ ); to clear the transient of the onsale products.
My only last lost bit is that I don't know how to set that value.