Ad

How To Get All Product Id From Shopify Python API

- 1 answer

I have created one private shopify app. Which can get almost all the information with product id. But I need one option to get product id of all products in a shop with API. I have tried the option below

shopify.Product.find()

but it shows only first 50 products. But my shop has more than 2.4k products.

Ad

Answer

Update as of 2019-07: This will no longer work as it has been deprecated >Update as of 2019-07: This will no longer work as it has been deprecated and subsequently removed from the Shopify API.

The replacement is detailed in this answer.

Original answer below


Shopify returns paginated responses for lists of resources. The default number of resources per page is 50, and the default page is 1. Your request is thus equivalent to the following:

shopify.Product.find(limit=50, page=1)

Shopify allows you to increase the limit per page to 250. Here's a helper function I use to get all of a given resource:

def get_all_resources(resource, **kwargs):
    resource_count = resource.count(**kwargs)
    resources = []
    if resource_count > 0:
        for page in range(1, ((resource_count-1) // 250) + 2):
            kwargs.update({"limit" : 250, "page" : page})
            resources.extend(resource.find(**kwargs))
    return resources

You use it like this:

products = get_all_resources(shopify.Product)

You can even pass in parameters. Your question asks specifically for the product ID- if you limit the query to only return the IDs, this will be much, much faster (as it doesn't have to pull in any of the product variants):

product_ids = get_all_resources(shopify.Product, fields="id")

Note that if you have 2.4k products, this may take some time!

Documentation: https://help.shopify.com/api/reference/product

Ad
source: stackoverflow.com
Ad