简体   繁体   中英

Shopify enable discount code in url (every page) - NO REDIRECT

IN SHOPIFY STORE: How to add discount code in URL without redirect with javascript. Having a parameter in the url with the discount code.

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;
    }
  })();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM