Everything About Shopify Scripts

Statistics show that most internet customers leave their carts empty before purchasing. This figure highlights how crucial it is to optimize every area of your online store to boost conversions. Shopify Scripts can assist you in achieving this goal. With these flexible scripts, you can automate discounts, streamline the checkout process, and customize the shopping experience to reduce cart abandonment rate significantly. This blog will cover Shopify Scripts, their types, what you can do with them in the Shopify Script Editor, and some must-know Shopify Script examples that can enhance your online store’s functionality.

What are Shopify Scripts?

Shopify scripts are a potent tool that opens up many features that let retailers personalize and improve checkout experience. Merchants can simply conduct promotions with or without codes, offer customized discounts, customize shipping options, and modify payment options at checkout by utilizing the code snippets of Shopify Scripts. Customers can enjoy a unique shopping experience by creating their own customized online ecommerce store.

The Shopify script makes it easy to apply a discount automatically when a customer adds a certain combination of products to their cart. The flexibility of these tools makes them essential for any eCommerce business looking to optimize its store.

Types of Shopify Script

Types of Shopify Script

There are three primary categories of Shopify script, and each has varying levels of customization:

  • Line Item Scripts
  • Shipping Scripts
  • Payment Scripts
  • Line Item Scripts

    Line Item Scripts lets you assign discounts to individual products or modify their prices depending on various criteria. These scripts can be used to implement Buy One, Get One (BOGO) promotions, offer percentage-based discounts, or make bundle sales. Using Line Item Scripts can help you automate complex discount structures and guarantee that promotions are implemented consistently, improving customer satisfaction and driving sales.

    Shipping Scripts

    The Shipping Scripts allows you to automate and customize shipping options based on the cart value, customer location, or any other criteria you specify. Based on the cart value, you can set up rules for free shipping based on the customer’s location and hide or show different shipping methods. The ability to customize the shipping experience based on customer expectations and company requirements can help reduce cart abandonment rates.

    Payment Scripts

    With Payment Scripts, you can modify the available payment methods at checkout and provide incentives to use specific payment methods. You can apply discounts when customers choose a particular payment method or restrict payment options based on factors like the country or cart amount. Payment scripts can improve conversion rates and user experience by expediting and promoting specific payment behaviours.

    Shopify Script Editor

    You can create custom scripts within the Shopify Script Editor for Shopify Plus merchants so that they can automate and customize various aspects of your online store. There are several advantages to Shopify’s script editor, including-

  • During checkout, discounts are automatically applied to particular products or collections.
  • You can offer tiered pricing based on the quantity of items a customer has in their shopping cart.
  • Creates successful loyalty programs.
  • Create dynamic pricing or promotions that adjust based on customer behaviour.
  • Delivers a personalized customer experience by customizing Shopify’s additional scripts
  • How to use Scripts in Shopify Script Editor

    shopify scripts examples

    Shopify Scripts are easy to create. You can change the scripts to your preference by using the provided templates. To add script to Shopify store, follow the following actions-

  • To get started, you need to go to the Shopify admin and then open the Script Editor. Once you click “Create Script,” you will be presented with a dialogue box where you can choose a “Script template“. Choose a category that fits your needs-line items, shipping rates, or payment gateways.
  • If you don’t have a template yet, select “Blank Template” or using an example template. By tailoring the script to your needs, you can make it fit your needs exactly.
  • Click “Create Script” to begin building within the selected category. Enter a descriptive title into the Title box.
  • On the left side of the screen, select “Code” which opens the Ruby source code console. In this section, you will input the script code. You can use the documentation for the Shopify API Scripts to direct your coding process.
  • After entering your code, select “Run Script” to execute it. Once you’ve reviewed your script and are satisfied with the outcome, you have the option to either “Save Draft” (if you wish to keep the script unpublished for further editing) or “Save and Publish” (to finalize and publish your new script).
  • Examples of Shopify Scripts

    Below are some well-known Shopify Script examples that can greatly enhance your online ecommerce store to get you started:

    Buy One, Get One Free

    A widely popular discount strategy is Buy One Get One (BOGO) free. Implementing these offers significantly increases your chances of converting casual browsers into committed buyers. With Shopify script, you can automate this process seamlessly, adding the complimentary item to the cart whenever a qualifying product is selected. Providing customers with unexpected perks can encourage them to return to your store.

    Example Scenario:

    When a customer adds a qualifying T-shirt to their cart, a second T-shirt is added as a free gift. Removing or reducing the quantity of qualifying T-shirts adjusts the number of free gifts accordingly. For example, adding three T-shirts results in three free T-shirts, but removing any will reduce the free T-shirts to match the new quantity.

    Create a new script, select Line Item Script, and paste the following code:

    # Buy One, Get One Free Script
    # Set the qualifying product ID and variant ID for the BOGO promotion
    QUALIFYING_PRODUCT_ID = 1234567890  # Replace with your qualifying product ID
    QUALIFYING_VARIANT_ID = 9876543210  # Replace with your qualifying variant ID
    # Set the product ID and variant ID of the free gift (same product or a different one)
    FREE_PRODUCT_ID = 1234567890  # Replace with your free product ID
    FREE_VARIANT_ID = 9876543210  # Replace with your free variant ID
    
    # Loop through the cart items to apply the promotion
    Input.cart.line_items.each do |line_item|
      # Check if the product in the cart is the qualifying product
      if line_item.variant.product.id == QUALIFYING_PRODUCT_ID && line_item.variant.id == QUALIFYING_VARIANT_ID
        # Calculate the number of free items to add (one for each qualifying item)
        free_quantity = line_item.quantity
    
        # Check if the free product is already in the cart
        free_item = Input.cart.line_items.find do |item|
          item.variant.id == FREE_VARIANT_ID
        end
    
        if free_item
          # Update the quantity of the free item if it's already in the cart
          free_item.quantity = free_quantity
          free_item.line_price = Money.zero
          free_item.on_sale = true
        else
          # Add the free product to the cart
          variant = Variant.find(FREE_VARIANT_ID)
          free_item = Input.cart.add_line_item(variant, free_quantity)
          free_item.line_price = Money.zero
          free_item.on_sale = true
          free_item.change_line_price(Money.zero, message: "Buy One, Get One Free!")
        end
      end
    end
    
    # Output the modified cart
    Output.cart = Input.cart

    Steps to Customize:

    Product IDs: Replace the QUALIFYING_PRODUCT_ID, QUALIFYING_VARIANT_ID, FREE_PRODUCT_ID, and FREE_VARIANT_ID with the actual product and variant IDs from your store.
    Quantity: The script adds one free product for each qualifying item in the cart. If you want to adjust the quantity (e.g., buy two, get one free), modify the free_quantity logic.

    Free Gift with Purchase

    One excellent way to get customers to add additional things to their shopping carts is by using the Free Gift script. The script is great for offering discounts on specific products and for line items as well. Consequently, customers who are open to receiving a gift will be more likely to choose other products from your store. There is nothing better than getting things for free, and many shoppers choose free items over discounts. This Shopify script allows you to create custom rules for spending and reward shoppers who meet those conditions.

    Example Scenario:
    If the minimum spend is set to $50, and a customer adds products to their cart totaling $60, the free gift will be automatically added. If the cart total drops below $50, the free gift will be removed.
    Create a new script, select Line Item Script, and paste the following code:

    # Free Gift with Purchase Script
    
    # Set the minimum spend amount to qualify for the free gift
    MINIMUM_SPEND = Money.new(cents: 5000)  # Change this to your desired amount, e.g., $50 = 5000 cents
    
    # Set the product ID and variant ID of the free gift
    FREE_GIFT_PRODUCT_ID = 1234567890  # Replace with your free gift product ID
    FREE_GIFT_VARIANT_ID = 9876543210  # Replace with your free gift variant ID
    
    # Calculate the cart subtotal
    cart_subtotal = Input.cart.subtotal_price
    
    # Check if the cart qualifies for the free gift
    if cart_subtotal >= MINIMUM_SPEND
      # Check if the free gift is already in the cart
      free_gift_line_item = Input.cart.line_items.find do |line_item|
        line_item.variant.product.id == FREE_GIFT_PRODUCT_ID
      end
    
      # Add the free gift if it's not already in the cart
      if free_gift_line_item.nil?
        variant = Variant.find(FREE_GIFT_VARIANT_ID)
        free_gift = Input.cart.add_line_item(variant, 1)
        free_gift.line_price = Money.zero
        free_gift.on_sale = true
        free_gift.change_line_price(Money.zero, message: "Free gift with purchase!")
      end
    else
      # Remove the free gift if the cart no longer qualifies
      Input.cart.line_items.each do |line_item|
        if line_item.variant.product.id == FREE_GIFT_PRODUCT_ID
          Input.cart.remove_line_item(line_item)
        end
      end
    end
    
    # Output the modified cart
    Output.cart = Input.cart

    Steps to Customize:

    Free Gift Product ID: Replace FREE_GIFT_PRODUCT_ID and FREE_GIFT_VARIANT_ID with the IDs of the product you want to offer as a gift.
    Minimum Spend Amount: Adjust the MINIMUM_SPEND value to the desired amount customers must spend to receive the gift.
    Gift Quantity: By default, the script adds one free gift per qualifying cart. If you want to add multiple gifts based on certain conditions, you can modify the script accordingly.

    Bundle Discount

    This is categorized as a line item script. You can use a bundle discount script to give customers a discount when they purchase a number of related products together. These scripts are used to encourage customers to purchase more by only applying them to certain products in the cart. Consider offering a 20% discount on t-shirts, jeans, and jackets in a cart if someone adds all three items. The goal of this strategy is not only to boost sales by encouraging customers to buy more, but also to help clear inventory as fast as possible. Such discounts can increase order values and make customers feel like they’re getting a better deal, which can encourage higher customer satisfaction and repeat business.

    Example Scenario:

    If your bundle includes a T-shirt, a hat, and a pair of socks, and you want to offer a 20% discount when all three are in the cart, the script will check if the customer has all three items and then apply the discount automatically.

    Create a new script, select Line Item Script, and paste the following code:

    # Bundle Discount Script
    # Define the bundle products and the discount percentage
    BUNDLE_PRODUCTS = [
      { product_id: 1234567890, variant_id: 1111111111 }, # Replace with product 1 ID and variant ID
      { product_id: 2234567890, variant_id: 2222222222 }, # Replace with product 2 ID and variant ID
      { product_id: 3234567890, variant_id: 3333333333 }  # Replace with product 3 ID and variant ID
    ]
    DISCOUNT_PERCENTAGE = 20  # Set the discount percentage for the bundle
    # Calculate the total quantity of bundle items in the cart
    bundle_items_in_cart = BUNDLE_PRODUCTS.map do |bundle_item|
      Input.cart.line_items.select do |line_item|
        line_item.variant.product.id == bundle_item[:product_id] &&
        line_item.variant.id == bundle_item[:variant_id]
      end.sum(&:quantity)
    end
    # Find the minimum quantity of any item in the bundle (this determines how many full bundles are in the cart)
    bundle_count = bundle_items_in_cart.min
    # Apply discount if a full bundle is present in the cart
    if bundle_count > 0
      Input.cart.line_items.each do |line_item|
        BUNDLE_PRODUCTS.each do |bundle_item|
          if line_item.variant.product.id == bundle_item[:product_id] &&
             line_item.variant.id == bundle_item[:variant_id]
            # Apply the discount to the line item
            discount = line_item.line_price * (DISCOUNT_PERCENTAGE.to_f / 100.0)
            line_item.change_line_price(line_item.line_price - discount, message: "Bundle Discount")
          end
        end
      end
    end
    # Output the modified cart
    Output.cart = Input.cart

    Steps to Customize:

    Product IDs: Replace the product and variant IDs in the BUNDLE_PRODUCTS array with your actual bundle products.
    Discount Percentage: Adjust the DISCOUNT_PERCENTAGE variable to the discount amount you want to offer.
    Bundle Composition: You can modify the BUNDLE_PRODUCTS array to include as many or as few products as needed for your bundle.

    Tiered Discounts

    By offering tiered discounts, you can create buzz about your merchandise and give customers the opportunity to save even more when they make purchases from you. Incremental discounts apply as total cart value increases. The goal of this strategy is to encourage customers to spend more to unlock bigger discounts. Using Shopify script, you may add tiered of discounts to the checkout page. These scripts allow you to let customers know that the more items they add to their cart, the more they save.

    Example Scenario:

    If a customer spends $250, the script will apply a 15% discount based on the configured tier. If they spend $350, a 20% discount will be applied instead.

    Create a new script, select Line Item Script, and paste the following code:

    # Tiered Discounts Script
    # Define the tiers for the discounts
    TIERS = [
      { threshold: Money.new(cents: 10000), discount: 0.10 },  # Spend $100 or more, get 10% off
      { threshold: Money.new(cents: 20000), discount: 0.15 },  # Spend $200 or more, get 15% off
      { threshold: Money.new(cents: 30000), discount: 0.20 }   # Spend $300 or more, get 20% off
    ]
    
    # Calculate the cart subtotal
    cart_subtotal = Input.cart.subtotal_price
    # Determine the applicable discount based on the subtotal
    applicable_discount = TIERS.reverse.find { |tier| cart_subtotal >= tier[:threshold] }
    # Apply the discount if a tier is met
    if applicable_discount
      Input.cart.line_items.each do |line_item|
        discount_amount = line_item.line_price * applicable_discount[:discount]
        line_item.change_line_price(line_item.line_price - discount_amount, message: "Tiered Discount")
      end
    end
    # Output the modified cart
    Output.cart = Input.cart

    Example Tiers:

    Spend $100, get 10% off
    Spend $200, get 15% off
    Spend $300, get 20% off

    You can customize the TIERS array to reflect your own discount levels by adjusting the threshold values (in cents) and the discount percentages.

    Steps to Customize:

    Discount Tiers: Modify the TIERS array to set your own discount levels. For example, if you want a 5% discount for spending $50, you’d add { threshold: Money.new(cents: 5000), discount: 0.05 }.
    Threshold Amounts: Adjust the threshold values to match your desired minimum cart subtotal for each discount tier.
    Discount Percentages: Change the discount values to the appropriate percentage for each tier.

    The following are some of the most common Shopify script use cases. If you prefer not to use scripts, there are numerous Shopify apps, such as iCart Cart Drawer Cart Upsell, that allow you to implement these promotional strategies directly in your store. Adding this to your store enhances its functionality and offers enticing deals to your customers without the need for custom code.

    Final Thought

    Any online retailer looking to improve the customer experience will benefit greatly from Shopify Scripts. You can automate and personalize ecommerce operations by leveraging different types of scripts. When you integrate these scripts into your ecommerce strategy, you’ll likely see an increase in customer satisfaction and overall sales as a result, making sure that your business remains competitive in a market that’s highly competitive.

    Bidisha Saha

    About the author

    Step into the content world with Bidisha Saha, a seasoned Senior Content Writer at Identixweb. Passionate about e-commerce, technology, and marketing, she optimizes online visibility, crafting engaging content for business growth. Join the journey through words and innovation with Bidisha.

    Leave a Reply

    Your email address will not be published. Required fields are marked *