Php Woocommerce根据ID更改产品价格
我有一个带有产品id的数组(Php Woocommerce根据ID更改产品价格,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我有一个带有产品id的数组(post\u id)。我的目标是循环通过这个阵列,改变WooCommerce产品中的所有产品价格 以下是我的尝试: foreach ($array as $product) { update_post_meta($product['post_id'], '_regular_price', $array['price']); } 阵列 [ { "post_id": 18, "sku": "S
post\u id
)。我的目标是循环通过这个阵列,改变WooCommerce产品中的所有产品价格
以下是我的尝试:
foreach ($array as $product) {
update_post_meta($product['post_id'], '_regular_price', $array['price']);
}
阵列
[
{
"post_id": 18,
"sku": "SNTP-UVS8P",
"price": "59.00"
},
{
"post_id": 17,
"sku": "TD-SKU30548",
"price": "10.00"
},
{
"post_id": 16,
"sku": "USBCV",
"price": "29.00"
}
]
一种方法是使用
wc\u get\u product(ID)
,然后在产品对象上使用set\u price
来设置价格
样本:
foreach ($products as $product) {
$wc_product = wc_get_product( $product['id'] );
$product->set_price( $product['price'] );
$product->save();
}
$array['price']
将返回未定义的值,并将其更改为$product['price']
foreach ($array as $product) {
update_post_meta($product['post_id'], '_regular_price', $product['price']);
}