2012-08-16 64 views
2

我正在使用Shopify建立一个在线商店,并且在默认模板中遇到了一些我想修改我的需求的jQuery/Ajax代码。Shopify post阿贾克斯添加到购物车成功消息在购物车页面

function addToCartSuccess (jqXHR, textStatus, errorThrown){ 

    $.ajax({ 
    type: 'GET', 
    url: '/cart.js', 
    async: false, 
    cache: false, 
    dataType: 'json', 
    success: updateCartDesc 
    }); 
    window.location = "/cart"; 
    $('.add-cart-msg').hide().addClass('success').html('Item added to cart! <a href="/cart" title="view cart">View cart and check out &raquo;</a>').fadeIn('300'); 
} 

我添加了window.location的行自己在想它会发布的购物车页面上,但无济于事消息。如果我删除了该代码,则当按下“添加到购物车”按钮时,成功消息将发布到产品页面上,因此它可以在不做任何修改的情况下实际工作

我也试图与一个POST命令来创建自己的功能,但真正我最好的猜测,因为我没有这个水平的jQuery的/阿贾克斯

function updateCartDesc (jqXHR, textStatus, errorThrown){ 

    $.ajax({ 
    type: 'POST', 
    url: '/cart', 
    async: false, 
    cache: false, 
    dataType: 'html', 
    success: function(){('.add-cart-msg').hide().addClass('success').html('Item added to cart! <a href="/cart" title="view cart">View cart and check out &raquo;</a>').fadeIn('300'); 
    } 
}); 

任何指点我在哪里任何以往的经验出错了?教程,指南等将是美好的。谢谢

+0

你想完成什么? – Paul 2012-08-16 21:49:23

+0

对不起,如果我没有说清楚。一旦点击添加到购物车按钮,就会在页面上显示一条消息,说明某件商品已添加到购物车。我想修改它,以便导航到不同的页面(购物车页面)并在那里添加消息,但是我的更改仅在不显示消息的情况下打开购物车页面。 – Bantros 2012-08-16 22:29:55

回答

3

您将不得不将用户重定向到购物车页面,并在URL中带有一个参数,告诉购物车页面显示消息。喜欢的东西下面:

你阿贾克斯添加到购物车电话和回调

$.ajax({ 
    type: 'GET', 
    url: '/cart.js', 
    async: false, 
    cache: false, 
    dataType: 'json', 
    success: updateCartDesc 
}); 

var updateCartDesc = function() { 
    //redirect the user to the cart and pass showSuccess=true in the URL 
    window.location = "/cart?showSuccess=true"; 
}; 

脚本您的购物车页面上

$(function() { 
    //if the current URL contains showSuccess, 
    //display the success message to the user 
    if(window.location.href.indexOf('showSuccess') != -1) { 
     $('.add-cart-msg').hide() 
          .addClass('success') 
          .html('Item added to cart!') 
          .fadeIn('300'); 

    } 
}); 

只是要注意,此代码假定你有一个元素放在/ cart页面上,类别为add-cart-msg

+0

这就是保罗现在的一种享受,正是我想要达到的。编辑:实际上,它始终显示成功消息。 – Bantros 2012-08-16 22:52:43

+2

不是问题,高兴地帮助:) – Paul 2012-08-16 22:55:38

+0

只是为了让你知道我不得不稍微改变一下代码,以阻止它始终出现在购物车页面上 - “if(window.location.href.indexOf('showSuccess')) != -1)“,再次感谢。 – Bantros 2012-08-16 23:33:22

相关问题