DEV Community

loading...

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

Zaw Htut Win
・1 min read

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.

Discussion (0)