Skip to content

Instantly share code, notes, and snippets.

@atikju
Created October 24, 2018 20:45
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save atikju/515bf0e675b7f6fb236369be48930fba to your computer and use it in GitHub Desktop.
Save atikju/515bf0e675b7f6fb236369be48930fba to your computer and use it in GitHub Desktop.
Shopify - Apply Discount / Coupon / Promo Code on cart page
<div class="cart-promo">
<h2>ENTER A PROMO CODE</h2>
<input type="text" id="devPromo">
<a href="/checkout?discount=none" id="redemDevPromo">Apply Coupon</a>
</div>
<script>
$(document).ready(function(){
//listen to the promo button click
$('#redemDevPromo').on('click', function(event){
//disable the button event
event.preventDefault();
//write the url format
var theUrl = '/checkout?discount=';
//grab the discount code from the input
var theDiscount = $('#devPromo').val();
//full url to redirect to checkout with promo code
var toRedirect = theUrl+theDiscount;
console.log(toRedirect);
//redirect
window.location.href = toRedirect;
});
});
</script>
@arpanjain100
Copy link

This works but there is an error at the time of redirection. Page waits for about 2 seconds on an error page of Shopify and then continues to redirect.

@atikju
Copy link
Author

atikju commented Apr 4, 2020

It shouldn’t be like that. Works fine for me!

@kaitoxdd
Copy link

kaitoxdd commented May 4, 2020

Hello, can you tell me how do I apply this to my theme? I tried putting the codes into a snippet file and called it on the cart page, but the javascript isn't working (Uncaught ReferenceError: $ is not defined) so it cannot get discount input.

@atikju
Copy link
Author

atikju commented May 4, 2020

Add the JS code in your theme.liquid right before the body tag ends. It'll work fine then.

@kaitoxdd
Copy link

kaitoxdd commented May 5, 2020

Thanks! Works fine for me now!

@penprog
Copy link

penprog commented Sep 10, 2020

Unfortunately did not work with me

@AnuragAnalog
Copy link

How to use it?

@secureweb16
Copy link

secureweb16 commented Oct 27, 2020

it's not good solution for me ,when i will put discount code on this and click on apply then it will redirect to checkout page, i need to apply discount on cart page not a checkout page,if anyone know how to do that please let me know

@tams1985
Copy link

tams1985 commented Nov 4, 2020

There's a Shopify app to exactly do that: [Voila - Discount on Cart] (https://apps.shopify.com/available-discount-coupon-list-on-cart-page)

You can also show selected coupon code on the cart page with this.

@MunirYounos
Copy link

The code takes you to the checkout page, I need the discount code to be effective on the cart page and calculate the total price...can you make something like that ...

@BossBabePremade
Copy link

Hi! This code is amazing. I do have a request if you are still there? Is it possible the discount can be updated on the same page instead of redirecting to the checkout page? This way my customers can just choose paypal? Thank you!

@SheetalSharma01
Copy link

Hii, The discount code is not applied to the checkout, plus it is also showing the error in the checkout page. Could you please suggest me something to correct this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment