Autofill Discount via URL - A Step by Step Walkthrough (eg. Auto Apply discount code using a Link!)

New Member
2 0 0

This thread has grown too large to take a conclusive code out. Can anyone volunteer to please put together a final version of a generic script on a gist? It will be highly appreciated. Thanks a bunch!

0 Likes
New Member
1 0 0

Any volunteers?  

 

Also woudl there be any differences on the parallax theme?

0 Likes
New Member
2 0 0

Hey@KR, @Kevin Seele,

Chris Cashwell already provided a final resolution only a couple posts up from you, and you can see the updated code here: https://gist.github.com/ccashwell/38cc80a977adaf1777fc6668bd438f28

@Chris Cashwell, + others who might be able to answer

Please note that I'm an extreme novice when it comes to web development. The only code(if you can even call it that) I write is in SQL, or VBA.

  • Is there a way to avoid the messaging stating that the discount code is not valid or has already been used if the value of the discount code = "MYDISCOUNT_CODE10"?

Thanks for everyone's time!

 

0 Likes
New Member
1 0 0

In the current era, it is important for a business to have a presence on the World Wide Web. Due to such a shift in trend, every small business is coming up with a website which would help them in promotional activities. Then why there is a need in designing and development of your website? 

In order to design and develop the websites, apart from using HTML and CSS codes, we also use different CMS (Content Management System) website development platforms like 
•    WordPress Development
•    Joomla! Development
•    osCommerce Development
•    Magento Development
•    Zen-Cart Development etc.
At www.webdesigncity.com.au before starting a project, a complete analysis is done and strategies are prepared so as to make out teamwork accordingly to deliver a flawless project. We make your website run flawlessly without any glitch as we provide a complete solution to website maintenance and support. We also guarantee success when it comes to ranking in the search engines by analyzing the keywords with respect to the project. Thus, steer your business towards fame, recognition, profitability and our team will help you to achieve the goals.

Web Design Sydney City is a professional name in the field of design and development in Australia and we help you create a online solution that is capable of reaching out to your target audience and creating a positive impact on them. http://www.webdesigncity.com.au/
0 Likes
New Member
2 0 0

I know many of you in this thread are JS wizards, but for those who are not:

There's a pretty powerful app that does this nicely (and has a free version):

https://apps.shopify.com/discount-url

If there's an app for it, why code it yourself right?

0 Likes
New Member
1 0 1

Hey everyone, I am new to shopify and just wanted a quick solution using Shopify's existing functionality.  What I wanted to do was pass a query string parameter called 'discount', and then just make a back-end call to shopify's discount link code, which will apply the discount.

So if you want to just pass a string like:

website.com/collections/?discount=DISCOUNTCODEHERE

I added the following before the </head> element in the theme.liquid file:

  <!-- CUSTOM DISCOUNT APPLICATOR -->
  <script>
    
    // Gets a parameter identified by name from the url query string
    function getParameterByName(name) {
    	var url = window.location.href;
    	name = name.replace(/[\[\]]/g, "\\$&");
    	var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    	if (!results) return null;
    	if (!results[2]) return '';
    	return decodeURIComponent(results[2].replace(/\+/g, " "));
	}
    
    var discount = getParameterByName('discount');
    if (discount && discount != "") {
    	var xmlHttp = new XMLHttpRequest();
      	xmlHttp.open( "GET", "https://" + window.location.hostname + "/discount/" + discount, false ); // false for synchronous request
    	xmlHttp.send( null );
    }
  </script>

Hope this helps!

1 Like
New Member
5 0 0

thx Alan  that is exactly what I was looking for and it worked first time

0 Likes
New Member
1 0 0

Can this code also work with Cart Drawer.

I am using the code here https://finalplusshop.myshopify.com/
when some one fill the subcription pop in Header a success popup come with discount code and "Continue Reading" apply the Discount code. If I go cart and Click on Checkout Discount apply automatically. But When someone click on shop Now the Drawer open then click on "Check out" discount code is not applying.

Can you please let me know if it possible the code work with Drawer as well.

0 Likes
New Member
1 0 0

@Chris_Cashwell  This is really a great solution!

 

I found I need to append ;path=/ to the cookie string to make the discount code work from all pages, which is the use case on our Shopify store(discount code on any URL in our store should work).

 

So, only one line change in the theme.liquid snippet for our use case. The snippet for cart.liquid remains the same.

 

<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] + ";path=/";
    }
  })();
</script>
0 Likes