【问题标题】:Update and display updated product prices in Woocommerce 3在 Woocommerce 3 中更新并显示更新的产品价格
【发布时间】:2018-11-27 06:07:04
【问题描述】:

我正在尝试使用 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 不会将这些值用于产品。

有人知道为什么会这样吗?

谢谢

【问题讨论】:

    标签: php wordpress methods woocommerce product


    【解决方案1】:

    您应该改用 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

    【讨论】:

      猜你喜欢
      • 2019-03-29
      • 2020-10-29
      • 2021-08-01
      • 2018-04-26
      • 1970-01-01
      • 2017-09-01
      • 2017-05-12
      • 2018-01-31
      • 1970-01-01
      相关资源
      最近更新 更多