Skip to content

Instantly share code, notes, and snippets.

@deniszgonjanin
Last active February 28, 2024 15:23
Show Gist options
  • Save deniszgonjanin/1c8fb60904524e7ee2c10535a35b670f to your computer and use it in GitHub Desktop.
Save deniszgonjanin/1c8fb60904524e7ee2c10535a35b670f to your computer and use it in GitHub Desktop.
Selling variants with a deposit when out of stock

Selling variants with a deposit only when they are out of stock

Selling back-orders by collecting a deposit is a great strategy to not only secure sales that might have been lost, but also get customer committment before sourcing new inventory.

Using the Downpay API and Shopify Flow, we can create a workflow that will allow out-of-stock variants to be sold with a deposit.

We will do this by listening to variant inventory quantity changes, and adding the variant to a deposit purchase option when the inventory hits zero or below.

A second workflow will remove the variants from the same deposit option when the variant comes back in stock.

Flow templates

Below are the links to the two completed flow templates. You can import them into Shopify Flow to start playing around. You will need to edit them to add your Downpay API key in the HTTP action of each flow, as well as change the ID of the deposit purchase option you want to add/remove variants from.

Adding deposits to variants when out of stock

Removing deposits from variants when back in stock

The rest of this tutorial explains in depth how to create your own flow from scratch.

Prerequisite: create a purchase option

Within the Downpay app, create a purchase option that will be applied to out-of-stock variants. When creating the option, add a variant to it to start, to let Downpay know that this purchase option will be applied to variants henceforth:

flexli-dev · Downpay · Shopify 2024-02-27 16-44-43

After you've created this option, click on it and note the number at the end of the URL in your browser address bar. This is the purchase option ID. We will need that ID later on.

flexli-dev · Downpay · Shopify 2024-02-27 16-55-27

Flow to add a variant when it becomes out of stock

In Shopify Flow, create a new flow. The flow will have three parts:

add

Trigger: listen to change in inventory

To kick off the flow, we add a 'Product Variant Inventory Change' trigger. This will run this flow anytime a variant's inventory is changed.

Condition: check whether the inventory went down to 0 or below

If the inventory changed to zero or below, we will go onto the next step. Otherwise do nothing.

Action: Send an API request to Downpay to add the variant to a purchase option.

We will call the Downpay API using the Send HTTP request action, which is found under the Flow set of available actions in Shopify Flow.

There are a few configuration options to enter here:

  • HTTP Method: Set to POST
  • URL: Set to https://downpay.hypehound.app/graphql

For Headers, there are two to set:

  • Key: Content-Type, value: Application/json
  • Key: Authorization, Value: Token <your_api_key>

You can find the Downpay API key for your shop inside the Downpay app, under the Settings page.

For the Body field, enter the following:

{
  "query": "mutation addVariant($input: PurchaseOptionGroupAddProductVariantsInput!) { purchaseOptionGroupAddProductVariants(input: $input){ errors { field message }}}",
  "operationName": "addVariant",
  "variables": {
    "input": {
      "id": "<your_purchase_option_id>",
      "productVariantIds": ["{{productVariant.id}}"] 
    }
  }
}

In the above snippet, replace the text "<your_purchase_option_id>" with the ID of the purchase option we created in Downpay earlier.

If you don't want to apply this logic to all the variants in your store, you can add another condition in the middle of the flow, for example to check if a particular tag is applied to a product. That way you can only offer deposits on out-of-stock variants of a particular product type, instead of store-wide.

But we're not done yet! We still need to create a second flow that will do the opposite - remove variants from a purchase option when stock climbs above zero...

Flow to remove a variant from a deposit purchase option when it comes back in stock

To start off, duplicate the flow we just created. You can do this by selecting the flow on the main shopify flow screen. The new flow will be very similar to the first one, it's simply some of the logic that will change. The finished version will look similar to this:

flexli-dev · Flow · Shopify 2024-02-27 17-08-11

Trigger

The trigger will remain the same - we want this flow to run whenever variant inventory changes.

Condition

The condition will change to only allow execution to proceed if the inventory level is 1 or above.

Action

The action is similar to the previous one, however the body of the API call will change, to the following:

{
  "query": "mutation removeVariant($input: PurchaseOptionGroupRemoveProductVariantsInput!) { purchaseOptionGroupRemoveProductVariants(input: $input){ errors { field message }}}",
  "operationName": "removeVariant",
  "variables": {
    "input": {
      "id": "<your_purchase_option_id>",
      "productVariantIds": ["{{productVariant.id}}"] 
    }
  }
}

Once again, change <your_purchase_option_id> to the ID of the Downpay purchase option created in the first step of the tutorial.

Wrapping up

Once you're done editing, make sure both your flows are activated. You can then test things out by adjusting the stock on any variant and seeing the deposit options get enabled/disabled on the storefront product pages as the variants come in and out of stock.

For questions and support, contact us at support@hypehound.io

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment