15

How do I add value to a woocommerce attribute through code? I have created an attribute called 'Dispatch time' (taxonomy: pa_dispatch) and now I want to add value to a particular product's Dispatch attribute.

How to do this programmatically?

enter image description here

Nagendra Rao
  • 7,016
  • 5
  • 54
  • 92

2 Answers2

15

I found the answer, you need to use wp_set_object_terms to set the terms of object of a taxonomy,

wp_set_object_terms( $object_id, $terms, $taxonomy, $append);

Where, $append can be true or false, if true, the tag will be appended to existing tag, if false, the tag is replaced.

In my example,

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

Here, the pa_dispatch is the woo-commerce taxonomy.

Nagendra Rao
  • 7,016
  • 5
  • 54
  • 92
  • 1
    thank you. where in your functions.php did you put this? and what is $object_id? – nicmare Nov 27 '14 at 12:57
  • @nicmare Put it where you want to change the value, but if you put this statement in functions.php directly, then the function will run every time any file loads! Also, the $object_id is nothing but the product's id, ie., post ID of that particular product. – Nagendra Rao Nov 27 '14 at 13:56
  • of course i would fire it with save_post hook. i will give it a try. thank you – nicmare Nov 27 '14 at 17:02
  • it is not working for me, $product->get_attribute('my_attr'); return 'val1 | val2' but wp_set_object_terms( $product->ID, 'val3', 'my_attr' , false); returns invalid_taxomony. ideas? – user2867106 Feb 02 '15 at 01:33
  • @user2867106 Is your 'my_attr' a woocommerce taxonomy? how did you create 'my_attr'? – Nagendra Rao Feb 02 '15 at 13:37
  • @Rao `wp_set_object_terms` doesn't update the text fields, do you have any idea how to do this? – Faisal Ashfaq Sep 04 '15 at 08:12
4

You cannot add value to an attribute. You need to make the product variable, create a variation, and assign it with the attribute. Now in this variation you can assign a value.

Attributes Configuration

Variations Configuration

Read mode:

  1. http://docs.woothemes.com/document/product-variations/
  2. http://www.youtube.com/watch?v=7PX8MWBOAeo

EDIT:

After more clarification on the question, here is an updated solution.

Add the below function to your functions.php. Call it on the appropriate hook and pass the product ID as well as the attribute values.

function se19519561_set_attributes($post_id, $attributes) {

    //Type attribute
    $product_attributes['type'] = array(
        //Make sure the 'name' is same as you have the attribute
        'name' => htmlspecialchars(stripslashes('Dispatch Time')),
        'value' => $attributes,
        'position' => 1,
        'is_visible' => 1,
        'is_variation' => 1,
        'is_taxonomy' => 0
    );

//Add as post meta
update_post_meta($post_id, '_product_attributes', $product_attributes);

}

Hope this helps!

Sameer Joshi
  • 877
  • 2
  • 9
  • 18
  • no, that's not what I am trying to achieve, I am able to add the value through GUI, but I want to do this programmatically in php. – Nagendra Rao Oct 22 '13 at 13:45
  • ie., I want to add the value, '3 business days' to a particular product's 'Dispatch time' attribute through php function. – Nagendra Rao Oct 22 '13 at 13:47
  • 1
    Do you want to add the attribute on the fly when the product page is accessed or do you want to add this to all the products at once? – Sameer Joshi Oct 22 '13 at 13:51
  • I have a set of product post ids, for which I want to add the dispatch time values, I don't need to create the attribute, its already created. I just want to add values to those attributes for some products. – Nagendra Rao Oct 22 '13 at 13:54
  • Hi, posted an edit into the answer with a function. Hope this helps. – Sameer Joshi Oct 23 '13 at 13:04