DEV Community

Zaw Htut Win
Zaw Htut Win

Posted on

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.

Top comments (1)

Collapse
 
andrewinsidelazarev profile image
Andrew Lazarev

How to rename Attribute, not attribute's value?