Iam trying to update woocommerce prices programmatically with with CRON.
I'm saving the new price to the _price, _regular_price and _sale_price post meta.
The post meta are correcty saved. But, somehow, the price is not updated.
This is a piece of the code:
function save_prices($post_ID){
$woo_prod = wc_get_product( $post_ID );
$price = some_function_to_get_the_price( $post_ID );
if($woo_prod->is_type( 'simple' )){
$woo_prod->update_meta_data( '_regular_price', $price );
$woo_prod->update_meta_data( '_sale_price', $price );
$woo_prod->update_meta_data( '_price', $price );
}
$woo_prod->save();
}
This function saves the post meta correctly, but woocommerce doesn't use these values for the product.
Anyone knows why this is happening?
Thanks
答案 0 :(得分:0)
您应该使用Woocommerce 3中引入的新CRUD getters and setters methods:
function save_prices($post_ID){
// Get an instance of the WC_Product object
$product = wc_get_product( $post_ID );
$price = some_function_to_get_the_price( $post_ID );
if($product->is_type( 'simple' )){
$product->set_regular_price($price);
$product->set_sale_price($price);
$product->set_price($price);
}
$product->save();
}
现在它应该工作并在前端显示更新的价格。
请参阅related documentation for available WC_Product
CRUD methods