Connect Shopify Products to Discourse Groups

This is a guide on how to connect the sale of products on Shopify to access to groups on Discourse.

An example use case for this is where you are selling access to a user group on discourse which you use to control access to a private category hosting private discussions or premium content.

For the rest of this guide I’ll assume we already have a Discourse user group with the name “premium” that we want to sell access to on Shopify.

Step 1. Setup your Shopify products

The first thing you need is a product on your Shopify store. We’ll use a product called “Premium” in our example

The next thing we need to do is set up a “metafield” for our product to store which Discourse group this product buys access to. There are various ways to manage metafields in Shopfiy. I like to use the Shopify app Metafields Guru. This gives you an easy way to associate metafields with your products.

We want to create a metafield with the following values for our Premium product:

  • namespace: discourse
  • key: group
  • value: premium

It should look like this

Basically we’re saying that this product is associated with the Discourse group with the name premium. We could enter any other Discourse group name in tthe “value” field.

Step 2. Setup your Shopify webhook

We want our users to get access to the “premium” group as soon as they purchase the premium product on shopify. The way we’re going to tell Discourse that the purchase has happened is by using a Shopify Webhook.

To Setup your webhook, click on “Settings”, click on “Notifications”, and then scroll down the bottom. You should see a section called “Webhooks”. Click “Create webhook”. In the modal we want to enter these values

  • Event: Order creation
  • Format: JSON
  • URL: https://[your_discourse_domain]/webhook-receiver/receive (e.g. https://angus.thepavilion.io/webhook-receiver/receive)
  • Webhook API Version: Latest

It should look like this (but with your domain instead of angus.thepavilion.io)

What we’re doing here is saying “When any order is created on my store, send a notification to my discourse with the order details”.

While you’re in your Notifications settings, also copy the long string of numbers and letters you see just below the webhook list that is highlighted in yellow and is introduce with the words “All your webhooks will be signed with”. Paste that in a notepad. We’ll need it later.

Step 3. Setup the Discourse Webhook Receiver Plugin

We now need to install the Webhook Receiver Plugin in Discourse so we can handle the webhook data that Shopify is going to send to Discourse whenever someone creates an order. First setup authentication for your webhook. Then setup the webhook payload handling following these steps.

In this implementation you need to setup the receipt request (Step 1A). To build your receiver request url, we need to set up a private app in your shopify admin. Click “Apps”, then click the link at the bottom of the view: “Manage private apps”:

Click “Create new private app” and fill out the form. Enable “Read Access” for Products for this app.

Once your app is created, scroll down to the “Example Url” field and copy / paste that into a text file. It will look something like this

https://06793f182355e8f5a73f63f9f7c33de5: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a@pav-dev.myshopify.com/admin/api/2021-01/orders.json
We want to update this url to retrieve the metafields of a product, which is what we want for our webhook receiver receipt request. If you take a look at the Shopify API docs for metafields you’ll see we need to change our url to

https://06793f182355e8f5a73f63f9f7c33de5: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a@pav-dev.myshopify.com/admin/api/2021-01/products/:key/metafields.json

Note that we’ve used :key here for the product id, as that is how keys obtained from a webhook payload are interpolated in the receipt request url in the webhook receiver plugin.

The rest of the steps in the webhook receiver plugin setup should be clear from the documentation for that plugin. As we’re using product metafields here to map Shopify products to discourse groups, our final key object map will look like this

premium:group.139

premium is the value we set for our product meta field and 139 is the id of the group we want to add users who purchased the premium product to.

1 Like