【发布时间】:2020-10-31 07:19:23
【问题描述】:
在 SHOPIFY STORE:如何在 URL 中添加折扣代码而不使用 javascript 重定向。在带有折扣代码的 url 中有一个参数。
【问题讨论】:
标签: javascript shopify discount shopify-template
在 SHOPIFY STORE:如何在 URL 中添加折扣代码而不使用 javascript 重定向。在带有折扣代码的 url 中有一个参数。
【问题讨论】:
标签: javascript shopify discount shopify-template
您网站中每个带有参数 discout 的 url 都会在结帐时激活。
使用示例
您的折扣代码是 DISCOUNTCODE1
www.myshopifywebsite.com/products/product1?discount=DISCOUNTCODE1
或
www.myshopifywebsite.com?discount=DISCOUNTCODE1
第 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=/';
}
})();
第 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;
}
})();
第 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;
}
})();
【讨论】: