在同步产品库存数量中,如何加快WooCommerce REST API的速度?
我想要与站点A的同步库存数量B(远程站点),即。当站点A中的订单处于过程状态时,将更新站点B中同一产品的数量。
对于映射产品ID,我在站点A和B中添加了Meta。
为了这样做,我使用以下代码。它有效,但是我的问题是速度过程,当库存数量希望在网站B中更新时,需要更多秒。
add_action('woocommerce_order_status_changed', 'update_base_site_inventory',11, 1);
function update_base_site_inventory($order_id){
$order = wc_get_order( $order_id );
if ($order->status == 'processing') {
$items = $order->get_items();
foreach($items as $item){
$product = wc_get_product($item->get_product_id());
$product_quantity = $product->get_stock_quantity();
$id_mapped = get_post_meta($item->get_product_id(), 'map_product_id', true);
$batch_update['update'] = array(array('id' => current($id_mapped), 'stock_quantity' => $product_quantity));
}
$api_response = wp_remote_request(
'https://...../wp-json/wc/v3/products/batch/', array(
'method' => 'POST',
'headers' => array('Authorization' => 'Basic ' . base64_encode( '....:.....' )),
'body' => $batch_update
)
);
}
}
我的方法正确吗?我应该怎么办?有什么方法可以提高API请求速度吗? 谢谢,
I want sync stock quantity of site B (remote site) with site A, ie. when an order in site A is in process status, the quantity of same product in site B will be updated.
For mapping product IDs, I add post meta in both site A and B.
In order to do so, I use following codes. It works, but my problem is speed process when stock quantity wants to be updated in site B, it takes more seconds.
add_action('woocommerce_order_status_changed', 'update_base_site_inventory',11, 1);
function update_base_site_inventory($order_id){
$order = wc_get_order( $order_id );
if ($order->status == 'processing') {
$items = $order->get_items();
foreach($items as $item){
$product = wc_get_product($item->get_product_id());
$product_quantity = $product->get_stock_quantity();
$id_mapped = get_post_meta($item->get_product_id(), 'map_product_id', true);
$batch_update['update'] = array(array('id' => current($id_mapped), 'stock_quantity' => $product_quantity));
}
$api_response = wp_remote_request(
'https://...../wp-json/wc/v3/products/batch/', array(
'method' => 'POST',
'headers' => array('Authorization' => 'Basic ' . base64_encode( '....:.....' )),
'body' => $batch_update
)
);
}
}
is my approach correct? what should I do? is there any way to increase the API request speed?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用WooCommerce客户端库,使用Composer安装:
然后在您的代码中使用它:
Try using WooCommerce Client Library, install using composer:
Then use it in your code like this: