I trust that you found this blog post to be enjoyable. If you are interested in having my team handle your eCommerce setup and marketing for you, Contact Us Click here.

How to create a Custom Web hook in Shopify

How to create a Custom Web hook in Shopify

Step 1:  Set Up the Webhook in Shopify

  • Log in to Shopify Admin: Go to your Shopify admin panel.
  • Navigate to Webhooks:
    • Click on "Settings" at the bottom-left corner of the admin panel.
    • Go to "Notifications".
  • Create a Webhook:
    • Scroll down to the "Webhooks" section.
    • Click "Create webhook".
    • Choose "Customer creation" from the "Event" dropdown.
    • Set the format to JSON.
    • Enter your "Webhook URL"
    •  Click "Save".

 

Step 2: Create a Webhook Listener

<?php

 

// Configuration

$shop = 'your-store-name.myshopify.com';

$accessToken = 'your-access-token';

$webhookUrl = 'your-webhook-url';

 

// Function to create the webhook

function createWebhook($shop, $accessToken, $webhookUrl) {

   $data = [

       'webhook' => [

           'topic' => 'customers/create',

           'address' => $webhookUrl,

           'format' => 'json'

       ]

   ];

 

   $ch = curl_init("https://$shop/admin/api/2024-01/webhooks.json");

   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

   curl_setopt($ch, CURLOPT_POST, true);

   curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

   curl_setopt($ch, CURLOPT_HTTPHEADER, [

       'Content-Type: application/json',

       "X-Shopify-Access-Token: $accessToken"

   ]);

 

   $response = curl_exec($ch);

   if (curl_errno($ch)) {

       echo 'Error:' . curl_error($ch);

   }

   curl_close($ch);

 

   echo "Webhook creation response: " . $response;

}

 

// Function to handle the webhook and update customer tags

function handleWebhook($shop, $accessToken) {

   $webhookPayload = file_get_contents('php://input');

   $customerData = json_decode($webhookPayload, true);

 

   if (isset($customerData['id'])) {

       $customerId = $customerData['id'];

       $currentTags = $customerData['tags'];

 

       // Define the new tags you want to add

       $newTags = ['Tag1', 'Tag2'];

       $existingTagsArray = explode(', ', $currentTags);

       $updatedTagsArray = array_unique(array_merge($existingTagsArray, $newTags));

       $updatedTags = implode(', ', $updatedTagsArray);

 

       $updateData = [

           'customer' => [

               'id' => $customerId,

               'tags' => $updatedTags

           ]

       ];

 

       $ch = curl_init("https://$shop/admin/api/2024-01/customers/$customerId.json");

       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

       curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');

       curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($updateData));

       curl_setopt($ch, CURLOPT_HTTPHEADER, [

           'Content-Type: application/json',

           "X-Shopify-Access-Token: $accessToken"

       ]);

 

       $response = curl_exec($ch);

       if (curl_errno($ch)) {

           echo 'Error:' . curl_error($ch);

       }

       curl_close($ch);

 

       // Log the response or handle errors as needed

       error_log("Update response: " . $response);

   }

}

 

// Create webhook

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'create_webhook') {

   createWebhook($shop, $accessToken, $webhookUrl);

}

 

// Handle webhook request

if ($_SERVER['REQUEST_METHOD'] === 'POST' && !isset($_POST['action'])) {

   handleWebhook($shop, $accessToken);

}

?>

 

 Step 3: Test Your Webhook 

Create a test customer in Shopify to see if your webhook correctly assigns the tags. Check the response from your webhook listener to confirm it’s working as expected.
         

 

 

Back to blog