How to configure Webhooks in Shopify App?

How to configure Webhooks in Shopify App?

07 Jun 2021
0 Comments
How to configure Webhooks in Shopify App?

Webhooks are a useful tool in Shopify for retrieving and storing data from any certain Admin events.

 

Webhooks can be useful for the followings:


● Synchronize multiple e-commerce platforms, third-party apps, CRMs, or stores in Shopify
● Sending notifications to Merchants for new Orders
● Sending notifications to Customers for their Orders
● Changing a product’s price in other eCommerce platforms
● Integrate our own Accounting Softwares
● And many more ...

 

Here, I am going to give a quick example of a simple webhook configuration in the Shopify App.

 

Before we move forward, I am assuming you already have created a Shopify app, if not I will recommend first create it using the ‘shopify_app’ gem here is GitHub documentation:
https://github.com/Shopify/shopify_app

 

Let's assume we have a requirement to send a welcome order notification to new customers when they purchase an item from the store for the very first time.

 

First, we need to add an order event webhook to our config using the add_webhook generator. Please run the following command:

 

rails g shopify_app:add_webhook -t orders/create -a
https://example.com/webhooks/orders_create

 

Here, please replace “ https://example.com ” with your app URL. Where “-t” is the topic and “-a” is the address to where the webhook should be sent to.

 

We can check the configuration in the shopify_app.rb initializer file. It should look like this:

 

ShopifyApp.configure do |config|
    config.webhooks = [
       {topic: 'orders/create', address: 'https://example.com/webhooks/orders_create'}
     ]
end

 

If you are doing configuration changes to your existing Shopify app, then make sure to delete the app from the Store if it is already installed and then reinstall the app so as to reflect the configuration changes into the app.

 

When installing the app, Shopify completes the OAuth flow and its callback, and ShopifyApp will then queue a background job which will ensure all the specified webhooks exist for our shop.

 

So, now run the below command to create an Orders_Create Job:

rails generate job orders_create

 

Add the following line of codes in the created job:

 

class OrdersCreateJob < ActiveJob::Base
    def perform(shop_domain:, webhook:)
       shop = Shop.find_by(shopify_domain: shop_domain)

       if shop.nil?
         logger.error("#{self.class} failed: cannot find shop with domain
'#{shop_domain}'")
           return
     end

     shop.with_shopify_session do
         domain = ShopifyAPI::Shop.current.domain
         OrderMailer.order_notification(webhook[:customer][:email],
webhook[:customer][:state], domain).deliver_now
           end
     end
end

 

Here, we will receive a webhook for creating an order event. I have sent Customer Email, Customer state i.e. whether the customer account is disabled/enabled/invited to the Order Mailer to send Order notification mail. You can update the above code by whatever parameters you want to pass to your Action Mailer.

 

Now you need to create a mailer for OrderMailer using Action Mailer. For Mailer configuration, please refer: https://guides.rubyonrails.org/action_mailer_basics.html

 

After completing all configurations, you need to create an Order from the Shopify store, and can now see an Order notification mail being delivered to your (customer) mail.

 

Here is a sample Order notification mail I received after placing an order from the Shopify store for which I set up the above webhook.

You can find here the list of supported webhook events by Shopify:
https://shopify.dev/docs/admin-api/rest/reference/events/webhook#events-2021-04

 

I hope you got a better understanding of Webhooks and its configuration in Shopify. Thanks for reading. We would love to hear from you, please feel free to put your valuable comments. Let me know if you encounter any issues while configuring it.




Leave a comment: