Python 如何使用WooCommerceAPI为产品属性赋予价值?
我的目标是使用WooCommerceAPI为自定义创建的产品属性赋予价值 但我不知道怎么做。事实并非如此 关于那件事,我什么都不提 我过去常常这样做:Python 如何使用WooCommerceAPI为产品属性赋予价值?,python,woocommerce,woocommerce-rest-api,Python,Woocommerce,Woocommerce Rest Api,我的目标是使用WooCommerceAPI为自定义创建的产品属性赋予价值 但我不知道怎么做。事实并非如此 关于那件事,我什么都不提 我过去常常这样做: product_attributes = { "attributes": [ { "name" : "color", "slug":"color_slug",
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()
更新如下:
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()
但什么都不管用
我假设我必须先创建属性,然后给出一个值。您需要先添加属性,然后添加属性项。您必须在不同的端点中进行添加。我就是这样做的:
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
希望这有帮助 //需要添加大小和颜色,并假设以下大小和颜色已添加到主属性中,现在希望将其添加到特定的产品属性中
//尺寸=XXL、XL、L、MN、S、SS
//颜色=白色
$size_name=“XXL、XL、L、MN、S、SS”//需要在特定产品中添加尺寸
$color\u name=“白色”//需要在特定产品中添加颜色
$size\u name=分解(“,”,$size\u name)
$color\u name=分解(“,”,$color\u name)
$attr['size']=“size”
$attr['color']=“color”
//首先需要获取产品属性,然后追加新属性(如果直接),然后覆盖
$proData=$this->woocommerce->get('products/'。$pid)//$pid=产品id
$addVar=array()
foreach($proData->attributes as$key=>$attributes){
如果($attributes->name==$attr['size']):
foreach($size\u名称为$singSize){
如果(!in_数组($singSize,$attributes->options)):
//打印($proData->attributes[$key]);
$proData->attributes[$key]->options[]=$singSize;
$addVar['size'][]=$singSize;
endif;//如果括号
}//对于每个括号
endif;//如果括号
如果($attributes->name==$attr['color']):
foreach($color\u名称为$singColor){
如果(!in_数组($singColor,$attributes->options)):
//打印($proData->attributes[$key]);
$proData->attributes[$key]->options[]=$singColor;
$addVar['color'][=$singColor;
endif;//如果括号
}//对于每个括号
endif;//如果括号
}//对于每个括号
$data=json_encode($proData->attributes)
$data=json_decode($data,true)
$finalAttr['attributes']=$data
$this->woocommerce->post('products/'。$pid,$finalAttr)//添加产品属性