Creating and using scripts

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

Creating new scripts

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, click Apps (or press G A):

    Apps

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

    Scripts list
  3. Click Create script to begin creating a new script.

    Click Create script
  4. 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.

  5. In the Ruby source code box, enter the code for your script. Use the Shopify scripts API documentation as a reference.

  6. 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.

    Did you know?

    Only one script can be published at a time. Publishing a script will unpublish all other scripts.

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.

Testing scripts

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

  1. From your Shopify admin, click Apps (or press G A):

    Apps

  2. Click Script Editor.

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

  4. In the Order preview section:

    • click Add in the Customer section to search for and add customers to the test order
    • in the Products section, search for and add products to the test order

    The products and customer that you add form a sample cart that you can test the script on.

  5. Click Run script in the Ruby source code box to run your script on the sample cart.

    Click Run script

    If your script doesn't have any errors, the Order preview section will update to show the discounts applied by the script. If the Order preview section does not change, or behaves in a way that you did not expect, there might be errors in your script. Check the Console box to resolve any script errors.

    Test script

    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 Save and publish to update and publish the script.

Publishing and unpublishing scripts

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.

Did you know?

Only one script can be published at a time. Publishing a script will unpublish all other scripts.

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.

Publishing a script

  1. From your Shopify admin, click Apps (or press G A):

    Apps

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

  3. Click Save and publish to save any changes to the script and publish it.

Unpublishing a script

  1. From your Shopify admin, click Apps (or press G A):

    Apps

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

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

Want to discuss this page?

Visit the Shopify Community

Ready to start selling online with Shopify?

Try it free