Ecommerce Shopify WordPress Discussion

How to get the total count of products in Shopify GraphQL API in Laravel?

I don't know how to get the total count of my products in shopify graphql. I need to get the total count of my products in shopify because I am creating a pagination with numbers. As of now I created cursor-based pagination which is the next and previous buttons but I want to add numbers of pages. I tried to remove the variable $numberProduct, I am using this to fetch only of number of products (9) per page and now I directly put the first: 100 in my query and the numbers or array of pages is showing but all of my products is showing in my product page/blade view because I declare it in my query which is the 100. This is my logic on how to get the numbers of pages: // Calculate total number of products $totalProducts = count($data['data']['products']['edges']); // Calculate total number of pages $totalPages = ceil((int)$products / $numProducts); // Generate an array of page numbers $pageNumbers = range(1, $totalPages); Output: 1 number of pages This is where I try to remove the variable $numberProduct and declare directly to my query first: 100: // Calculate total number of products $totalProducts = count($data['data']['products']['edges']); // Calculate total number of pages $totalPages = ceil((int)$totalProducts / 9); // Generate an array of page numbers $pageNumbers = range(1, $totalPages); Output: 12345678 number of pages which is correct
You cheat and skip the GQL and just ask the products endpoint for the count. Same as asking the RestAPI for the count. Instant and easy. There is no analog in GQL for that. GQL works off of cursors, arguably the much faster way of paging through products, but there is no totalCount with GQL.

January 17, 2024

This is just an idea, I'm not sure if it will work: $api = ShopifyApp::api(); // Define the query. $query = '{ shop { products(first: 1) { totalCount } } }'; $response = $api->graph(GraphqlEnum::ADMIN_API, new GraphqlValue($query)); // Total count of products. $totalCount = $response['data']['shop']['products']['totalCount'];

January 17, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.