简体   繁体   English

使用Timber和Shopify,如何更新购物车预览?

[英]Using Timber and Shopify, how can I update the cart preview?

I'm using Timber in a Shopify theme. 我在Shopify主题中使用Timber

I have a POST request to /cart/add on a custom page. 我在自定义页面上有对/cart/addPOST请求。 The item is added to the cart properly but in my callback, I need to update the cart preview ( ajax-cart-template.liquid ). 该项目已正确添加到购物车,但在我的回调中,我需要更新购物车预览( ajax-cart-template.liquid )。

I see that a function already exists in ajax-cart.js to do this, where a Handlebars template is rendered in the private function, buildCart() . 我看到ajax-cart.js已经存在一个函数来执行此操作,其中在私有函数buildCart()呈现了一个Handlebars模板。

I could duplicate the code in the buildCart() function and put it in my callback, but that is obviously redundant. 我可以在buildCart()函数中复制代码,并将其放入回调中,但这显然是多余的。 Am I able to somehow call buildCart() outside of that scope? 我可以以某种方式在该范围之外调用buildCart()吗?

With a tiny bit of customization this should be doable. 只需进行一点定制,就可以做到。

First, make buildCart a public method by adding build: buildCart to: 首先,通过将build: buildCart添加到以下内容,使buildCart成为公共方法:

module = {
  init: init,
  load: loadCart,
  build: buildCart
};

As I write this I realize loadCart is never used publically. 在撰写本文时,我意识到loadCart从未公开使用。 Oops. 哎呀。 I think my idea here was that if someone wanted to rebuild the cart, they'd call this ajaxCart.loadCart() and it would get the data and build it out. 我认为我的想法是,如果有人要重建购物车,他们将其称为ajaxCart.loadCart()它将获取数据并将其构建出来。

Since you already have the cart, you can instead do ajaxCart.build(cart_object) and you're good to go. 由于您已经拥有购物车,因此可以执行ajaxCart.build(cart_object) ,一切顺利。

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

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