skip to Main Content

My aim is to give value to a custom created product attribute with the usage of woocommerce API.

But I do not know how to do it. The official API documentation does not
mention anything about that.

I used to do the create like this:

product_attributes = {
    "attributes": [
        {
            "name" : "color",
            "slug":"color_slug",
            "visible": True,
            "options": [
              "blue",
              "black",
            ]
        }
    ]
    }
wcapi_yachtcharterapp.post("products/attributes", product_attributes).json()

and the update like this:

product_attributes = {
    "attributes": [
        {
            "name" : "color",
            "slug":"color_slug",
            "visible": True,
            "options": [
              "blue",
              "black",
            ]
        }
    ]
    }
wcapi_yachtcharterapp.put("products/attributes/"+str(post_id), product_attributes).json()

but nothing works.

I assume that I have to create first the attribute and then give a value.

2

Answers


  1. You need to add the attribute first and then the attribute terms.You have to do it in different endpoints. This is how I do it :

    import yaml
    from woocommerce import API
    from pprint import pprint
    from collections import ChainMap
    
    
    class WOO_API():
     def __init__(self):
        with open("config.yml", "r") as ymlfile:
            cfg = yaml.full_load(ymlfile)
    
        self.API = API(
        url=cfg['woocommerce']['url'], # Your store URL
        consumer_key=cfg['woocommerce']['consumer_key'], # Your consumer key
        consumer_secret=cfg['woocommerce']['consumer_secret'], # Your consumer secret
        wp_api=True, # Enable the WP REST API integration
        version="wc/v3" # WooCommerce WP REST API version
        )
    
     def retrieve_attributes(self):
        return self.API.get("products/attributes").json()
    
     def add_attribute(self,
                       name,
                       slug,
                       tp="select", 
                       order_by="menu_order", 
                                        has_archives=True):
        data = {
            "name": name,
            "slug": slug,
            "type": tp,
            "order_by": order_by,
            "has_archives": has_archives
        }
        return self.API.post("products/attributes", data).json()
    
     def retrive_attribute_terms(self, id):
        parameters = {
            'per_page': 100
        }
        return self.API.get("products/attributes/"+id+"/terms", params=parameters).json()
    
     def add_attribute_terms(self, name, id):
        data = {
            'name' : name
        }
        return self.API.post("products/attributes/"+id+"/terms", data).json()
    

    ID used in attribute terms endpoint is the attribute id.

    Hope this helps!

    Login or Signup to reply.
  2. //Need to add size and color, and assume below size and color already added in main attribute and now want to add this in perticular product attribute <br/>
    //Size = XXL,XL,L,MN,S,SS <br/>
    //Color = White <br/>
    $size_name = "XXL,XL,L,MN,S,SS"; //need to add size in perticular product <br/>
    $color_name = "White";  //need to add color in perticular product <br/>
    $size_name = explode(",", $size_name);<br/>
    $color_name = explode(",", $color_name);<br/>
    $attr['size'] = "Size";<br/>
    $attr['color'] = "Color";<br/>
    //First need get product attribute and then append new attribute if direct then overwrite <br/>
    $proData = $this->woocommerce->get('products/'.$pid);   //$pid = produc id<br/>
    $addVar = array();<br/>
    foreach ($proData->attributes as $key => $attributes) { <br/>
        if( $attributes->name == $attr['size'] ):<br/>
            foreach( $size_name as $singSize ){<br/>
                if ( ! in_array($singSize, $attributes->options ) ):<br/>
                    //print_r($proData->attributes[$key]);<br/>
                    $proData->attributes[$key]->options[] = $singSize;<br/>
                    $addVar['size'][] = $singSize;<br/>
                endif;//if bracket<br/>
            }//for each bracket<br/>
        endif;//if bracket<br/>
        if( $attributes->name == $attr['color'] ):<br/>
            foreach( $color_name as $singColor ){<br/>
                if ( ! in_array($singColor, $attributes->options ) ):<br/>
                    //print_r($proData->attributes[$key]);<br/>
                    $proData->attributes[$key]->options[] = $singColor;<br/>
                    $addVar['color'][] = $singColor;<br/>
                endif;//if bracket <br/>
            }//for each bracket<br/>
        endif;//if bracket<br/>
    }//for each bracket<br/>
    $data = json_encode($proData->attributes);<br/>
    $data = json_decode($data,true);<br/>
    $finalAttr['attributes'] = $data; <br/>
    $this->woocommerce->post('products/'.$pid, $finalAttr); //add product attribute <br/>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search