Skip to main content

Build a Discounts Allocator Function

Developer Preview

This Function API is available only in the Discounts Allocator developer preview.

The Discounts Allocator Function will be available only to Shopify Plus merchants.

Building a Discounts Allocator Function allows for greater customization in defining discount strategies, like implementing custom logic to distribute discounts across line items in an order.

In this tutorial, you'll create a Discounts Allocator Function that uses metafields to help define its custom allocation logic.

The first metafield is applied at the order level. It sets a cap on the discount, ensuring that a 50% discount won't exceed a specified dollar amount.

The second metafield is applied at the shop level. It sets a maximum discounted amount for a cart.

Requirements

Create an app

Create a Remix app that has the write_discounts and read_products access scopes, using Shopify CLI 3.72.1 or higher.

Install Node.js

Install Node.js 22 or higher.

Install the app

Install your app on the development store.

Project

Anchor to Update your app's access scopesUpdate your app's access scopes

Your app requires the write_discounts_allocator_functions access scope to create and update discounts.

Anchor to Update the ,[object Object], fileUpdate the shopify.app.toml file

Add the write_discounts_allocator_functions scope to your app's shopify.app.toml file.

Error loading file. Please try again later.

Anchor to Deploy your app with new access scopesDeploy your app with new access scopes

  1. Deploy your updated configuration to Shopify:

    Terminal

    shopify app deploy
  2. Restart your app development server:

    Terminal

    shopify app dev
  3. Open or reinstall your app using the URL provided by Shopify CLI. To preview your extensions, press p in the terminal window where you started your app.

  4. Accept the new access scope permissions in your store.

Anchor to Create the Discounts Allocator FunctionCreate the Discounts Allocator Function

  1. Navigate to your app's directory:

    Terminal

    cd directory
  2. Run the following command to create a new Discounts Allocator Function:

    Terminal

    shopify app generate extension --template discounts_allocator --name discounts-allocator
  3. Choose the language that you want to use. For this tutorial, select JavaScript.

Anchor to Update the configurationUpdate the configuration

Anchor to Replace the content of the extension's ,[object Object], fileReplace the content of the extension's run.graphql file

This GraphQL query is structured to retrieve all necessary data for applying discounts to shopping cart items, including discount details, cart line items, and shop metafields.

It ensures the function has access to discount rules, item specifics, and cap limits to accurately calculate and apply discounts according to predefined policies.

Error loading file. Please try again later.

Navigate to your extension's directory:

Terminal

cd extensions/discounts-allocator

Run the following command to regenerate types based on your input query:

Terminal

shopify app function typegen

Anchor to Update the function logicUpdate the function logic

This function applies discounts to shopping cart items according to set rules and limits.

It calculates discount amounts, enforces caps to prevent excessive reductions, and compiles the results and any errors into a structured output, ensuring fair and policy-compliant discount application.

Error loading file. Please try again later.

Anchor to Define discount metafields for orders and the shopDefine discount metafields for orders and the shop

Use the GraphiQL app to create metafield definitions for the discount and shop.

Anchor to Create the discount metafield definitionCreate the discount metafield definition

To set the maximum discount amount for each discount, use the metafieldDefinitionCreate mutation with ownerType: "DISCOUNT".

Error loading file. Please try again later.

Anchor to Create the shop metafield definitionCreate the shop metafield definition

To set the maximum discount amount for the cart, use the metafieldDefinitionCreate mutation with ownerType: "SHOP".

Error loading file. Please try again later.

Anchor to Create the frontend UI for registering your Discounts Allocator FunctionCreate the frontend UI for registering your Discounts Allocator Function

After a merchant installs your app, they'll need to register the Discounts Allocator Function to actively allocate discounts.

Caution

You're replacing the Shopify discount engine with the Discounts Allocator Function. Your Function will take precedence over most discount features that are built by Shopify.

Anchor to Create a new route for the Discounts Allocator FunctionCreate a new route for the Discounts Allocator Function

In app/routes, create a new file named app.discounts-allocator.$functionId.jsx.


The Shopify Remix app template uses file-based routing, so the filename determines the page's URL. The $ prefix indicates that functionId is a dynamic segment. The path for this page is /app/discounts-allocator/{functionId}.

Error loading file. Please try again later.

Anchor to Add a Remix ,[object Object], function to handle form submissionAdd a Remix action function to handle form submission

Add a Remix action to app.discounts-allocator.$functionId.jsx to handle the form submission. This function calls the discountsAllocatorFunctionRegister mutation to register the Discounts Allocator Function.

Error loading file. Please try again later.

Anchor to Create the UI for registering the Discounts Allocator FunctionCreate the UI for registering the Discounts Allocator Function

Use the primaryAction in the Page component to expose a button that calls the action function. This button allows merchants to register the Discounts Allocator Function.

Error loading file. Please try again later.

In extensions/discounts-allocator/shopify.extension.toml, populate the create and details setting in the [extensions.ui.paths] section. This change is automatically applied as long as you're running dev.


The settings in the shopify.extension.toml file define the URLs that Shopify uses to enable merchants to create and edit discounts based on your Function. Shopify automatically fills in any dynamic tokens in these URLs.

Error loading file. Please try again later.

Anchor to Register your Discounts Allocator FunctionRegister your Discounts Allocator Function

  1. If your app isn't already running, then start it using Shopify CLI:

    Terminal

    shopify app dev
  2. Follow the CLI prompts to preview your app, and install or view it on your development store.
  3. In your development store, navigate to /app/discounts-allocator/:functionId and register your Discounts Allocator Function.
    The UI for register the discounts allocator
Info

The functionId is a dynamic segment in the route. It's used to identify the Discounts Allocator Function that the merchant is registering. You can find your functionId in your app's .env file or in your Partner Dashboard if you navigate to Apps > "your app" > Extensions > discounts-allocator-js.

Troubleshooting

If you receive a Could not find Function error, then confirm the following:

  • The Function ID is correct.
  • You've installed the app on your development store.
  • Development store preview is enabled in the Partner Dashboard.
  • Your app has the write_discounts access scope.

Anchor to Create a discount and build a cartCreate a discount and build a cart

To test the single-discount-cap capability of your allocator, prepare an automatic discount and build a cart to apply the discount.

  1. In your Shopify admin, go to Discounts.
  2. Click the Create discount button near the top right of your page and select Amount off products. Fill in the values for the discount:
    • For Method, use Automatic.
    • For Discount percentage, use 50.
  3. Open your development store and build a cart with some eligible products. The discount amount applied should be 50 percent off.

Anchor to Test your Discounts Allocator Function with a Single Discount CapTest your Discounts Allocator Function with a Single Discount Cap

Anchor to Set the maximum eligible amount for individual discountsSet the maximum eligible amount for individual discounts

Use the metafieldsSet mutation to set the single-discount-cap metafield for the discount you created.

Error loading file. Please try again later.

Anchor to Replace the placeholdersReplace the placeholders

Replace DISCOUNT_ID with the ID of the discount you created and replace SINGLE_DISCOUNT_CAP with the desired cap, for example 500.

Error loading file. Please try again later.


Notice that the discount amount applied to your cart is capped at the value you set in the metafield when you refresh the cart.

Anchor to Create a maximum discount amount for a cartCreate a maximum discount amount for a cart

Anchor to Set the maximum discount amount for a cartSet the maximum discount amount for a cart

Use the metafieldsSet mutation in your shop to utilize the per-cart-cap capability of your allocator.

Error loading file. Please try again later.

Anchor to Replace the placeholdersReplace the placeholders

Replace SHOP_ID with the ID of your shop and replace PER_CART_CAP with the desired per cart cap, for example 50.

Error loading file. Please try again later.

Anchor to Test your Discounts Allocator Function with a Per Cart CapTest your Discounts Allocator Function with a Per Cart Cap

  1. Click the Create discount button and select Amount off order. Fill in the values for the discount:
  • For Method, use Automatic.
  • For Fixed Amount, use 500 or any amount higher than your PER_CART_CAP.
  1. Go to the cart you created and refresh the page, the total savings will be capped at the PER_CART_CAP value you set in the metafield.
Was this page helpful?