I'm writing a script for an e-commerce with Woocommerce who read a csv and set new price/sale price on product. The variable below are an example.
With this code I'm able to set price and sale price on product, but when the sale data is expired, the price don't return to regular price.
$price = '40';
$sale_price = array();
$sale_price[1] = '20';
$sale_price[2] = '2020-02-10';
$sale_price[3] = '2020-02-11';
update_post_meta( $product_id, '_price', $price );
update_post_meta( $product_id, '_regular_price', $price);
if( !empty($sale_price) ){
update_post_meta( $product_id, '_price', $sale_price[1] );
update_post_meta( $product_id, '_sale_price', $sale_price[1] );
update_post_meta( $product_id, '_sale_price_dates_from', $sale_price[2] );
update_post_meta( $product_id, '_sale_price_dates_to', $sale_price[3] );
}
if I don't set the '_price' post_meta I can't see the regular price or the sale price.
_sale_price_dates_from
&_sale_price_dates_to
dates because that must betimestamp
not direct date so update withtimestamp
and check that – Krunal Prajapati