python - 如何使用 woo commerce api 和 python 成功创建产品
问题描述
我正在尝试从我的 django 应用程序创建/更新产品到我的网站。
我面临的问题是我无法使用 woo commerce api 从我的 django 创建产品到网站。
更新过程有效。
这是我的代码:
def create_woocommerce_product_individually(wcapi,name,fetched_sku,fetched_url,short_description,description,woo_commerce_category_id):
data = {
"name": name,
"sku": fetched_sku,
"images": [
{
"src": fetched_url
},
],
"short_description": short_description,
"description": description,
"categories": [
{
"id": woo_commerce_category_id
}
],
}
#post data to the woocommerce API
wcapi.post("products",data).json()
print(" 3A STEP - WOO PRODUCT CREATED IN THE SITE")
def update_woocommerce_product_individually(wcapi,name,fetched_sku,fetched_url,short_description,description,woo_commerce_category_id,post_id):
data = {
"name": name,
"sku": fetched_sku,
"images": [
{
"src": fetched_url
},
],
"short_description": short_description,
"description": description,
"categories": [
{
"id": woo_commerce_category_id
}
],
}
#put data to the woocommerce API
wcapi.put("products/"+str(post_id),data).json()
print(" 3B STEP - WOO PRODUCT UPDATED IN THE SITE")
这是代码的一部分,根据响应调用上述函数:
r=wcapi.get("products/?sku="+fetched_sku).json()
if len(r) > 0:
#if it exists in the website , take the post id
post_id=r[0]['id']
if len(r) == 0:
#call the create
create_woocommerce_product_individually(wcapi,name,fetched_sku,fetched_url,short_description,description,woo_commerce_category_id)
product.is_stored_to_website = True
product.save()
print("Stored : {} \n".format(product.is_stored_to_website))
else:
#call the update
update_woocommerce_product_individually(wcapi,name,fetched_sku,fetched_url,short_description,description,woo_commerce_category_id,post_id)
product.is_stored_to_website = True
product.save()
print("Stored : {} \n".format(product.is_stored_to_website))
我在一些论坛上读到,wordpress 主题可能是创建的问题。我改了,问题没有解决。
我是否遗漏了什么或写错了与创建的 api 调用相关的内容?
解决方案
打印 API 响应以查看发生了什么。
查:
wcapi.post("products",data).json()
print(" 3A STEP - WOO PRODUCT CREATED IN THE SITE")
至:
result = wcapi.post("products",data).json()
print(" 3A STEP - WOO PRODUCT CREATED IN THE SITE - {}".format(result))
我就是这样做的:
data = {'categories': [{'id': 56}],
'dimensions': {'height': '', 'length': '1990.0', 'width': '1180.0'},
'images': [],
'name': 'Product Name',
'regular_price': '4769',
'short_description': '<h3>Blbalablabal</h3>\n'
'<ul>\n'
'\t<li><strong>caracteristic1: </strong>12.5 mm</li>\n'
'\t<li><strong>caracteristic3: </strong>1.99 x 1.18 m</li>\n'
'\t<li><strong>caracteristic3: </strong>Circular '
'total</li>\n'
'</ul>\n',
'sku': '56565',
'slug': 'product-name',
'status': 'publish',
'type': 'simple',
'weight': ''}
然后添加产品:
def add_product(self, data):
return self.API.post("products", data).json()
希望这可以帮助!
推荐阅读
- vue.js - VueJS 和 axios:不能在 axios 响应中使用 url 在新窗口中打开
- javascript - 如何通过计算任何数组项在 mongodb 聚合中添加新字段
- react-native - 如何在`react-native init`之后设置`react-native-web`
- arrays - 如果加载数据后出现for循环,如何附加两个numpy数组?
- r - geom_smooth(): 一行,不同颜色
- php - Laravel Eloquent 嵌套组的关系
- ssh - 如何从 Ubuntu 虚拟机 ssh 到与主机 PC 位于同一 LAN 中的机器?
- java - 在方解石中如何用另一个 sqlNode 修改生成的 SQLNode?
- mysql - 从 SQL 查询问题下载 CSV
- javascript - 在 javascript 中定位名称属性时,smoothscroll 不起作用