Creating and using scripts in Script Editor

You can create and run scripts on your online store's cart by using the Script Editor.

Shopify Plus

Scripts and the Script Editor app are available to Shopify Plus merchants only.

Create a new script

When the Script Editor is installed in your store, you can use it to create scripts. Visit the Shopify scripts API documentation for a reference on writing code for scripts.

  1. From your Shopify admin, go to Apps.

  2. Click Script Editor. The Scripts overview page shows a list of scripts for your store.

    Creating new script img2
  3. Click Create script to begin creating a new script.

    Click Create script
  4. In the Select script template dialog:

    1. Choose the type of script that you want to create.
    2. Choose an example template as a starting point, or choose Blank template to start writing a script from scratch.
    3. Click Create script to create a script with the type that you have chosen.

      Tip

      The type of script you choose here (Line items or Shipping rates) determines the script's type, and the methods that the script will be able to use. The script type that you choose in this step cannot be changed later.

  5. In the Title box, enter a name for the script. Use a descriptive name so you can tell what the script does at a glance.

  6. Click Code to open the Ruby source code console.

  7. Enter the code for your script. Use the Shopify scripts API documentation as a reference. Remember that your script can only access the methods available to its type. For example, a line item script cannot use shipping methods.

  8. Click Run script to run the script.

  9. When you are finished:

    • click Save draft to save an unpublished draft of the script
    • click Save and publish to create and publish the script.

    Tip

    You can publish one script per script type. For example, you can publish a line items script and a shipping rates script, but you can't publish two line items scripts at the same time.

Script templates

When you choose to create a new script, you can choose from a few templates that will let you start writing code for specific scripts faster. The following templates are available:

  • Percentage (%) off a product
  • Amount ($) off a product
  • Percentage (%) and amount ($) off a product
  • Tiered discounts
  • Buy one get one free (BOGO)
  • Blank template (that is, no template code)

When you create the script, the Ruby source code box contains the default code for the template that you chose. You can use the template code to start writing your script.

The Script Editor documentation also has more script examples.

Error checking and debugging

The Script Editor will check your code for basic errors while you're writing a script:

Error reporting in the Script Editor

The Script Editor will also report errors that occur in your published scripts:

Screenshot of errors published scripts in the Script Editor

It's a good idea to correct errors in your script before testing or publishing it. If your script has syntax errors, testing your script probably won't work.

If you need more information on a script error, then hover over the red x icon in the Script Editor's margin:

Hovering over the X in the Script Editor margin reveals details about syntax errors

Debug production errors

Production errors are errors in a published script that occur when the script is run in your online store. Details about these errors are connected to the script that was running when the error occurred. If you duplicate a script that produced errors, then the error information remains with the original script and is not copied to the duplicate script.

To debug a published script:

  1. Duplicate the published script that contains the errors.

  2. Publish the duplicate script so that there is a version of the script that is available to your customers in your online store. This step assumes that the script doesn't include so many errors that it needs to be removed (unpublished) from your online store.

  3. Debug the original script using the information about the errors.

Test a script

While you are working on a script, you can test it from within the Script Editor to make sure it works.

To test a script:

  1. From your Shopify admin, go to Apps.

  2. Click Script Editor.

  3. Click the name of the script that you want to test.

  4. To edit a test cart, click Input. You can also click Both to see the script code and the test cart at the same time:

    Click 'Input' in the Script Editor

    The input view shows options for editing a cart to test your script. You can add products, customers, discount codes, customer information, and shipping information based on what you want to test your script against.

    • Type in the Products section to search for and add products to the test order.
    • Type in the Discount code section to search for and add discount codes to the test order.
    • Type in the Customer information section to search for and add customers to the test order.
    • Click Add or Edit in the Shipping address section to change the shipping address for the test order.
  5. Click Run script to see the changes that the script makes to your test cart:

    Click Run script

    If your script runs successfully, the Output section will update to show the changes applied by the script. If the Output section doesn't change, or there are unexpected changes, there might be errors in your script. Check the Console box in the Ruby source code section to resolve any script errors:

    View test cart output

    The Console box will report the script's resource usage. More complex scripts will use more resources. Scripts have a limited amount of CPU and memory available to them.

  6. When you are finished:

    • click Save draft to update and save an unpublished draft of the script
    • click Publish to update and publish the script.

Publish and unpublish a script

You can publish or unpublish existing scripts from within the Script Editor app. The current published script cannot be changed. To edit a published script, you must unpublish it, edit the draft, and then publish it again.

You can also duplicate a script and use it as a starting point to create a new script or use it for testing and debugging.

Tip

Only one script per script type can be published at one time. For example, you can publish a line items script and a shipping rates script, but you can't publish two line items scripts at the same time.

Publishing a script does not apply its discounts to existing carts. Similarly, unpublishing a script does not remove discounts that the script has applied to existing carts.

Publish a script

When you publish a script, error counts that are connected to the script are cleared and information about the errors disappears from the script.

To publish a script:

  1. From your Shopify admin, go to Apps.

  2. Click the name of the draft script that you want to publish.

  3. Click Publish or Save and publish to save changes to the script and publish it:

    Click save and publish

Unpublish a script

To unpublish a script:

  1. From your Shopify admin, go to Apps.

  2. Click the name of the published script that you want to unpublish.

  3. Click Unpublish to make the script an unpublished draft.

Duplicate a script

To duplicate a script:

  1. From your Shopify admin, go to Apps.

  2. Click the name of the script that you want to duplicate.

  3. Click Duplicate to make a copy of the script.

Schedule scripts to run during Launchpad events

You can also schedule line item and shipping scripts to run during events using the Launchpad app.

Want to discuss this page?

Visit the Shopify Community

Ready to start selling online with Shopify?

Try it free