我想在 Shopify 网站中创建帐户后将客户重定向到结帐页面。
Shopify 提供了在客户登录时重定向的方法:
{% form 'customer_login' %}
<input type="hidden" name="checkout_url" value="/checkout"/>
但是将相同的方法应用于客户注册,表单在成功时重定向到结帐,但忽略错误检查:
{% form 'create_customer' %}
<input type="hidden" name="return_to" value="/checkout" />
这种使用 HTML 表单而不是 Liquid 表单的方法会重定向到结帐,但客户尚未登录,这违背了结帐前创建帐户的目的:
<form method='post' action='/checkout' id='create_customer' accept-charset='UTF-8'>
<input name='form_type' type='hidden' value='create_customer'/>
<input name="utf8" type="hidden" value="✓">
错误检查后有什么重定向的想法吗?谢谢!
最佳答案
下面的代码将 checkout_url 存储在用户浏览器中,然后在用户完成注册过程后将用户重定向到该页面。我已在我拥有的所有 5 家 Shopify 商店中使用了此解决方案。
第 1 步:将此代码粘贴到 login.liquid 文件的底部。
<script>
var Test = __st;
localStorage.setItem('Test', JSON.stringify(Test));
</script>
第 2 步:将此代码粘贴到您的 index.liquid 文件(或您的用户当前重定向到的任何文件)的顶部
<script>
var retrievedObject = localStorage.getItem('Test');
var domain = '';
var theUrl = JSON.parse(retrievedObject)["pageurl"];
var url_dec = decodeURIComponent(theUrl);
if (document.referrer.includes('challenge')) {
if (url_dec.includes("checkout_url")) {
if (url_dec.includes(".myshopify.com")) {
redirectURL = domain.concat((window.location['host']), "/account/login?checkout_url=https://", Shopify.shop, "/",);
}
else
{
redirectURL = domain.concat((window.location['host']), "/account/login?checkout_url=https://", (window.location['host']), "/",);
}
var newCheckout = url_dec.replace(redirectURL, '');
window.location.replace(newCheckout);
localStorage.removeItem("Test");
};
}
</script>
关于重定向至 Shopify 客户注册处的结帐页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36289888/