I am working on a woocommerce rest api in which I am passing the product data from mongodb using python. I wrote a script for the same. First time the script ran successfully and passed two products but when I tried to pass more products it is failing. I don't know where I am doing it wrong.
Showing this error:
requests.exceptions.ReadTimeout: HTTPConnectionPool(host='localhost', port=80): Read timed out. (read timeout=5)
script:
from woocommerce import API
import os, sys
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(parent_dir)
from portal_v2.settings import _MONGO_DB_URI
from pymongo import MongoClient
import os, csv, ast
wcapi = API(
url="http://localhost/wordpress1",
consumer_key="ck_******************************************",
consumer_secret="cs_******************************************",
wp_api=True,
version="wc/v1"
)
class Products(object):
def __init__(self, dbname):
_client = MongoClient(_MONGO_DB_URI)
self._database = _client[dbname]
self.getCollections()
self.dbname = dbname
def getCollections(self):
self.products = self._database['products']
def getProducts(self):
product_dict = []
for each in self.products.find().limit(10):
product_dict.append({"name": each['name'],"slug": each['name'].replace(' ','').replace('/','') + each['sku'].replace(' ','').replace('/',''),"type": "simple","status": "publish","featured": False,"catalog_visibility": "visible","description": each['description'],"sku": each['sku'],"regular_price": each['mrp'],"sale_price": each['cost_price'],"date_on_sale_from": "","date_on_sale_to": "","purchasable": True,"total_sales": 0,"virtual": False,"downloadable": False,"downloads": [],"download_limit": -1,"download_expiry": -1,"download_type": "standard","external_url": "","button_text": "","tax_status": "taxable","tax_class": "","manage_stock": False,"stock_quantity": None,"in_stock": True,"backorders": "no","backorders_allowed": False,"backordered": False, "sold_individually": False, "weight": each['weight_gms'],"dimensions": {"length": each['length_mm'],"width": each['width_mm'],"height": each['height_mm']},"shipping_required": True,"shipping_taxable": True,"shipping_class": "", "shipping_class_id": 0,"reviews_allowed": True,"average_rating": "0.00","rating_count": 0,"related_ids": [],"upsell_ids": [],"cross_sell_ids": [],"parent_id": 0,"purchase_note": "","categories": [{"id": 9,},{"id": 14,}],"tags": [],"images": [{"src": each['image_url'].replace('dl=0','raw=1'),"name": each['name'],"position": 0}],"attributes": [],"default_attributes": [],"variations": [],"menu_order": 0})
data = {'create':product_dict}
print data
return data
def updateProducts(self, data):
wcapi.post("products/batch", data)
# print wcapi.get("products/?per_page=45").json()
data = Products('xyz').getProducts()
Products('xyz').updateProducts(data)
where 'xyz' is the database name.
To enable the legacy REST API within WooCommerce, go to WooCommerce > Settings > Advanced > Legacy API and tick the Enable the legacy REST API checkbox.
Step 1: Log in to the backend of your WordPress website. Step 2: Hover over “WooComerce”, select “Settings”, and then “Advanced”. Step 3: Toggle the “Legacy API” tab and activate the “Enable the legacy REST API” button. The WooCommerce API is now enabled.
Download the WordPress REST API Basic Auth plugin. Log in to your WordPress Dashboard and go to Plugins -> Add New. Click on the Upload Plugin button and select the plugin's zip file. Go to the Installed Plugins menu and activate the plugin from there.
In order to access your WooCommerce API in Postman, you'll need to first generate an API key. To do this, log into your WooCommerce account and navigate to Settings > Advanced > REST API. From here, you'll be able to create a new API key.
Just increase the timeout option. something like:
wcapi = API(
url="http://localhost/wordpress1",
consumer_key="ck_******************************************",
consumer_secret="cs_******************************************",
wp_api=True,
version="wc/v1",
timeout=10 # the default is 5, increase to whatever works for you.
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With