您可以尝试下面列出的代码,可能需要根据您的主题进行一些更新。
需要注意:您需要在产品添加到购物车后检查并创建一种方法来更新迷你购物车。
window.onload = function(){
// disable the default behavior
$('.boost-pfs-addtocart-btn').each(function(index, button){
var $this = $(button);
$this.closest('form').on('submit', function(e){
e.preventDefault();
});
});
// create new logic once the button clicked
$(document).on('click', '.boost-pfs-addtocart-btn', function(){
var form = $(this).closest('form');
$.post('/cart/add.js', form.serialize(), function(data, status){
if( status == 'success' ){
// show popup once added to cart
var model = $(".ajax-success-modal");
model.fadeIn(500);
var item = form.parents('.boost-pfs-filter-product-item-inner')[0]
var imgSrc = $(item).find('.boost-pfs-filter-product-item-main-image').attr('src');
var pTitle = $(item).find('.boost-pfs-filter-product-item-title').text();
var CPrice = $(item).find('.boost-pfs-filter-product-item-regular-price').text();
model.find(".ajax-product-image").attr("src", imgSrc);
model.find(".added-to-wishlist").hide();
model.find(".added-to-cart").show();
model.find(".ajax-product-title").text(pTitle);
model.find(".ajax_price").text(CPrice);
model.find(".ajax_qty").text(1);
// updating the cart counter in header
$.get("/cart.js", function(data){
$('#cartCount,.ajax_qtyA').text('').text(data.item_count);
$('.ajax_cartTotal').text(Shopify.formatMoney(data.total_price, window.money_format));
}, 'json');
}
}, 'json');
});
}
请将任何 JS 文件中的代码或</body> 结尾之前的代码放入<script></script> 标记中,如果将其放在正文标记之前。
注意:还要确保在代码运行之前加载jQuery,否则代码会因为依赖jQuery lib而失败。