Skip to content
nive.sh

Loading index…

↑↓ navigate · ↵ opennive.sh

[Shopify Script] 2 or more variants within the same product ID get 10% off

In this article, we are going to create a Shopify Script that gives 10% discount on the line items having at least 2 quantities of same product.

Nivesh Saharan

· 1 min read · 198 words

Introduction

In this article, we are going to create a Shopify Script that gives 10% discount on the line items having at least 2 quantities of same product.

Requirements

2 or more variants within the same product ID get 10% off

Case 1:

  • Product A Variant 1
  • Product A Variant 2
  • Product A Variant 3

  Total 10% off

Case 2

  • Product A Variant 1
  • Product A Variant 2

  Total 10% off

Case 3

  • Product A Variant 1
  • Product A Variant 2
  • Product B Variant 1

  Only Product A (Variant 1+2) 10% off, not Product B

Case 4

  • Product A Variant 1
  • Product B Variant 1

  No discount

Final code

# Settings
DISCOUNT_PERCENTAGE = 10;
DISCOUNT_MESSAGE = "Bulk Discount";
MINIMUM_QUANTITIES = 2
 
# Code starts here
products_by_quantity = {};
 
Input.cart.line_items.each do |line_item|
  product = line_item.variant.product
  next if product.gift_card?
 
  if products_by_quantity[product.id]
    products_by_quantity[product.id] += line_item.quantity;
  else
    products_by_quantity[product.id] = line_item.quantity;
  end
end
 
Input.cart.line_items.each do |line_item|
  product = line_item.variant.product
  next if product.gift_card?
 
  if products_by_quantity[product.id] >= MINIMUM_QUANTITIES
    line_item.change_line_price(line_item.line_price * ((100 - DISCOUNT_PERCENTAGE) / 100), message: DISCOUNT_MESSAGE);
  end
end
 
Output.cart = Input.cart
 

Keep reading

Related articles

All articles

Jul 17, 2020 · 5 min read

Different types of Shopify apps

In this lesson, we will learn about four types (Public, private, custom and draft apps) of Shopify apps and different characteristics for each type of app.

  • shopify

Jun 27, 2023 · 5 min read

Arrays and Tuples

There are two primitive compound types in Rust: Arrays and Tuples.

  • JavaScript
  • Rust

Jun 26, 2023 · 4 min read

A Practical guide to use Arrays and Objects in JavaScript

JavaScript objects and arrays are fundamental data structures used to organize and store data. In this comprehensive guide, we will explore the concepts of objects and arrays, provide relatable examples for both technical and non-technical individuals, and delve into safe techniques for copying and modifying these data structures. By following the discussed techniques, you can ensure data integrity, avoid unintended side effects, and confidently work with JavaScript data.

  • javascript

Newsletter

Build notes, when there is something worth sending.

New deep-dive articles and the occasional note on what I am shipping. No schedule, no filler, unsubscribe any time.

Taking on new projects

Need help with shopify engineering and customization?

Tell me about the storefront, product or platform you’re working on. A 30-minute call is usually enough to map the first steps.

Email

hey@nive.sh

Local time

--:-- · Gurugram (IST)