dongqian3750 2018-08-24 04:56
浏览 97

以编程方式设置产品价格,以便在Woocommerce中随处显示

I have a php cron job that is updating product information from a 3rd party vendor. These changes can be price, sale price, stock, etc.

I have everything running except when I go to put a product on sale via the following code:

 update_post_meta( $product_id, '_sale_price', $sale_price );
 update_post_meta( $product_id, '_price', $sale_price );

the product is updated correctly and shows the sale price in the product on the website and the product admin. When I go to use one of the shortcodes for woocommerce to display products on sale it does NOT pull back what was updated via the above code, ONLY if it was done through the admin panel.

[products limit="24" columns="4" on_sale="true" ]

So my code is not doing something woocommerce does, but I have no idea what it is I am missing. My only fix so far I have found is to remove the sale price in the admin panel->save and then add it back->and save again. Since there are 18,000+ variations on 3,000+ products this is not something that can be done by hand.

As an aside: I had this problem with WP all import too when we imported vis csv file feeds.

Any ideas or thoughts would be helpful.

Thanks!

  • 写回答

1条回答 默认 最新

  • dongya0914 2018-08-24 05:58
    关注

    The best way should be to use CRUD methods introduced with Woocommerce 3… But as it is in a cron job, I am not sure that it will work. Anyway try the following:

    // Get an instance of the WC_Product object
    $product = new WC_Product( $product_id );
    
    // Set product prices
    $product->set_sale_price( $sale_price );
    $product->set_price( $price );
    
    // Save data and update caches
    $product->save();
    

    Now depending on the product type, you might be need to call the product object instance differently…

    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)