skip to Main Content
{% cartitems = [{ 
"id":1,"properties":{"_bundle_sku":"111""},"quantity":1},
{ "id":2,"properties":{"_bundle_sku":"222""},"quantity":1},
{ "id":3,"properties":{"_bundle_sku":"333""},"quantity":1},
{ "id":4,"properties":{"_bundle_sku":"222""},"quantity":1}
}] %}

This is for example I am getting result of my cart items and now I like to keep items together whose _bundle_sku values are same. But I don’t find any help to manipulate cart json object in shopify liquid files.

So here I like the response to achieve like following –

{% cartitems = [{ 
"id":1,"properties":{"_bundle_sku":"111""},"quantity":1},
{ "id":2,"properties":{"_bundle_sku":"222""},"quantity":1},
{ "id":4,"properties":{"_bundle_sku":"222""},"quantity":1},
{ "id":3,"properties":{"_bundle_sku":"333""},"quantity":1}
}] %}

Please assist me how can I achieve this.
many thanks.

2

Answers


  1. You have to use Javascript. Liquid is not going to be of much help to you. Remember this basic pattern.

    • Shopify renders Liquid server-side. Combines all the templates, snippets, assets into a huge string of HTML and dumps to your browser.
    • Use Javascript once all the rendering is done to "do stuff". Your bundling use is a prime example. It becomes trivial to deal with "items" connected to other items using Javascript. Forget using Liquid for this purpose. It is not meant for that.
    Login or Signup to reply.
  2. You may use Ajax Cart API to do that with a /cart/update.js POST.
    Documentation is available here:
    https://shopify.dev/docs/themes/ajax-api/reference/cart#post-cart-update-js

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search