Populating Transaction Data via URL Parameters

Last modified July 31, 2017

If you use the Store Builder Library (SBL) on your website, you can optionally populate certain pieces of transaction data via URL parameters - i.e., values passed as part of the URL customers use to access the store page - by adding JavaScript code to parse the URL and apply the parameter values to SBL functions. This is possible for any transaction data that can be applied via SBL functions, including:

  • Products
  • Customer First Name
  • Customer Last Name
  • Coupon Code

 The following example would be implemented near the bottom of your page, just after the closing </body> tag.

Note

The following example is provided "as is" and is intended for illustration purposes. You may need to adjust the code provided in the example to meet your needs or work with your specific implementation.
Example of populating transaction data via URL parameters
<script>
    var QueryString = function () {
  // This function is anonymous, is executed immediately and 
  // the return value is assigned to QueryString!
  var query_string = {};
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
        // If first entry with this name
    if (typeof query_string[pair[0]] === "undefined") {
      query_string[pair[0]] = decodeURIComponent(pair[1]);
        // If second entry with this name
    } else if (typeof query_string[pair[0]] === "string") {
      var arr = [ query_string[pair[0]],decodeURIComponent(pair[1]) ];
      query_string[pair[0]] = arr;
        // If third or later entry with this name
    } else {
      query_string[pair[0]].push(decodeURIComponent(pair[1]));
    }
  } 
  return query_string;
}();
</script>
    <script>
        $( document ).ready(function() {

			var s = {};
			
            if(QueryString.product) {
                s = {
                    'products' : [
                        {
                            'path':QueryString.product,
                            'quantity':1
                        }
                    ]
                }
            }
 
            if(QueryString.email && QueryString.fname && QueryString.lname) {
				s.paymentContact = {};
				s.paymentContact.email = QueryString.email;
				s.paymentContact.firstName = QueryString.fname;
				s.paymentContact.lastName = QueryString.lname;
			}
 
            if(QueryString.coupon) {
				s.coupon = QueryString.coupon;
            }
            
			if (s) fastspring.builder.push(s);
        });
    </script>



The above example allows you to pass, via the URL, information that will be added to the cart session.  If you specify the product in the URL, be sure to use the product path / product ID (see the example here) rather than the product's Display name.


For example: 

Example URL

.../cart.html?product=8675309&coupon=FALLSALE&fname=Leeroy&lname=Jenkins&email=ne1@all.com


In this example, the store page on your website would have a URL ending in cart.html. The name of the page can be whatever you need. The above will add product ID 8675309 to the cart, apply coupon FALLSALE, and apply the customer's first name, last name and email address to the order.


If you only want to pass the product:


Example URL

.../cart.html?product=8675309



If you only want to pass the product and the coupon code:


Example URL

.../cart.html?product=8675309&coupon=FALLSALE


If you pass in the customer name or email address, then currently, the first name, last name and email address must all  be passed in order for any of them to work.  For example:


Example URL

.../cart.html?fname=Leeroy&lname=Jenkins&email=ne1@all.com