如何使用WooCommerceAPI为产品属性赋予价值?

2024-10-01 15:39:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我的目标是使用WooCommerceAPI为自定义创建的产品属性赋予价值

但我不知道怎么做。official API documentation没有 关于那件事,我什么都不提

我过去常常这样做:

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

更新如下:

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

但什么都不管用

我假设我必须首先创建属性,然后给出一个值


Tags: nametrue属性blueproductpostattributescolor
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/>

您需要先添加属性,然后添加属性项。您必须在不同的端点中进行添加。我就是这样做的:

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是属性ID

希望这有帮助

相关问题 更多 >

    热门问题