Get a Call Back

Shopify, one of the leading e-commerce platforms, empowers merchants to boost sales and customer engagement through various features, and one such powerful tool is the Discount system. Knowledge in Shopify functions to create discounts can significantly impact your store's success. In this article, we'll delve into the technical aspects of Shopify's discount functionality, exploring the different types of discounts and providing code examples for implementation.
Types of Shopify Functions to Create Discount
1. Percentage Discounts
Percentage discounts are a popular choice for enticing customers with a percentage off their total purchase. This can be achieved using the discount code and percentage parameters in the Shopify API.
{% assign discount_code = 'SUMMER20' %}
{% assign percentage = 20 %}
{% if discount_code %}
{% if discount_code == checkout.discount_code %}
{% assign discount = checkout.total_price * percentage / 100 %}
{% assign discounted_price = checkout.total_price - discount %}
Your discounted price is ${{ discounted_price | money }}.
{% else %}
Invalid discount code.
{% endif %}
{% else %}
No discount code applied.
{% endif %}
In this example, the discount_code variable holds the code customers enter, and the percentage variable specifies the discount percentage. The code then checks if the entered discount code matches and applies the discount accordingly.
2. Fixed Amount Discounts
If you prefer fixed amount Shopify functions to create discounts , you can utilize the fixed_amount parameter in the Shopify API.
{% assign discount_code = 'SAVE10' %}
{% assign fixed_amount = 10 %}
{% if discount_code %}
{% if discount_code == checkout.discount_code %}
{% assign discounted_price = checkout.total_price - fixed_amount %}
Your discounted price is ${{ discounted_price | money }}.
{% else %}
Invalid discount code.
{% endif %}
{% else %}
No discount code applied.
{% endif %}
This code snippet works similarly to the percentage discount example, but it subtracts a fixed amount (fixed_amount) instead of a percentage.
3. Free Shipping
Offering free shipping can be a powerful incentive. To implement this, set the free_shipping parameter to true in the Shopify API.
{% assign free_shipping = true %}
{% if free_shipping %}
Free shipping on your order!
{% else %}
Standard shipping rates apply.
{% endif %}
This example checks whether free shipping is enabled and displays a corresponding message. Keep reading this informative blog to know other Shopify functions to create discounts.
Advanced Shopify Functions to Create Discount
1. Conditional Discounts
You might want to apply discounts based on certain conditions, such as the total order value or specific products in the cart. Here's an example using Liquid, Shopify's templating language:
{% assign order_total = checkout.total_price %}
{% assign discount_threshold = 100 %}
{% assign discount_percentage = 10 %}
{% if order_total >= discount_threshold %}
{% assign discount = order_total * discount_percentage / 100 %}
{% assign discounted_price = order_total - discount %}
Enjoy a {{ discount_percentage }}% discount on your order!
{% else %}
Spend ${{ discount_threshold | minus: order_total | money }} more to unlock a {{ discount_percentage }}% discount!
{% endif %}
This code checks if the order total exceeds a certain threshold and applies a percentage discount accordingly.
2. Limited-Time Discounts
Creating discounts with a limited timeframe is another effective function among all Shopify functions to create Discounts. Shopify provides the starts_at and ends_at parameters for this purpose.
{% assign current_time = 'now' | date: '%s' %}
{% assign discount_starts_at = '2023-01-01' | date: '%s' %}
{% assign discount_ends_at = '2023-01-15' | date: '%s' %}
{% if current_time >= discount_starts_at and current_time <= discount_ends_at %}
Hurry! Limited-time offer: 20% off with code 'LIMITED20'.
{% else %}
Check back for future promotions!
{% endif %}
Adjust the discount_starts_at and discount_ends_at variables to set the desired time frame.
Implementing Discounts Programmatically
Shopify provides a powerful API that allows programmatic creation and management of Shopify functions to create Discount. Below is an example using the Shopify API in Python to create a percentage discount:
import shopify
shopify.ShopifyResource.set_site("https://your-shop-name.myshopify.com/admin")
discount = shopify.Discount.create(
code="SUMMER20",
discount_type="percentage",
value=20,
applies_to_id="all",
starts_at="2023-06-01T00:00:00Z",
ends_at="2023-08-31T23:59:59Z",
usage_limit=100,
)
print("Discount created:", discount)
This Python script utilizes the Shopify Python API library to create a percentage discount with the code 'SUMMER20', applicable to all products, starting on June 1, 2023, and ending on August 31, 2023, with a usage limit of 100.
Advanced Discount Strategies and Implementation
3. Tiered Discounts
Encourage customers to spend more by implementing tiered discounts based on the total order value. This approach rewards larger purchases with higher percentage discounts. The Liquid code snippet below demonstrates how to achieve this:
{% assign order_total = checkout.total_price %}
{% assign tier1_threshold = 50 %}
{% assign tier1_discount = 10 %}
{% assign tier2_threshold = 100 %}
{% assign tier2_discount = 15 %}
{% if order_total >= tier2_threshold %}
{% assign discount = order_total * tier2_discount / 100 %}
{% assign discounted_price = order_total - discount %}
Enjoy a {{ tier2_discount }}% discount on your order!
{% elsif order_total >= tier1_threshold %}
{% assign discount = order_total * tier1_discount / 100 %}
{% assign discounted_price = order_total - discount %}
Spend ${{ tier2_threshold | minus: order_total | money }} more to unlock a {{ tier2_discount }}% discount!
{% else %}
Spend ${{ tier1_threshold | minus: order_total | money }} more to unlock a {{ tier1_discount }}% discount!
{% endif %}
This code of Shopify functions to create discounts establishes two tiers of discounts based on the order total, providing customers with a higher percentage discount for reaching a higher spending threshold.
4. Combination Discounts
Create compelling offers by combining different types of discounts. For instance, you can offer a fixed amount discount along with free shipping for orders over a certain value. The Liquid code snippet below illustrates this concept:
{% assign order_total = checkout.total_price %}
{% assign free_shipping_threshold = 75 %}
{% assign fixed_amount_discount = 10 %}
{% if order_total >= free_shipping_threshold %}
{% assign discounted_price = order_total - fixed_amount_discount %}
Enjoy ${{ fixed_amount_discount | money }} off and free shipping on your order!
{% else %}
Spend ${{ free_shipping_threshold | minus: order_total | money }} more to qualify for a ${{ fixed_amount_discount | money }} discount and free shipping!
{% endif %}
By combining Shopify functions to create Discount, you provide customers with added value, making their shopping experience more enticing.
5. Implementing Discounts Programmatically with Python
Building on our Python example, let's explore creating tiered discounts programmatically using the Shopify API:
import shopify
shopify.ShopifyResource.set_site("https://your-shop-name.myshopify.com/admin")
def create_tiered_discount(code, thresholds, discounts, starts_at, ends_at, usage_limit):
for i in range(len(thresholds)):
discount = shopify.Discount.create(
code=f"{code}{i+1}",
discount_type="percentage",
value=discounts[i],
applies_to_id="all",
starts_at=starts_at,
ends_at=ends_at,
usage_limit=usage_limit,
)
print(f"Tier {i+1} Discount created:", discount)
create_tiered_discount(
code="TIERED",
thresholds=[50, 100],
discounts=[10, 15],
starts_at="2023-01-01T00:00:00Z",
ends_at="2023-12-31T23:59:59Z",
usage_limit=100,
)
This Python script creates tiered percentage discounts with different codes, thresholds, and discounts, allowing you to apply a dynamic pricing strategy based on customer spending.
Conclusion
Shopify functions to create discounts provide a versatile toolkit for store owners to attract and retain customers. By understanding the technical aspects and leveraging the Shopify API, you can implement a variety of discount strategies tailored to your business needs. Whether it's percentage discounts, fixed amount discounts, conditional offers, or limited-time promotions, Shopify empowers you to take your e-commerce game to the next level.
Get a Call Back