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.

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

    Click Create script
  4. In the Select script template dialog, choose a template for your script. Use one of the example scripts as a starting point, or choose Blank template to start writing a script from scratch.

    Note: The type of script you choose here (Line item or Shipping rate) 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. Click Create script to create a script with the type that you have chosen.

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

  7. In the Ruby source code box, 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. 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

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

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.

Tip

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 Publish or Save and publish to save changes to the script and publish it.

    Click save and publish

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