批量更新WooCommerce变化价格效率
我正在尝试从外部来源获取价格,并每天在Woocomceers网站上更新我的产品价格。
我的问题是我有很多变化(150种产品,每种产品都有10-15种变化,总数超过2000种变化),当我尝试更新循环中每个变化的价格时 它需要太多时间,并使用了超过3GB的内存。
我设法通过
update_post_meta($variation_id, '_sale_price', $price)
更改价格而不是更快
$variation->set_sale_price($price)
$variation->save()
的速度来使其更快,但我不确定绕过WooCommerce API并直接更新价格是安全的。
我做得正确吗?如果没有,有更好的方法可以做到这一点?
I'm trying to get prices from an external source and update my product prices in my woocomerce site in a daily basis.
My problem is I have a ton of variations (150 products, each product has 10-15 variations, over 2000 variations in total and growing) and when i try to update the price for each variation in a loop
it takes so much time and uses over 3Gb of memory.
I have managed to make it a lot faster by using
update_post_meta($variation_id, '_sale_price', $price)
for changing the price instead of
$variation->set_sale_price($price)
$variation->save()
but im not sure it is safe to bypass woocommerce api and update prices directly.
Am I doing it correctly? If not is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会看 https://actionscheduler.org/ ,该与WooCommerce一起捆绑在一起。它可以在后台处理较大的任务,并在资源允许的情况下运行工作。
您可以安排重复操作调度程序作业以查询所有产品(使用wc_get_products(), https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-wc_product_query )每小时都说。
然后,当您循环浏览$产品时,您可以为每种简单产品或变化安排一个动作调度程序事件。每个产品更新都将获得一个活动。您可以通过WooCommerce-设置 - > Action Scheduler区域看到操作调度程序事件。
这样,您可以继续使用CRUD方法(例如 - > save()等)
使用操作调度程序需要一些习惯,但是我现在将其用于每项维护任务,以使大量资源拥有大量资源。
I would look at https://actionscheduler.org/ which is bundled along with woocommerce in later versions. It can handle larger tasks in the background and runs the job(s) as resources permit.
You can schedule a repeating action scheduler job to query all the products (using wc_get_products(), https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query) to say every hour.
Then, as you loop through the $products, you can schedule a single action scheduler event for each simple product or variation. Each product update will get an AS event. You can see Action Scheduler events via the WooCommerce->Settings->Action Scheduler area.
This way you can continue to use the CRUD methods (like ->save() etc.)
Using action scheduler takes a bit of getting used to, but I use it now for every maintenance task that hogs lots of resources.
我最终做了
update_post_meta
方法,但我遇到了一个小问题,例如从产品中删除销售时,它仍然显示在我网站上的销售产品中。我进行了一些搜索,发现这是因为WooCommerce使用了一个名为
WC_Product_Meta_Mookup
以更快地获取产品数据的表,并且当我更改价格时不会更新,所以Onsale
列保持不变。我尝试使用wc_update_product_lookup_tables()
,但没有效果。因此,我通过使用WPDB手动为每个变体更新所述列来修复它,然后在更新所有产品后清除WooCommerce瞬变:
I ended up doing the
update_post_meta
method but I ran into minor issues like when I remove sale from a product it still shows up in on sale products in my website.I did some search and I found out it's because woocommerce uses a table called
wc_product_meta_lookup
for getting product data faster, and because its not getting updated when i'm changing prices, theonsale
column remains the same. I tried usingwc_update_product_lookup_tables()
but it had no effect.So I fixed it by updating the said column for each variation manually using wpdb, and then clearing woocommerce transients after I finished updating all products: