Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Woocommerce根据ID更改产品价格_Php_Wordpress_Woocommerce - Fatal编程技术网

Php Woocommerce根据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

我有一个带有产品id的数组(
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']);
}