0

我不熟悉 AJAX/Javascript,所以我很难做一些简单的事情。

在有人将商品添加到购物车之前,我正在尝试使用Shopify 的 AJAX API清除购物车。

我的代码目前如下所示:

    <script>
  function clearcart()
  {
    var my_button=$('input.addToCart');
my_button.on('click',function(){
    $.ajax({
        type: "POST",
        url: '/cart/clear.js',
        data: '',
        success: success,
        dataType: 'json',
        success: function() { 
            return Shopify.clear();

        },
        error: function(XMLHttpRequest, textStatus) {
          /* error code */
        }
    });

    return false;
});
  }
</script>
<input type="submit" name="add" value="Try Now" class="addToCart" onclick="clearcart();" "javascript:window.location='/cart' return false"/>
4

3 回答 3

1

尝试将您的代码更新为此:

 <script>
   function clearcart() {
    var my_button=$('input.addToCart');
    my_button.on('click',function(){
      $.ajax({
        type: "POST",
        url: '/cart/clear.js',
        data: '',
        dataType: 'json',
        success: function() { 
            Shopify.clear();
        },
        error: function(XMLHttpRequest, textStatus) {
          /* error code */
        }
      });
      return false;
    });
  }
</script>
<input type="submit" name="add" value="Try Now" class="addToCart" onclick="clearcart();" "javascript:window.location='/cart' return false"/>
于 2015-07-28T05:41:05.627 回答
0

Actually Shopify.clear() does the AJAX call for you. So all you really need to do is:

function clearCart() {
  Shopify.clear();
}

In your form you have onclick="clearCart();" which will invoke that function for you.

I see you are doing some stuff with your window.location and if you wanted you could also do that within your clearCart function by leveraging the callback that can be passed into the Shopify.clear function call:

function clearCart() {
  Shopify.clear(function(){
    window.location = "/cart";
  });
}

Hope this helps.

于 2013-10-04T18:56:56.023 回答
0

使用它清除购物车页面中的所有数据

$('.addToCart').click(function(){
  $.ajax({
    type: "POST",
    url: '/cart/clear.js',
    data: '',
    dataType: 'json',
    success: function() { 
      location.reload();
    },
    error: function(XMLHttpRequest, textStatus) {
      /* error code */
    }
  });
  return false;
});
于 2020-04-17T11:13:44.167 回答