Videos
You can't get more than 250 products with a single request from Shopify.
Refer to the docs here: https://shopify.dev/docs/admin-api/rest/reference/products/product?api[version]=2020-04 ( where the limit max value can be 250 )
In order to get more than 250 products you need to make a recursive function and use the page_info argument to make paginated requests. More on the matter can be seen here: https://shopify.dev/tutorials/make-paginated-requests-to-rest-admin-api
When you make a request and there is a pagination shopify returns a header similiar to this one:
Link: "<https://{shop}.myshopify.com/admin/api/2019-07/products.json?page_info=vwxyzab&limit=6>; rel=next"
In order to make a request to the second page you need to grab the link and make a request to it, the same applies when you make that request, there will be the same header if there are more pages and so on.
So you need to get your response header and the link from it and make it recursive:
function makeRequest(nextLink = '{STORE URL}/products.json?limit=250'){
return new Promise((resolve, reject) => {
fetch(nextLink).then(r => {
const headerLink = r.headers.get('link');
const match = headerLink.match(/<[^;]+\/(\w+\.json[^;]+)>;\srel="next"/);
const nextLink = match ? match[1] : false;
if(nextLink){
makeRequest(nextLink)
} else {
resolve();
}
})
})
}
Two cases are here:
Case 1: You have the admin access. You can simply paste the below code snippet into your theme.liquid file and get all product data in browser console.
<script>
const total_products = {{- shop.products_count | default : false -}};
if(total_products){
let storeProducts = [];
let fetchCalls = [];
let ceailedCount = Math.ceil(total_products/250);
for (let i =1 ; i <= ceailedCount; i++ ){
fetchCalls.push(fetch('/products.json/?limit=250&page='+i))
}
Promise.all(fetchCalls)
.then(m => m.forEach(s => {
let k = s.json();
k.then( s => { storeProducts.push(s.products) })
}
)).then( j => console.log('process Completed',storeProducts ));
}
</script>
Case 2: You are viewing the website as a customer. In this case, you must first know how many total products are in the store, which you can get by making the below Ajax calls within the browser console. All you have to do is run the below fetch calls one by one in the browser console until less than 250 products or no product array is returned.
fetch('/products.json?limit=250&page=1').then(r => r.json()).then( p => console.log(p));
fetch('/products.json?limit=250&page=2').then(r => r.json()).then( p => console.log(p))
fetch('/products.json?limit=250&page=3').then(r => r.json()).then( p => console.log(p))
fetch('/products.json?limit=250&page=4').then(r => r.json()).then( p => console.log(p))
fetch('/products.json?limit=250&page=5').then(r => r.json()).then( p => console.log(p))
//and so on... until you are getting product results.
Note that; all the fetch calls are the same, just the page number is different. Each Fetch call returns us up to 250 products. When you keep making fetch calls in the same order, and the last fetch call will return either no product or less than 250 products. Which is the last fetch call you have to make. Consequently, you know the total number of products, ( 250 times page number ). Note the page number of that last call and then run the below code ( REPLACE THE PAGE_NUMBER WITH YOUR NUMBER )
let storeProducts = [];
let fetchCalls = [];
let ceailedCount = PAGE_NUMBER;
for (let i =1 ; i <= ceailedCount; i++ ){
fetchCalls.push(fetch('/products.json/?limit=250&page='+i))
}
Promise.all(fetchCalls)
.then(m => m.forEach(s => {
let k = s.json();
k.then( s => { storeProducts.push(s.products) })
}
)).then( j => console.log('process Completed',storeProducts ));
Finally, you will get the all products in an array storeProducts