Iam尝试使用CRON以编程方式更新woocommerce价格。
我将新价格保存到_price,_regular_price和_sale_price发布元数据中。
帖子元已正确保存。但是,价格却没有更新。
这是一段代码:
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();
}
此功能可正确保存帖子元,但woocommerce不会将这些值用于产品。
有人知道为什么会这样吗?
谢谢
您应该改用Woocommerce 3引入的新的CRUD getter和setter方法:
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();
}
现在它应该可以工作并在前端显示更新的价格。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句