Webhooks
There is a built method to listen for webhooks sent by Shopify back to your application. These are validated based on a secret you received when setting them up.
Setting Up Webhooks
- Head to your Shopify admin
- Click Settings > Notifications
- Scroll down to Webhooks and click Create Webhook
- Enter the URL as shown in the sections below.
- Leave the type as
JSON
and on the latest API.
If you are trying to test any webhooks locally you'll need to use a service like Ngrok to forward your localhost to a secure endpoint.
Product Create
Rather than running the full import continuously you can add a webhook on Product Creation that sends the data to Statamic and queues the import of that one product.
Your URL should point to the following endpoint:
https://YOURSITE/!/shopify/webhook/product/create
Product Update
Similarly, rather than running the full import to catch any changes to products, you can add a webhook on Product Update that sends any updated data to Statamic and queues a refresh of that product.
Your URL should point to the following endpoint:
https://YOURSITE/!/shopify/webhook/product/update
Product Delete
If you want products to be removed from Statamic whenever you delete them from Shopify, you can add a webhook in Shopify which tells the system to delete the products.
Your URL should point to the following endpoint:
https://YOURSITE/!/shopify/webhook/product/delete
Order Created
This will scan for all line_items
and refetch the product data.
Your URL should point to the following endpoint:
https://YOURSITE/!/shopify/webhook/order
Events
Each webhook listener also fires an event you can use to hook into with your own logic based on the payload received.
The available events are:
StatamicRadPack\Shopify\Events\CustomerCreate
StatamicRadPack\Shopify\Events\CustomerDelete
StatamicRadPack\Shopify\Events\CustomerUpdate
StatamicRadPack\Shopify\Events\ProductCreate
StatamicRadPack\Shopify\Events\ProductDelete
StatamicRadPack\Shopify\Events\ProductUpdate
StatamicRadPack\Shopify\Events\OrderCreate
Each event has one property $data
with the payload data decoded to a stdClass
.