gpt4 book ai didi

jquery - Shopify 在购物车页面上发布 Ajax 添加到购物车成功消息

转载 作者:行者123 更新时间:2023-12-01 03:47:33 24 4
gpt4 key购买 nike

我正在使用 Shopify 构建一个在线商店,并且在默认模板中遇到了一些 jQuery/Ajax 代码,我想根据自己的需要进行修改。

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/Ajax 经验

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');
}
});

有任何关于我哪里出错的指示吗?教程、指南等会很棒。谢谢

最佳答案

您必须使用 URL 中的参数将用户重定向到购物车页面,告诉购物车页面显示消息。类似于下面的内容:

您的 Ajax 添加到购物车调用和回调

$.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 类的元素。

关于jquery - Shopify 在购物车页面上发布 Ajax 添加到购物车成功消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11994239/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com