简体   繁体   English

使用AJAX更新Shopify购物车产品列表

[英]Update Shopify cart product list using AJAX

I have 'Ajaxified' my cart on Shopify, when I click the 'Add to cart' button on a product page, the price updates in the top right hand corner of the screen and the product is added to the cart without a page refresh. 我在Shopify上将购物车“ Ajaxified”化了,当我单击产品页面上的“添加到购物车”按钮时,价格会在屏幕右上角更新,并且产品会添加到购物车中,而无需刷新页面。

When you click the price in the top right hand corner, it reveals a dropdown showing you what products are in your basket (It shows product - image, url, price & quantity). 当您单击右上角的价格时,它会显示一个下拉菜单,向您显示购物篮中有哪些产品(显示产品-图片,网址,价格和数量)。

However, when you click 'Add to cart' this list of products does not update until you refresh the page. 但是,当您单击“添加到购物车”时,直到刷新页面,此产品列表才会更新。

How do I get this list to refresh (or how do I add the product to the list in the AJAX request)?? 如何刷新此列表(或如何将产品添加到AJAX请求的列表中)?

I have tried adding this into the ajaxify cart module ( https://raw.githubusercontent.com/carolineschnapp/ajaxify-cart/master/ajaxify-cart.liquid ): 我尝试将其添加到ajaxify购物车模块( https://raw.githubusercontent.com/carolineschnapp/ajaxify-cart/master/ajaxify-cart.liquid ):

$.getJSON(_config.shopifyAjaxCartURL, function(cart) {
    $("#dropdown-cart").append('<tr><td><a href="'+PRODUCT.URL+'" class="dropdown-product-image"><img src="'+PRODUCT.IMAGE+'"  alt="'+PRODUCT.DESCRIPTION+'" /></a></td><td class="restrain-width"><a href="'+PRODUCT.URL+'" class="small">PRODUCT.NAME</a></td><td class="text-right"><span class="small">PRODUCT.QUANTITY</span></td></tr>');
});

This updates the cart with the table rows and cells, but I do not know how to call the correct variables for PRODUCT.**** . 这将使用表的行和单元格更新购物车,但我不知道如何为PRODUCT.****调用正确的变量。 I have tried liquid ( {{ product.title }} ) variables but these do not work for me. 我尝试使用液体( {{ product.title }}变量,但这些变量对我不起作用。

I also tried to use cart.item , cart.product cart.product_title , as cart. 我还尝试将cart.itemcart.product cart.product_title用作cart. is referenced in the code already, but this just broke the script. 已在代码中被引用,但这只是破坏了脚本。

Does anyone know how to simply refresh this form without a page refresh after the button is clicked (this would be perfect!), or how to add products using the route i've started down above? 有谁知道如何简单地刷新此表单,而无需单击按钮后刷新页面 (这将是完美的!),或者如何使用我从上面开始的路线添加产品?

So i've finally figured out where I was going wrong, I was trying to call cart.title where it should have been cartItem.title . 于是我终于想通了,我要去哪里错了,我是想叫cart.title它应该已经cartItem.title Then I just looped these in jQuery with each . 然后,我只是在用jQuery的这些循环each

Here's my full code: 这是我的完整代码:

$.each(cart.items, function(index, cartItem) {
   $("#dropdown-cart").append('<tr><td><a href="'+cartItem.url+'" class="dropdown-product-image"><img src="'+cartItem.image+'" alt="product image"/></a></td><td class="restrain-width"><a href="'+cartItem.url+'" class="small">'+cartItem.quantity+' '+cartItem.title+'</a></td><td class="text-right"><span class="small">'+(Shopify.formatMoney(cartItem.line_price, "{{ shop.money_format | remove: "'" | remove: '"' }}"))+'</span></td></tr>');
});

shopify have its own ajax methods, that you can use to you ajax cart shopify有自己的ajax方法,您可以将其用于ajax购物车

shopify cart.js shopify cart.js

This worked for me perfectly! 这完全适合我! Outdated post maybe, but for those looking where to input this code it's under: 也许过时的帖子,但是对于那些希望在哪里输入此代码的人来说,它位于以下位置:

$.getJSON(_config.shopifyAjaxCartURL, function(cart) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM