一、安装WooCommerce
(一)安装
在wordpess后台插件那里,直接搜索“woocommerce ”,然后安装即可。

不过要注意版本的兼容问题,比如我用的是wordpress 4.9,在后台安装就不成功,后来安装了WooCommerce 4.3才成功,但是运行时又报错了。
后来升级了xampp,然后安装最新版本的wordpress与WooCommerce,在后台安装的时候还是failed,没办法,最后只能下载了WooCommerce ,放到本地的wordpress插件目录下面。
(二)设定WooCommerce选项
激活WooCommerce之后,自动跳出一些选项,需要填写。

根据自己的实际情况填写即可。
(三)主题
在wordpress后台,直接搜索“Woocommerce”,可以搜索到许多优秀的免费主题。
(四)首页
这时访问网站的首页仍然是博客文章的形式,只有访问www.xx.com/shop才是商店。
如果要将首页设为商店的形式,点击“外观—自定义”,“主页设置”,你的主页显示为“一个静态页面”,选择“商店”即可,点击保存,接着你可以回到网址首页xxx.com,店铺立马就出现了,而不是之前的xxx.com/shop了
二、设定WooCommerce API

三、更改货币符号

四、发布产品
(一)安装WooCommerce
pip install woocommerce
(二)成果展示

(三)代码
from woocommerce import API
wcapi = API(
url="http://localhost/wp02/",
consumer_key="ck_5a0c4cb7f30d04b9d700eb7c3323714faace7716",
consumer_secret="cs_4e70f6c54cf96ad7f504a32af488bcae407f9a18",
version="wc/v3"
)
data = {'categories': [{'id': 56}],
'dimensions': {'height': '', 'length': '1990.0', 'width': '1180.0'},
'images': [
{
"src": "http://localhost/wp02/wp-content/uploads/2021/03/002-03.jpg"
},
{
"src": "http://localhost/wp02/wp-content/uploads/2021/03/002.jpg"
}
],
'name': 'Product Name24',
'regular_price': '47.69',
'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': '5656530',
'slug': 'product-name',
'status': 'publish',
'type': 'simple',
'weight': ''}
def add_product(data):
return wcapi.post("products", data).json()
add_product(data)
小提示:
发布产品的时候,sku一定不能相同,如果相同,也没有出错信息,只是发布不了产品,我在这个bug上被折腾了很久。
参考:
https://pypi.org/project/WooCommerce/
http://woocommerce.github.io/woocommerce-rest-api-docs/?python#create-a-product
https://www.youtube.com/watch?v=Vq6rAVfqK3k
(四)代码二
注意事项:
1.传id时要传入[{'id': 56}].
2.本地测试时图片地址要本地的才行,网上的不行。
from woocommerce import API
wcapi = API(
url="http://localhost/wp02/",
consumer_key="ck_5a0c4cb7f30d04b9d700eb7c3323714faace7716",
consumer_secret="cs_4e70f6c54cf96ad7f504a32af488bcae407f9a18",
version="wc/v3"
)
#https://stackoverflow.com/questions/60390113/how-to-create-a-product-successfully-using-woo-commerce-api-with-python
def create_woocommerce_product_individually(wcapi,name,fetched_sku,fetched_url,short_description,description,woo_commerce_category_id):
data = {
'categories': woo_commerce_category_id,
'dimensions': {'height': '', 'length': '1990.0', 'width': '1180.0'},
'images': [
{
"src": fetched_url
},
{
"src": fetched_url
}
],
'name': name,
'regular_price': '47.69',
'short_description': short_description,
"description": description,
'sku': fetched_sku,
'slug': 'product-name',
'status': 'publish',
'type': 'simple',
'weight': ''
}
#post data to the woocommerce API
wcapi.post("products",data).json()
print(" 3A STEP - WOO PRODUCT CREATED IN THE SITE")
create_woocommerce_product_individually(wcapi,"product 002","3332222415","http://localhost/wp02/wp-content/uploads/2021/03/08.jpg","short desc","desc",[{'id': 56}])
四、关于报错
如果报以下错误:
requests.exceptions.ReadTimeout: HTTPConnectionPool(host='localhost', port=80): Read timed out. (read timeout=5)
解决方法:
Set the timeout around 20 seconds in the API() constructor and it should work for you then.
虽然有所改善,可以多发布几个商品,可是仍然不满足需求。
参考:https://github.com/woocommerce/wc-api-python/issues/61
https://pypi.org/project/WooCommerce/