Skip to content
Advertisement

Woocommerce Change product price by ID

I have an array with product id (post_id). My goal is to loop thru this array and change all the product prices in WooCommerce products.

Here is my attempt:

foreach ($array as $product) {
    update_post_meta($product['post_id'], '_regular_price', $array['price']);
}

Array

[
  {
    "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"
  }
]

Advertisement

Answer

$array['price'] will return undefined change it to $product['price']

foreach ($array as $product) {
  update_post_meta($product['post_id'], '_regular_price', $product['price']);
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement