如何使用woo Commerce API和python成功创建产品

问题描述 投票:0回答:1

我正在尝试将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主题可能是创建的问题。我更改了它,但问题仍未解决。

我是否缺少某些东西,或者写了一些与用于create的api调用相关的东西?

python django woocommerce woocommerce-rest-api
1个回答
0
投票

打印API响应以查看发生了什么。

Chage:

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()

希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.