Updating product attributes in WooCommence is a painful experience, right?

zawhtutwin

Zaw Htut Win

Posted on June 5, 2021

Updating product attributes in WooCommence is a painful experience, right?

In this section,let me share how to update the product attributes programmatically in wordpress.

Let's say we have a product attribute call "markup". We have to write like this in order to get the attribute correctly.

$markup_value = get_post_meta($product->post_id, '_product_attributes',true)['markup']['value'];
Enter fullscreen mode Exit fullscreen mode

In order to save the attribute again, quite tricky but still managable.

$post_id = $product->post_id;
$product_attr = get_post_meta(post_id, '_product_attributes',true);//get the whole product attributes first

$product_attr['markup']['value'] = 50.75;//your desired attribute value

update_post_meta(post_id , '_product_attributes',$product_attr);
Enter fullscreen mode Exit fullscreen mode

So, that's it. If you have a list of post id, and you want to update the product attribute(in our case markup), that's how you are going to do it.

💖 💪 🙅 🚩
zawhtutwin
Zaw Htut Win

Posted on June 5, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related