Add attribute / custom attribute to product in woocommerce

I have products from some API and want to insert these products. I was able to insert products and use already existing attributes ( or added by UI ).

Adding new term (My Color) to the existing attribute (Color) :

// Add term to the attribute
wp_set_object_terms( $post_id, "My Color", 'pa_color' , true );

Use the added attribute with current product :

// Data for the term to be used
$theData = array(
                 'pa_color'=>
                    array( 
                       'name'=>'pa_color', 
                       'value'='My Color',
                       'is_visible' => '1',
                       'is_variation' => '1',
                       'is_taxonomy' => '1'
                    )
            );

// Add this attribute to this product
update_post_meta( $post_id,'_product_attributes',$theData);

How can I add new attribute and use it with the current product, for ex :

RAM : 4GB

I have tried this :

register_taxonomy(
    'pa_ram',
    'product',
    array(
            'label' => __( 'RAM' ),
            'rewrite' => array( 'slug' => 'size' ),
            'hierarchical' => true,
        )
    );
}

Here is the URL of the attributes I can see/add in UI :

wp-admin/edit.php?post_type=product&page=product_attributes

enter image description here

0

Leave a Comment