Adding Custom Product Attribute and Variable in WooCommerce Programatically

can you please let me know how I can add Custom Product Attribute and Variable in WooCommerce Programatically? I know this easy to be done in Woocommerce GUI but I need to do this through functions.php

I have a Attribute Called “Class” and Some Variables as [A,B,C,D] now I would like to add them to the Cart and Shop page, I alraedy tried this function but it didn’t add any thing to the page:

Read More
function woocommerce_variable_add_to_cart() {
   //Type attribute
    $product_attributes['type'] = array(
        //Make sure the 'name' is same as you have the attribute
        'name' => htmlspecialchars(stripslashes('Class')),
        '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);

}

Thanks

Related posts

Leave a Reply

1 comment

  1. I just noticed that your function above refers a “$attributes” which doesn’t look like it’s defined in that function. Hopefully your working copy of that function fixed that otherwise value => NULL which probably is why your update failed.

    On the other hand I think your $product_attributes is badly defined (I’m not 100% sure, though).

    I created a custom attribute called “my_attr_name” with the value “my_custom_value” and checked the MySQL database to see how it is stored. It is stored inside table “wp_postmeta” with meta_key=_product_attributes and the “meta_value” serialized. So I copied that serialized value and tried to unserialize it just to see how it should be defined in the first place. It looks like yours except that your attribute key (ie. ‘type’) and the value for the first key of the subarray should match.

    This is my example:

    Array
    (
        [john-doe] => Array
            (
                [name] => 'John Doe'
                [value] => my_custom_value
                [position] => 0
                [is_visible] => 1
                [is_variation] => 0
                [is_taxonomy] => 0
            )
    
    )
    

    As you see the array’s key john-doe ~ first key of the subarray (which is also John Doe). In your case:

    type <> htmlspecialchars(stripslashes('Class'))
    

    It worth mentioning that if your ‘name’ => ‘John Doe’ then the master key should be something like ‘john-doe’ (lower-case and minus as space replacement). I don’t know if this is a must but at least this is the way WP 4.2.2 works.

    As for the other keys:

    • is_visible: if the attribute is visible on the product page
    • position : the order of attribute (0=first,1=next attribute,etc); I think this one should be always defined otherwise you risk to overwrite the first (0) index; so you should find out how many they are before adding a new one (see $meta=get_post_meta (get_the_ID(), ‘_product_attributes’))
    • is_variation: probably smth. to do with variable product (?!)
    • is_taxonomy: I think is should be always false as the meta_key is not regarded as taxonomy (just a guess)