2012-08-16 57 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

相關問題