PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 1, 2022

[FIXED] How to get all product id from Shopify Python API

 July 01, 2022     api, python-2.7, shopify     No comments   

Issue

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.


Solution

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



Answered By - Julien
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing