Code Monkey home page Code Monkey logo

shopify-scripts's Introduction

Shopify Scripts Public Repository

Shopify Scripts are customizations written in Ruby that allow you to create personalized customer experiences. Using line item, shipping, and payment scripts you can implement custom logic and tailor the user experience during a customer’s checkout journey. Scripts are enabled on a store’s checkout by using the Script Editor app.

This repository is administered by Shopify Plus for the purposes of collaboration among our community of merchants and partners. However, scripts submitted are not vetted or supported by Shopify or your Merchant Success Manager. Equally, while we do not guarantee support for scripts from the original contributor, please get in touch with them in the first instance with any issues or questions.

Read the License for more information.

How they work

Shopify Scripts are written in a stripped-down version of Ruby, and work by receiving an “input” of the cart, customer, and shipping methods or payment gateways, run the script code to perform modifications, and then return the result as an “output” which is then applied to the cart or checkout. The Script Editor app hosts scripts you’ve created on Shopify’s servers, allowing them to affect the cart and checkout at a fundamental level without the need of third-party apps or externally hosted plugins.

There are three different types of Shopify Scripts:

  1. Line item scripts affect line items in the cart and can change prices and grant discounts. Note: Theme modifications may be required to the show the discount amount or messages in the cart and checkout.
  2. Shipping scripts interact with shipping, and can rename, show, hide, or reorder shipping methods and grant discounts on shipping rates.
  3. Payment scripts interact with payments, and can rename, show, hide, or reorder payment gateways.

Note: payment scripts do not interact with payment gateways shown before the checkout.

Using this Repo

Within this repo you can:

1. Use examples as a template to build your own scripts

Scripts are organized into 3 categories within the repository:

  • Line Item
  • Shipping
  • Payment

When searching for a script to use, first open the relevant folder for the script type you are looking for. The README.md file will contain all the information about what the script does. The script file will be <scriptname.rb> - this is the file you will open and add into the Script Editor App on your store.

2. Submit a script to the community

When submitting a script:

  • Ensure you are submitting your script within the relevant folder
  • Create a new folder for your script and name it appropriately
  • Include a README.md file explaining what the script does and any other appropriate information using this format.
  • Include your script as a .rb file
  • Submit a Pull Request to have your script added to the repository

Script Creation Tools

There are tools out in the wild that aid in the creation of scripts based on plug and play variables. Jason Godson has created a Shopify Script Creator where you can generate scripts and manually paste them into your store. There are also tools like Disco Labs Playwright that can be integrated into your store as an app.

Scripts Ruby API & Liquid Variables

Shopify provides Ruby API documentation which is accessible via the Script Editor. This API is used to write the custom logic which runs in the checkout as is documented here:

This API differs from the Liquid variables exposed to scripts. These are used by scripts to communicate changes to the storefront and checkout. An example would be the message which appears next to a discount in the cart. This documentation can be found here.

Best Practices and Optimization

  • Only 1 script of each type can be published at a time. This often results in monolithic scripts which “do everything” the merchant needs. This in turn often leads to memory, CPU and character limitation issues. The solution to this is to refactor scripts to apply generic conditions based on parameters exposed to scripts, for example line item properties and tags. A script with hundreds of product IDs listed to enable bundle discounting should be refactored to look for a product tag instead.
  • Using Ruby’s safe navigation operator (&.) is a great idea to avoid “Nil” errors.
  • Minimise loops. Can you achieve what you’re trying to achieve without looping over all line items? If so, take that route.
  • Add code comments so that others can understand what the script does and can debug it.
  • Keep your code as DRY (Don’t Repeat Yourself) as possible, avoid repetition, and abstract away common functionality.
  • Test your scripts both in the editor and in the checkout preview.
  • Test your scripts with a large number of line items and varying line item quantities. This will avoid unexpected CPU or execution time errors.
  • Watch out for rounding issues. We have limited control over rounding in scripts and it’s not uncommon to find that a discount applied has caused a rounding issue at the line item level price or line price level. This can cause issues with ERPs and other integrations.
  • You can schedule Shopify Scripts to publish with Launchpad.

Noteworthy Limitations

  • Only 1 script of each type can be published at a time.
  • Script types (line item, shipping, payments) are tightly scoped which means variable availability can be an issue. For example, a customers chosen shipping method isn’t available in a payment script.
  • This tight scoping also results in the following common issues:
    • No access to metafields
    • No access to the collection(s) a product belongs to
    • No access to cart/checkout attributes
  • Regular expression pattern matching is not available in scripts which makes pattern matching a challenge. Date and time is also not available.
  • Shopify Scripts have memory, CPU, and character limitations imposed for security and performance reasons.
  • Scripts are limited to the Online Store channel only:
    • Scripts are not supported and do not fire in the wholesale channel checkout
    • Scripts do not fire when used in conjunction with the Buy Button
    • Scripts do not fire when sending a draft order invoice to a customer
    • Note: Scripts DO fire if using checkout permalinks
  • Scripts cannot display messaging in the checkout without modifications to checkout.liquid
  • Theme modifications are required to show the discount off of the original price and any messages in the cart

shopify-scripts's People

Contributors

byjpr avatar coleatkinson avatar jgodson avatar ljeremy avatar mtlexpos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

shopify-scripts's Issues

Variant FREE Gifts

How can this work so that only one variant of a product is free. Currently all variants of the product get added as free.

Bundles app - product object shows configurable products

Hi,

We have recently hit an issue with the Bundles App; when you view the JSON product object for the Bundle, there is no reference to any 'other products' in the bundle, just the (maximum of) 3 products with options.

This means the PDP (Shopify frontend product page) will only display 3 items - without manually adding those as a metafield object and hacking them in. Short of making a proxy app and requesting them via the API, there is no way to view the whole bundle.

Is there any plan to resolve this? The first question a client will ask is 'where's the rest? I only see part of the Bundle'

The cart transform and checkout displays the components correctly.

Shipping is free even when non tagged 'freeship' items are in the cart

Hi,

Thank you for your work.

Form the read me it seems as though the shipping price is only meant to change to free when all of the items in the cart are tagged with the "freeship" tag. This doesn't seem to be the case. When one tagged "freeship" item is in the cart then the shipping is changed to free regardless of whether there are other products in the cart that are not tagged with "freeship".

Scripts not saving or running 2nd time Error, Failed to run script... is shown

Problem

A script error is appearing when running a script twice: "Failed to run the script; refresh or try again later"

Scope

Identified on 2 stores

Examples

https://shinesty.myshopify.com/admin/apps/d25e45407e508f96409c2dd796e9bd95/scripts/6848527

https://shinesty.myshopify.com/admin/apps/d25e45407e508f96409c2dd796e9bd95/scripts/46923845

Screencast:
https://www.loom.com/share/25a6d9c71c214805a22c8fc306f56900

Expected vs Actual

Expected: Should save and run without error
Actual: Save shows error, 2nd run script also shows error. (workaround steps at bottom)

Replication Steps

  1. Adding a product to the cart input
  2. Clicking "Run Script"
  3. Clicking "Run Script" again will show the error above

This happens even on a script with nothing except Output.cart = Input.cart in the code

More Information

Workaround for now is:

When adding a product to the cart input, save the script first. Once it's saved, running the script multiple times will work

Zendesk ticket 1
Zendesk ticket 2

@Shopify/scripts

Allow scripts to use Theme Settings

A lot of merchants have requirements which exceed the features of the Wholesale Channel, so that's not an option for them.

However, when introduced to the Wholesale Channel, they love the easy configurability of the customer groups and discount options. Of course, once they see both, they want both.

In the interim, a great workaround would be to expose the Theme Settings attributes to Scripts, allowing the merchant to manage discounting tiers and values from a better UI, which we'd have access to in the Scripts.

Without this, they can only edit variables by digging into the code itself. As intuitive as we try to make this by putting it all in variables at the top of our scripts, it feels very stop-gap and makes the merchant nervous about having to dig into code, which is detrimental to their experience and confidence.

BuyOne_Load FX fail

Hi
When I use Script: BuyOne_Load FX chain on selected objects via file dialogue (guide inside).lua only half of the track is rendered. When I load FX Chain in the normal way it is fine.
Can you help? This is a recent problem Script worked fine unto now
I have the latest Reaper running Native on a MacMini M1 latest MacOs
Cheers
Andy

Scripts not working for checkout permalinks

I saw in the repo's Noteworthy Limitations here that Scripts DO fire if using checkout permalinks, but that doesn't seem to be the case.

I have a sample script here that updates the pricing of a specific product if it meets the criteria:

for line_item in Input.cart.line_items do
  if line_item.variant.skus.include?("KIT_SKU")
    if line_item.quantity > 2
      new_line_item = line_item.split(take: 2)
      new_line_item.change_line_price(Money.new(cents: 100) * 350, message: "2 Kit Bundle")
      Input.cart.line_items << new_line_item
    elsif line_item.quantity == 2
      line_item.change_line_price(Money.new(cents: 100) * 350, message: "2 Kit Bundle")
    end
  end
end

Output.cart = Input.cart

In the Script Editor, the Script had no errors and I was able to run the script and the result was as expected.

Can I get some help with this or confirm if Shopify Scripts are running for checkout permalinks? Thanks!

Take discount after all other discounts

I'm looking for a way to implement this script after all other discounts are taken.

For example, if Product A cost $200 and a coupon is used to save $50, I would like the script to discount this percentage off of the fully discounted price of $150, not on the full price of $200.

Is there a line I can add to accomplish this?

Scripts should run on the StoreFront API as well as other channels

Scripts are limited to the Online Store channel only:
Scripts are not supported and do not fire in the wholesale channel checkout
Scripts do not fire when used in conjunction with the Buy Button
Scripts do not fire when sending a draft order invoice to a customer
Note: Scripts DO fire if using checkout permalinks

Part of Shopify's appeal is the ability to leverage all the amazing OOB features that Shopify provides such as cart/checkout/order management. Yet, the inability to run Shopify Scripts on a Shopify Plus store's Storefront API channel is really misleading to how much the Storefront API can be used to create a complex custom storefront experience.

Why do the Shopify Scripts only run on the cart object?
Since the Storefront API skips the creation of a cart object and creates a checkout object, why can't Shopify allow the scripts to run on the checkout object for all channels besides the Online Store channel?

I would love to be able to leverage Shopify to its full capabilities.

Unable to duplicate scripts on one store "Failed to duplicate the script" error message

Problem

Unable to duplicate scripts on store, getting "Failed to duplicate the script" error message. Showing 422 error in Console

Scope

One store

Examples

Expected vs Actual

EB: Duplicating a script will create a duplicate

AB: Duplicating a script will result in an error message & 422 error

Replication Steps

  • Go to example script
  • Click Duplicate
  • See Failed to duplicate the script; refresh or try agin later message

More Information

  • Splunk Logs show 422 error but not much else from what I can see

Error shows for every script I tested on the merchants store
I can't replicate the error on my test store with existing scripts or when I copy the merchants script over to my store

Zendesk Ticket
@Shopify/scripts

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.