我一直在使用ajax填充抽屉购物车的功能,因此不必刷新页面即可查看购物车中的新项目。 这是相关的ajax调用:
$.ajax({
url: _config.shopifyAjaxAddURL,
dataType: 'json',
type: 'post',
data: $addToCartForm.serialize(),
success: function(itemData) {
$.getJSON(_config.shopifyAjaxCartURL, function(cart) {
$('.drawer-refresh-hide').css('display','block');
$.each(cart.items, function(index, cartItem) {
var line= index +1;
var cents = "";
if (cartItem.price % 100 < 10) {
cents = "0";
}
var price = parseInt(cartItem.price/100) + "." + cents + cartItem.price % 100;
$('.dropdown-cart').append("<tr class='responsive-table-row'><td class='cart-first'><a href='"+ cartItem.url +"'</a><img src='"+ cartItem.image +"'/><a href='/cart/change?line="+ line +"&quantity=0' class='qty-remove small--hide'><p class='small'>{{ 'cart.general.remove' | t }}</p></a><input type='submit' name='update' value='{{ 'cart.general.update' | t }}' class='qty-remove small small--hide ajax-submit'></td><td class='cart-second'><span class='CartTitle'>"+ cartItem.product_title +"</span>{% unless item.product.has_only_default_variant %}<p>"+ cartItem.variant_title+"</p>{% endunless %}{% if "+ cartItem.available +" %}<span class='stock-message'>In Stock</span>{% endif %}</td><td class='cart-mobile-last'><span>Quantity</span></td><td data-label='test' class='mobile-edit-container'><div class='cart-quantity'><input type='button' value='+' class='qtyplus qty-plus-2' field='updates_"+ cartItem.id +"'></input><input type='number' name='updates[]' id='updates_"+ cartItem.id +"' class='quantity' value='"+ cartItem.quantity +"'/><input type='button' value='-' class='qtyminus qtyminus-2' field='updates_"+ cartItem.id +"'></input></div></td><td data-label='{{ 'cart.label.total' | t }}' class='small--hide'>£"+ price +"</td></tr>");
});
$(function(index, cartItem) {
var line2= index +1;
var cents2 = "";
if (cart.total_price % 100 < 10) {
cents2 = "0";
}
var price2 = parseInt(cart.total_price/100) + "." + cents2 + cart.total_price % 100;
$('.dropdown-cart-2').append("<div class='cart-total'><span class='order-first'>Total</span><span class='order-second'>"+ price2 +"</span></div>");
});
});
$('.drawer-main-form').css('display','none');
// Re-enable add to cart button.
$addToCartBtn.addClass('inverted');
_setText($addToCartBtn, _config.addedToCartBtnLabel);
_showFeedback('success',' ',$addToCartForm);
window.setTimeout(function(){
$addToCartBtn.prop('disabled', false).removeClass('disabled').removeClass('inverted');
$addToCartForm.find('.swatch-element.option-selected').removeClass("option-selected");
_setText($addToCartBtn,_config.addToCartBtnLabel);
}, _config.howLongTillBtnReturnsToNormal);
// Update cart count and show cart link.
$.getJSON(_config.shopifyAjaxCartURL, function(cart) {
if (_config.cartCountSelector && $(_config.cartCountSelector).size()) {
var value = $(_config.cartCountSelector).html() || '0';
$(_config.cartCountSelector).html(value.replace(/[0-9]+/,cart.item_count)).removeClass('hidden-count');
}
if (_config.cartTotalSelector && $(_config.cartTotalSelector).size()) {
if (typeof Currency !== 'undefined' && typeof Currency.moneyFormats !== 'undefined') {
var newCurrency = '';
if ($('[name="currencies"]').size()) {
newCurrency = $('[name="currencies"]').val();
}
else if ($('#currencies span.selected').size()) {
newCurrency = $('#currencies span.selected').attr('data-currency');
}
if (newCurrency) {
$(_config.cartTotalSelector).html('<span class=money>' + Shopify.formatMoney(Currency.convert(cart.total_price, "{{ shop.currency }}", newCurrency), Currency.money_format[newCurrency]) + '</span>');
}
else {
$(_config.cartTotalSelector).html(Shopify.formatMoney(cart.total_price, "{{ shop.money_format | remove: "'" | remove: '"' }}"));
}
}
else {
$(_config.cartTotalSelector).html(Shopify.formatMoney(cart.total_price, "{{ shop.money_format | remove: "'" | remove: '"' }}"));
}
};
});
},
error: function(XMLHttpRequest) {
var response = eval('(' + XMLHttpRequest.responseText + ')');
response = response.description;
if (response.slice(0,4) === 'All ') {
$addToCartBtn.prop('disabled', false);
_setText($addToCartBtn, _config.soldOutBtnLabel);
$addToCartBtn.prop('disabled',true);
}
else {
$addToCartBtn.prop('disabled', false).removeClass('disabled');
_setText($addToCartBtn, _config.addToCartBtnLabel);
}
}
});
这可行,但是我在购物车项目的每个功能上都遇到了问题,因此请注意:
$.each(cart.items, function(index, cartItem) {
var line= index +1;
var cents = "";
if (cartItem.price % 100 < 10) {
cents = "0";
}
var price = parseInt(cartItem.price/100) + "." + cents + cartItem.price % 100;
$('.dropdown-cart').append("<tr class='responsive-table-row'><td class='cart-first'><a href='"+ cartItem.url +"'</a><img src='"+ cartItem.image +"'/><a href='/cart/change?line="+ line +"&quantity=0' class='qty-remove small--hide'><p class='small'>{{ 'cart.general.remove' | t }}</p></a><input type='submit' name='update' value='{{ 'cart.general.update' | t }}' class='qty-remove small small--hide ajax-submit'></td><td class='cart-second'><span class='CartTitle'>"+ cartItem.product_title +"</span>{% unless item.product.has_only_default_variant %}<p>"+ cartItem.variant_title+"</p>{% endunless %}{% if "+ cartItem.available +" %}<span class='stock-message'>In Stock</span>{% endif %}</td><td class='cart-mobile-last'><span>Quantity</span></td><td data-label='test' class='mobile-edit-container'><div class='cart-quantity'><input type='button' value='+' class='qtyplus qty-plus-2' field='updates_"+ cartItem.id +"'></input><input type='number' name='updates[]' id='updates_"+ cartItem.id +"' class='quantity' value='"+ cartItem.quantity +"'/><input type='button' value='-' class='qtyminus qtyminus-2' field='updates_"+ cartItem.id +"'></input></div></td><td data-label='{{ 'cart.label.total' | t }}' class='small--hide'>£"+ price +"</td></tr>");
});
可以正确填充购物车,但是如果添加了多个产品,则这些项目会显示多次。举个例子: 用户添加产品A。产品A随后显示在抽屉式购物车中。 用户无需刷新页面即可添加产品B。购物车抽屉现在显示: -产品A -产品B -产品A(再次)
因此每个执行两次(这很有意义),但是不确定如何解决此问题,这样添加的产品就不会重复出现了吗?
答案 0 :(得分:0)
删除$('.dropdown-cart')
函数之前的each
行。
$('.dropdown-cart tr').remove();
$.each(cart.items ...)