How to add value to a product attribute in WooCommerce?

I am looking for a way to programmatically add a value to an attribute in WooCommerce. Any tips?

Kirk Lynch Asked on November 21, 2015 in WordPress.
Add Comment
2 Answer(s)
Best answer

Lets say you have created the attribute ‘Dispatch Time’ and assigned it the value ‘3 business days’ as seen on the screenshot below:

RE: How to add value to a product attribute in WooCommerce?

You can use the wp_set_object_terms() function to add or change the value of an attribute


<?php wp_set_object_terms( $object_id, $terms, $taxonomy, $append ); ?>

In the context of the scenario above, we will set it up like this:

wp_set_object_terms( $object_id, ‘2 Business Days’, ‘pa_dispatch’ , false);

where the @object_id is the post ID of the product, the ‘pa_dispatch’ is the Woocommerce taxonomy and ‘2 Business Days’ is the attribute value. The $append can be either true or false with the following meaning:
– True will add the value to the attribute
– False will change the value of the attribute

Geraldine Stevens Answered on November 21, 2015.
Add Comment

Woo commerce it is new concept everybody should not full of awareness but it is good for us all of people shopping online easy to buy woo commerce

alinamike Answered on January 17, 2017.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.