我有外部 REST API,我从中构建一个如下所示的数组:
$arr = array(
1 => array('code' => '0100686', 'qty' => '2', 'price' => '65.22'),
2 => array('code' => '0100687', 'qty' => '1', 'price' => '5.23'),
3 => array('code' => '0100688', 'qty' => '8', 'price' => '0.28')
);
之后,我需要更新 WooCommerce 中产品的价格和数量。 (上面的数组代码是WC中的SKU)。
之后我的代码如下:
foreach ($arr as $single) {
$product_id = wc_get_product_id_by_sku($single['code']);
// I need here to update the product price
// I need here to update the product in stock
}
我搜索了一下,有很多直接通过 SQL 查询或使用一些钩子的解决方案,他们说我应该进行瞬时清理等等......我无法想出一个最佳解决方案。您能帮助我完成这项任务的最佳解决方案是什么吗?
你可以尝试这样的方法:
$arr = array(
array( 'code' => '0100686', 'qty' => '2', 'price' => '65.22' ),
array( 'code' => '0100687', 'qty' => '1', 'price' => '5.23' ),
array( 'code' => '0100688', 'qty' => '8', 'price' => '0.28' )
);
foreach ( $arr as $single ) {
$product_id = wc_get_product_id_by_sku( $single['code'] );
if ( ! $product_id ) {
continue;
}
$product = new WC_Product( $product_id );
if ( ! $product ) {
continue;
}
$product->set_price( $single['price'] );
$product->set_stock_quantity( $single['qty'] );
$product->save();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)