Ad

Using Timber And Shopify, How Can I Update The Cart Preview?

- 1 answer

I’m using Timber in a Shopify theme.

I have a POST request to /cart/add on a custom page. The item is added to the cart properly but in my callback, I need to update the cart preview (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().

I could duplicate the code in the buildCart() function and put it in my callback, but that is obviously redundant. Am I able to somehow call buildCart() outside of that scope?

Ad

Answer

With a tiny bit of customization this should be doable.

First, make buildCart a public method by adding build: buildCart to:

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

As I write this I realize loadCart is never used publically. 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.

Since you already have the cart, you can instead do ajaxCart.build(cart_object) and you're good to go.

Ad
source: stackoverflow.com
Ad