Shopify: Coupon auto apply links

So sometimes you just want to link to your shopify store and have a coupon automatically apply from the link.

I sent out about 800 unique coupons yesterday. I used Shopifys Bulk Discounts app (which is awesome) to generate 800 unique coupon codes with the same rules. The coupon codes it puts out are rather long and random though, so instead of just asking people to copy/paste the coupon, we wanted to have a button which linked to the site and applied the coupon.

There are a few Shopify apps that might do it for about $9 a month. Not 100% sure if they do the trick as I didn’t try them.

But there is a way to do it without apps and with a few easy lines of code.

There are other snippets like this around the net, but thanks to some regex help from my buddy @dtbaker, this one accepts hyphens, which is what you get with the bulk discounts app.

Edit your shopify theme code then add this code. See comments below for instruction.

this snippet goes into theme.liquid

<script>
 /* Put this in theme.liquid, preferably right before "</body>" */
 (function() {
 var discountParam = document.location.search.match(/discount=([\w-]+)/);
 if (discountParam && discountParam.length > 1) {
 document.cookie = discountParam[0];
 }
 })();
 </script>

and this one in the cart.liquid.

<script>
 /*
 Put this in cart.liquid, preferably at the bottom of the file.
 Also, make sure your cart's "<form>" has an ID of "cartform".
 */
 (function() {
 var discountCookie = document.cookie.match(/discount=([\w-]+)/);
 if (discountCookie && discountCookie.length > 1) {
 document.getElementById('cart-form').action = '/cart?' + discountCookie[0];
 }
 })();
 </script>

If you found this useful please leave a comment 🙂

Previous Post
By
Digital Designer / WordPress Theme Developer

Leave a Reply

Your email address will not be published. Required fields are marked *