How To Display Shopify Products with PHP

  Shopify

So you have finally set up your Shopify app. You have connected it to your web host. Now, you want to start working with Shopify API to display the products of the Shopify store.

In this tutorial, we’re going to take a look at how to display Shopify products using PHP.

If in case you ended up here and you don’t have any Shopify developer account, then we suggest reading our guide below to get started.

 

Getting Started

Create a new PHP file and name it products.php inside the file paste the following code:

<?php require_once("inc/functions.php"); $requests = $_GET; $hmac = $_GET['hmac']; $serializeArray = serialize($requests); $requests = array_diff_key($requests, array('hmac' => '')); ksort($requests); $token = PASTE YOUR GENERATED TOKEN HERE $shop = PASTE YOUR SHOP URL HERE

The code above will basically retrieve first the data of the Shopify store.

After getting all the data, assign the $_GET['hmac'] to a new variable $hmac then serialize the $requests variable. Then remove the key ‘hmac’ inside the array $requests.

Then we’ll sort the $requests variable with ksort() function.

Next, the following variables are important for making Shopify requests.
Remember that we generated a token when we installed the app in our store? Right now we’re going to use that, now paste the token in the value of $token variable.

Note that we are not yet using the database and so it’s important to back up the access token in a file for reference.

Next, the $shop variable. Paste the URL of the store that you are using for this app BUT without https and myshopify.com. Just the SUBDOMAIN.

Example:

https://this-is-the-subdomain.myshopify.com/

Using Collection API

$collectionList = shopify_call($token, $shop, "/admin/api/2020-07/custom_collections.json", array(), 'GET'); $collectionList = json_decode($collectionList['response'], JSON_PRETTY_PRINT); $collection_id = $collectionList['custom_collections'][0]['id']; $array = array("collection_id"=>$collection_id); $collects = shopify_call($token, $shop, "/admin/api/2020-07/collects.json", $array, 'GET'); $collects = json_decode($collects['response'], JSON_PRETTY_PRINT);

We’ll be using the function shopify_call() to get the first collection ID that we needed for retrieving the products. You may skip this part if you want to display specific products from a collection ID.

To display specific products from a collection ID, you can use this code instead:

$collection_id = PASTE YOUR COLLECTION ID HERE $array = array("collection_id"=>$collection_id); $collects = shopify_call($token, $shop, "/admin/api/2020-07/collects.json", $array, 'GET'); $collects = json_decode($collects['response'], JSON_PRETTY_PRINT);

Where can I get my collection id?

Finding Collection ID in Shopify Admin page
  • Click Products
  • Next, Collections
  • Then click one of your desired collection.
  • Check the URL
https://example.myshopify.com/admin/collections/1234567890

The numerical value at the end of the URL above is the collection ID, copy that and paste it to your $collection_id variable.

To explain, the code above will return an array that contains all the product IDs inside the collection ID that we used. Basically, it’s a JSON code but we decoded it and turn it into an array that’s why we use json_decode() function.

Using Product API

foreach($collects as $collect){ foreach($collect as $key => $value){ $products = shopify_call($token, $shop, "/admin/api/2019-07/products/".$value['product_id'].".json", array(), 'GET'); $products = json_decode($products['response'], JSON_PRETTY_PRINT); echo $products['product']['title']; } }

The code above will display all the product’s name available inside the collection ID that we have used.

Output

Display Shopify Products per Collection ID using PHP and Shopify API

Congratulations! Now you have successfully displayed all the products that are available inside a certain collection. If you have concerns or questions, don’t hesitate to ask down in the comments.

 

React to this topic
Subscribe
Notify of
guest
20 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments