DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป is a community of 966,155 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
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 (0)

๐Ÿ‘‹ Have You Posted on DEV Yet?

Head over to our Welcome Thread and tell us a bit about yourself!