Search code examples
pythonwordpresswoocommercewoocommerce-rest-api

woocommerce rest api python get products


What I need is to get all product IDs that are 'instock' using python. It looks like this:

wcapi.get('products', params={"per_page": 100, 'stock_status': 'instock', 'tag': '1111'}).json()

This code works, but have max limit 100 per_page, I don't understand how can I acquire rest of them (about 2k items in stock) and moreover this request returns all info about products, but I need only IDs.


Solution

  • Following the docs, I see that you can pass a page parameter also.

    That makes it pretty straightforward to iterate all pages:

    page = 0
    While True:
      products = wcapi.get('products', params={'per_page': 100, 'stock_status': 'instock', 'tag': '1111', 'page': page}).json()
      # retrieve ids from **products**
      if len(products) == 0: # no more products
        break
      page = page + 1
    

    Edit: Bear in mind that this is going to process per page (in this case 100) products per loop.