重定向至 Shopify 客户注册处的结帐页面

sop*_*e f 6 redirect liquid shopify

我想在 Shopify 网站中创建帐户后将客户重定向到结账页面。

\n\n

Shopify 提供了这种在客户登录时重定向的方法:

\n\n
{% form \'customer_login\' %}\n <input type="hidden" name="checkout_url" value="/checkout"/>\n
Run Code Online (Sandbox Code Playgroud)\n\n

但将相同的方法应用于客户注册时,表单会在成功时重定向到结账,但会忽略错误检查:

\n\n
  {% form \'create_customer\' %}\n <input type="hidden" name="return_to" value="/checkout" />\n
Run Code Online (Sandbox Code Playgroud)\n\n

这种使用 HTML 表单而不是 Liquid 表单的方法会重定向到结帐,但客户尚未登录,这违背了结帐前创建帐户的目的:

\n\n
 <form method=\'post\' action=\'/checkout\' id=\'create_customer\' accept-charset=\'UTF-8\'>\n <input name=\'form_type\' type=\'hidden\' value=\'create_customer\'/>   \n <input name="utf8" type="hidden" value="\xe2\x9c\x93">\n
Run Code Online (Sandbox Code Playgroud)\n\n

错误检查后有什么重定向的想法吗?谢谢!

\n

sop*_*e f 2

我找到了一个解决方案,可以使用 AJAX 将创建客户请求发送到 Shopify,然后在成功时将用户重定向到结账。如果有错误,它们会显示在表单上,​​而不是像之前建议的解决方案那样自动重定向。

HTML:

{% form 'create_customer' %}

<div class ="errors"> </div>
<input type="text" value="" placeholder="First Name" name="customer[first_name]" id="first_name"/>
<input type="text" value="" placeholder="Last Name" name="customer[last_name]" id="last_name"/>
 <input type="email" value="" placeholder="Email" name="customer[email]" id="email" />
 <input type="password" value="" placeholder="Password" name="customer[password]" id="password" />
 <input class="btn" type="submit" value="Create"/>

 {% endform %}
Run Code Online (Sandbox Code Playgroud)

jQuery:

jQuery(function() {
jQuery('#create_customer').submit(function(event) {
  event.preventDefault();
  var data = jQuery(this).serialize();

 //create new account
  jQuery.post('/account', data)
    .done(function(data){
    var logErrors = jQuery(data).find('.errors').text();

    //if there are errors show them in the html form
    if (logErrors != "" && logErrors != 'undefined'){
        jQuery('#create_customer .errors').html(logErrors);
        jQuery('#create_customer .errors').show();

    //if account creation is successful show checkout page
    }else{
       console.log('success');
      document.location.href = '/checkout';
    }
    }).fail(function(){console.log('error');});
   return false;
}); 
});
Run Code Online (Sandbox Code Playgroud)