批量更新WooCommerce变化价格效率

发布于 2025-01-22 04:12:39 字数 425 浏览 0 评论 0原文

我正在尝试从外部来源获取价格,并每天在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喜你已久 2025-01-29 04:12:39

我会看 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.

假装爱人 2025-01-29 04:12:39

我最终做了update_post_meta方法,但我遇到了一个小问题,例如从产品中删除销售时,它仍然显示在我网站上的销售产品中。

我进行了一些搜索,发现这是因为WooCommerce使用了一个名为WC_Product_Meta_Mookup以更快地获取产品数据的表,并且当我更改价格时不会更新,所以Onsale列保持不变。我尝试使用wc_update_product_lookup_tables(),但没有效果。

因此,我通过使用WPDB手动为每个变体更新所述列来修复它,然后在更新所有产品后清除WooCommerce瞬变:

global $wpdb;
$wpdb->update( 'wp_wc_product_meta_lookup', ['onsale' => $is_on_sale], ['product_id'=> $variation_id]); 

delete_transient( 'wc_products_onsale' );

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, the onsale column remains the same. I tried using wc_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:

global $wpdb;
$wpdb->update( 'wp_wc_product_meta_lookup', ['onsale' => $is_on_sale], ['product_id'=> $variation_id]); 

delete_transient( 'wc_products_onsale' );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文