Reputation: 119
IN SHOPIFY STORE: How to add discount code in URL without redirect with javascript. Having a parameter in the url with the discount code.
Upvotes: 1
Views: 1845
Reputation: 1
Shopify actually uses cookie discount_code. All you need to do is deploy the following script via GTM on all pages. It will grab discount url param and store it to discount_code cookie. Shopify will pick it up automatically.
<script>
(function() {
var params = new URLSearchParams(window.location.search);
if (params.has('discount')) {
var discountCode = params.get('discount');
document.cookie = "discount_code=" + encodeURIComponent(discountCode) + "; path=/; max-age=" + (30*24*60*60);
}
})();
</script>
Upvotes: 0
Reputation: 119
Every url in your website with the parameter discout will be activated in the checkout.
EXAMPLE OF USE
your discount code is DISCOUNTCODE1
www.myshopifywebsite.com/products/product1?discount=DISCOUNTCODE1
or
www.myshopifywebsite.com?discount=DISCOUNTCODE1
STEP 1
/* Put this in theme.liquid, preferably right before "</body>" inside a script tag */
//this code set the cookie
(function() {
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
var product = urlParams.get('discount');
if (product != null && product.length > 1) {
document.cookie = 'discount=' + product + ';path=/';
}
})();
STEP 2
//Insert this code in cart-template.liquid or cart.liquid at the bottom of the page inside script tag
//Also, make sure your cart's "<form>" has an ID of "cartform".
/*Function to getcookie*/
function getCookie(nome) {
var name = nome + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ')
c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
}
return "";
}
(function() {
var discountCookie = getCookie('discount');
if (discountCookie != null && discountCookie.length > 1) {
document.getElementById('cart_form').action = '/cart?discount=' + discountCookie;
}
})();
STEP 3
//Insert this code in header.liquid (for reciving discount also in a product page), preferably at the bottom of the page inside script tag
//Also, make sure your chechout "<form>" has an ID of "checkoutgsdr".
/*Function to getcookie*/
function getCookie(nome) {
var name = nome + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ')
c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
}
return "";
}
(function() {
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
var product = urlParams.get('discount');
if (product == null || product.length <= 1) {
var discountCookie = getCookie('discount');
if (discountCookie != null && discountCookie.length > 1) {
document.getElementById('checkoutgsdr').action = '/checkout?discount=' + discountCookie;
}
}else{
document.getElementById('checkoutgsdr').action = '/checkout?discount=' + product;
}
})();
Upvotes: 1