Offer Shopify automatic discounts only to specific customers
By Philip Dematis · 3/8/2023 · 5 minutes read![How to offer automatic discounts to specific customers on Shopify, without paying for an app](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fgimc8ubrwwpd%2F4BbwIGZLsPHnvmK2c012bD%2Fc38e1201781d269f020be7b257d6fb8f%2FFrame_4.jpg&w=3840&q=100)
Disclaimer: This guide uses custom theme code. Pasting code into a theme is not always guaranteed to work. For example: If you're using an unofficial theme, or other custom code, or apps are affecting normal theme functionality. In these cases it's possible that the new code won't be effective.
Shopify has several different ways you can offer discounts to your products. Compare-at price lets you display the discounted price next to the new price. Plus stores have Scripts and Checkout Functions to configure discounts to products and orders.
But the most frequent way is by using automatic discounts, and discount codes.
However, a very common question is: how do I create a discount, that automatically applies to specific customers?
Unfortunately, automatic discounts only apply based on the cart. This means that you can't offer an automatic discount to specific customers.
![Automatic discount page](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fgimc8ubrwwpd%2F7zU25OKuNMInFiN30Of2BM%2F961b028acafb0d77282d4116c9f82a4e%2Fimage.png&w=3840&q=90)
So, what's the solution?
Discount codes are the only other option. How do we use them to offer discounted prices without any manual action by the user?
This guide assumes you don't want to install and pay for an app. It only involves native Shopify features, and some custom code. Read some benefits of custom development vs. apps here.
Create a customer segment
First of all, you need to determine who you want to offer the discount to.
The most common occasion is when you want to offer it to customers with a specific tag.
This is a perfect use case for Customer segments.
What you need to do, is create a segment based on the tag. For example, if the customers are already tagged as "Loyalty", you can go into the Customer segments and create a filter: customer_tags CONTAINS 'Loyalty'
.
![Customer segment](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fgimc8ubrwwpd%2F44UaRsweehiOHAA1Szzf91%2Fe12229ef9b847ccf6dc5416bc73c9b9c%2Fimage.png&w=3840&q=90)
You can read more instructions about this here.
The plan
Discount codes, in general, need to be used on the checkout directly. But what if you don't want to make users type in the code, even if you could place it on a prominent banner?
![Checkout input for discount code](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fgimc8ubrwwpd%2F1UqZJ8RDlGVzn81GSEpCXM%2F5d12815f833950be9fd6af7c81ac8a17%2Fimage.png&w=3840&q=90)
Here's the answer: There are ways to programmatically apply discount codes, with some simple theme code. Here's the plan:
Create a discount code that gets applied to specific customer segments.
Add a small snippet to your theme, that automatically applies the discount code on checkout for your chosen customers.
Implement the custom automatic discount
1. Create the segment (read section 1) and discount code for the customers you want it to apply to.
![Create segment](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fgimc8ubrwwpd%2F1i5GZyQ5HuYHwVpwxga8Na%2Fa31e93b19bd9a9b7ba9ec9c4591e66b7%2Fimage.png&w=3840&q=90)
2. Select the "All customers segment again and tag the customers.
![Tag customers](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fgimc8ubrwwpd%2F1HFxIqd1qnrrU1dLp2EOBO%2Fb08fade1a93f216475fb0cb51e308a07%2Fimage.png&w=3840&q=90)
3. Create a duplicate of your theme, that you'll work on. This helps prevent issues with your live store.
![Duplicate theme](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fgimc8ubrwwpd%2F340kafwKEMIWVmctZkDnlV%2F97b8bc66c0942a28aaedc26c07026aa1%2Fimage.png&w=3840&q=90)
4. Open the theme's code, and locate the Snippets folder on the sidebar. Add a new snippet named "customer-discount". This will create a new Liquid file.
![Edit theme code](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fgimc8ubrwwpd%2F6BdJWVtTSLZuwnK0eTbfok%2F86bfc17ce72cd80d4ee53410d17774f0%2Fimage.png&w=3840&q=90)
5. Paste the below code into the code editor.
{% comment %}
Custom code: auto customer discount
Replace LOYALTY and LOYALTY10 with the customer's tag and discount code
{% endcomment %}
{% assign discount_customer_tag = 'LOYALTY' %}
{% assign discount_code = 'LOYALTY10' %}
{% if customer.tags contains discount_customer_tag %}
<script>
window.addEventListener('load', function() {
fetch("/discount/{{ discount_code }}")
})
</script>
{% endif %}
6. Replace the variables with your own tag and discount code, and save.
![Paste code](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fgimc8ubrwwpd%2F2cdAHD4kcP37Yn7u0eHCuO%2F94a7a4850932eca9ca9a5c03a0de9f26%2Fimage.png&w=3840&q=90)
7. From the sidebar, open the theme.liquid
file.
![Open theme.liquid](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fgimc8ubrwwpd%2F1IZUA0q6DMJJQ77bT7B0Vb%2F680b8d9d0f1443c07e07156265be895b%2Fimage.png&w=3840&q=90)
8. Scroll down to the bottom, find the </body>
tag, and insert the below code right before it, then save.
{% render 'customer-discount' %}
![Code editor](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fgimc8ubrwwpd%2F42hSVVfdLfyuKEs8Ad3go0%2Fc680856fd517b2d836cc30fba53ca47c%2Fimage.png&w=3840&q=90)
9. Click the "Preview store" button to test the feature on the new theme. I've found that checkout doesn't work if you're logged in as an admin, it's best if you get a Share Preview link and visit it on an incognito tab.
![Preview store](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fgimc8ubrwwpd%2F3BeBdiqVBfguRUXo9HpcAu%2F0769f6b9b53c7bd42ef8ab3aa880543d%2Fimage.png&w=3840&q=90)
10. Log in as a user that's tagged with the same tag, add a product to cart, and go to checkout. You should be seeing the discount code, automatically applied.
![Checkout](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fgimc8ubrwwpd%2F4nhFxMF7UhGXRigdyK3DVT%2F860219782f96e3191a6e10fce3ac07d6%2Fimage.png&w=3840&q=90)
11. Perform further testing. Trying to log out and back in, use other discount codes and automatic discounts, use any apps you have etc.
12. Publish the new theme. If there are changes on the live theme since you duplicated it, you will need to merge them.
![Publish theme](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fgimc8ubrwwpd%2F75O2Ul4EQmNnAwi9YUJrrG%2F26a8115225ef5f82c48b8986e17bc3d6%2Fimage.png&w=3840&q=90)
How it works
Here's what happens on the website now:
1. When a user opens a page on the site, the if
statement checks a) whether they're logged in, b) whether have the tag you set up.
2. If not, the new code doesn't load. If yes, the new code loads and gets executed.
3. All the code does, is it sends a request to the /discount/THE_DISCOUNT_CODE
endpoint of your store. This endpoint applies the discount in the background, and that's eventually visible on checkout. It's the same as when you get a shearable link of the code.
![Shearable discount link](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fgimc8ubrwwpd%2F1zNLDYlBuaZCJ2eehVgd5G%2F1ff601364bf5f11efdcfa95b839c1dd9%2Fimage.png&w=3840&q=90)
To keep things simple, and prevent issues with the user removing the code, logging out etc., the script runs every time a page loads. Impact to the site's loading speed is minimal, because the script runs only after the page has loaded.
Next steps
While this takes care of how a discount applies automatically, and contributes to an optimized Shopify checkout, there are further steps you may want to take. For example, you might want to display discounted prices throughout the site. These additions need code that depends on your theme, so they're impossible to include in this tutorial.
An alternative is to display a message on the site. (Read some tips on how to design something like that, here.) For example, "10% off for Loyalty members". This could even be added to a Custom Liquid section on your theme, like so:
{% if customer.tags contains discount_customer_tag %}
10% off for Loyalty members
{% endif %}
Thanks for reading! If you need help implementing this, feel free to reach out using the contact form below.