Shopify themes, liquid, logos, and UX
Is there any way using javascript on a liquid page that I can access the discount codes for my store?
Something like this:
<script>
/* Discount data */
var discounts = {{ discounts | json }}
</script>
Or something using Jquery?
Essentially I need to be able to grab the discount details from a discount code on a page in my shopify store.
Discount object is only accessable in notifications or in Order Print app
>>The discount object contains information about a discount, including its id, code, amount, savings, and type. Note that this object will display a value only if it's accessed in notifications or in the Order Printer app.
https://help.shopify.com/en/themes/liquid/objects/discount
So, you can't access it within theme.
Author: Hovhannes Sargsyan (aka sarhov)
Email: shopify@sarhov.com
Website: https://sarhov.com
Try Shopify free for 14 days,
You can get it from your cookies:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<div id="discount-message"></div>
<script type="text/javascript">
var codeCookieValue;
var getCookie = function(name) {
var value = "; " + document.cookie;
var parts = value.split('; '+name+'=');
if (parts.length == 2) return parts.pop().split(";").shift();
};
codeCookieValue = getCookie('discount_code');
</script>
<!-- end snippet -->
Old question, but I don't believe it was ever properly answered. I whipped up this function to be able to query the Shopify API to pull the entire discounts list, iterate through them to search for a specific discount (by discount code) and output the information accordingly. Hope this helps future devs!
async function queryDiscount(title) {
const response = await fetch(window.Shopify.routes.root + 'admin/price_rules.json');
const rules = await response.json();
$(rules.price_rules).each(function(n) {
if (this['title'] == title) {
console.log("Found Discount:\nID: " + this['id'] + "\nValue Type: " + this['value_type'] + "\nStart Date: " + this['starts_at'] + "\nEnd Date: " + this['ends_at'] + "\nEntitled Collection IDs: " + this['entitled_collection_ids']);
}
});
};
queryDiscount('SUMMERSALE10OFF');
Thank you, that works, but if you have automatic discount/price rules they won't be listed. do you know a way to get the automatic discounts, or is it only possible with the GraphQL api?
User | RANK |
---|---|
210 | |
166 | |
70 | |
55 | |
53 |
Make the shift from discounts to donations, and witness your business not only thrive fina...
By Holly Dec 4, 2023On our Shopify Expert Marketplace, you can find many trusted third party developers and fr...
By Arno Nov 27, 2023You've downloaded the Search & Discovery app from the Shopify App store, and as you're ...
By Skye Nov 8, 2023