Skip to main content

Migrating from Shopify Scripts to Shopify Functions

Functions availability

Shopify Scripts allow you to customize cart behavior, shipping, and payment methods in a store. With Shopify Functions, these customizations are now handled through dedicated Function APIs that offer better performance and flexibility. This guide shows how to migrate your existing Shopify Scripts to Shopify Functions by testing in production without disrupting customers.


In this tutorial, you'll learn how to do the following tasks:

  • Map your existing Shopify Scripts functionality to the right Shopify Functions.
  • Test new Functions safely alongside existing Scripts in production.
  • Deploy Functions without affecting customers.


Anchor to Function APIs mappingFunction APIs mapping

Use the following table to map your existing Shopify Script to the appropriate Function API:

Shopify Script typeShopify Function APIUse case
Line item scriptsDiscounts APIApply discounts to products, or the entire cart.
Line item scriptsCart Transform APIModify cart line items (bundle, merge, or add items).
Line item scriptsCart and Checkout Validation APIValidate cart contents and block checkout under certain conditions.
Shipping scriptsDelivery Customization API and Discounts APIHide, rename, or reorder delivery methods, and apply discounts to delivery methods.
Payment scriptsPayment Customization APIHide, rename, or reorder payment methods.

You can test your new Shopify Function alongside your existing Shopify Script without affecting customers.

  1. In the Shopify admin, go to Apps > Script Editor.

  2. Create a temporary script with the following code but don't publish it:

    Output.cart = Input.cart
  3. Save the script as a draft and note the preview URL, which follows this format:

    https://{your-store}.myshopify.com/admin/scripts/preview?script_id={script_id}

    When accessed, this preview link runs the temporary passthrough script instead of your published script, effectively disabling your current script's logic for that session.


Anchor to Step 2: Add testing logic to your FunctionStep 2: Add testing logic to your Function

In your Shopify Function, add logic to target specific test users. The most reliable method is using customer tags.

  1. In the Shopify admin, go to Customers and add a tag like TESTER to customers who should experience the new Function behavior.

  2. In your Function's input query, include customer tag information:

    src/run.graphql

    query Input {
    cart {
    buyerIdentity {
    customer {
    hasAnyTag(tags: ["TESTER"])
    }
    }
    # ... rest of your query
    }
    }

Anchor to Implement conditional logicImplement conditional logic

Add conditional logic in your Function to only apply new behavior to tagged customers:

File

// Check if the customer has the TESTER tag
let has_tester_tag = input
.cart()
.buyer_identity()
.and_then(|bi| bi.customer())
.map(|customer| customer.has_any_tag())
.unwrap_or(&false);

if *has_tester_tag {
// Apply new Function logic here
// ... your function implementation
} else {
// Return no changes (let existing script handle it)
return Ok(FunctionRunResult { operations: vec![] });
}
const customer = input.cart.buyerIdentity?.customer;
const hasTesterTag = customer?.hasAnyTag ?? false;

if (hasTesterTag) {
// Apply new Function logic here
// ... your function implementation
} else {
// Return no changes (let existing script handle it)
return { operations: [] };
}

Anchor to Step 3: Deploy and test in productionStep 3: Deploy and test in production

You can test your Function in development and production.

  1. Test your Function thoroughly in a development store:

    Terminal

    shopify app dev
  2. Verify that the Function behaves identically to your existing Shopify Script.

Anchor to Production deployment and testingProduction deployment and testing

Choose one of the following deployment methods:

Anchor to Option A: App with configuration UIOption A: App with configuration UI

If your Function is part of an app with a user interface, then use the app to enable and configure the Function in production.

Anchor to Option B: GraphiQL deploymentOption B: GraphiQL deployment

Note

This method works best for Functions that don't require ongoing merchant configuration.

If you have a custom Function without UI configuration, then complete the following steps:

  1. Use the Shopify GraphiQL App to enable the Function directly.

  2. Retrieve a list of available Functions using this query:

    query.graphql

    query getFunctions {
    shopifyFunctions(first: 100) {
    edges {
    node {
    id
    title
    apiType
    }
    }
    pageInfo {
    hasNextPage
    endCursor
    }
    }
    }
  3. Use the Function's id in the appropriate creation mutation for your Function type (for example, discountAutomaticAppCreate or deliveryCustomizationCreate).

  1. Tag test users: Add the TESTER tag to select customers or your own test account.
  2. Test with script preview: Have tagged users access your store using the preview link from Step 1 to disable the existing script.
  3. Verify behavior: Ensure the Function produces the same results as the original Script for tagged users, while untagged users continue to experience the original Script behavior.

Anchor to Step 4: Complete the migrationStep 4: Complete the migration

After you've verified that the Function works correctly:

  1. Remove testing code: Update your Function to remove the customer tag conditional logic.

  2. Deploy the final version:

    Terminal

    shopify app deploy
  3. Deactivate the Script: In your Shopify admin, go to Apps > Script Editor and unpublish your existing Script.


  • Always test in a development store before deploying to production.
  • Keep the testing period short to minimize code complexity.
  • Monitor your store's performance and error logs after migration.
  • Have a rollback plan ready in case issues arise.
  • Document any differences in behavior between Scripts and Functions.
  • Test edge cases that might behave differently between Scripts and Functions.

You might encounter the following issue during migration:

  • Function not triggering: Verify the Function is properly enabled and configured.
  • Different behavior: Check for differences in input data structure between Scripts and Functions.


Was this page helpful?