Php 在Woocommerce 3中更新和显示更新的产品价格

Php 在Woocommerce 3中更新和显示更新的产品价格,php,wordpress,methods,woocommerce,product,Php,Wordpress,Methods,Woocommerce,Product,我试图用CRON以编程方式更新woocmerce价格 我正在将新价格保存为_价格、_常规价格和_销售价格 post meta已正确保存。但是,不知何故,价格没有更新 这是一段代码: 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

我试图用CRON以编程方式更新woocmerce价格

我正在将新价格保存为_价格、_常规价格和_销售价格

post meta已正确保存。但是,不知何故,价格没有更新

这是一段代码:

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();
}
此函数可正确保存post meta,但woocommerce不会对产品使用这些值

有人知道为什么会这样吗

谢谢

您应该改用Woocommerce 3推出的新版本:

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();
}
现在它应该可以工作了,并在前端显示更新的价格