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

New contributor

Ruel Almonia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.