How to Select the current cart being displayed.

Highlighted
New Member
1 0 0

Hi Everyone, 

I'm fairly new to shopify and I want to ask if anyone knows how to select the current cart item.

so the setup is that currently I have a caller for a script function 

  {% if settings.cart_type == 'drawer' %}
    {% include 'ajax-cart-template' %}
  {% endif %}
 
and inside the ajax-cart-template.liquid below
if you see the forloop at the bottom it is
{% for item in cart.items %}
then i can pull data from the cart.items individually for all the items in the cart
how can i select just one of the items that is being shown right now.
so right now the forloop will show all of the cart items. where as I only want to show the value of the currently rendering item.
 

 

 

<script id="CartTemplate" type="text/template">
{% raw %}
  <form action="/cart" method="post" novalidate class="cart ajaxcart">
    <div class="drawer__inner drawer__inner--has-fixed-footer">
      {{#items}}
      <div class="ajaxcart__product appear-animation appear-delay-{{animationRow}}">
        <div class="ajaxcart__row">
          <div class="grid">
            <div class="grid__item one-third">
              <a href="{{url}}" class="ajaxcart__product-image"><img src="{{img}}" alt="{{name}}"></a>
            </div>
            <div class="grid__item two-thirds">
              <div class="ajaxcart__product-name--wrapper">
                <a href="{{url}}" class="ajaxcart__product-name">{{{name}}}</a>
                {{#if variation}}
                  <span class="ajaxcart__product-meta">{{variation}}</span>
                {{/if}}
                {{#properties}}
                  {{#each this}}
                    {{#if this}}
                      <span class="ajaxcart__product-meta">{{@key}}: {{this}}</span>
                    {{/if}}
                  {{/each}}
                {{/properties}}
              </div>

              <div class="grid grid--full display-table">
                <div class="grid__item display-table-cell one-half">
                  <div class="ajaxcart__quantity js-qty">
                    <label for="updates_{{key}}" class="visually-hidden">{% endraw %}{{ 'products.product.quantity' | t }}{% raw %}</label>
                    <input type="number" id="updates_{{key}}" name="updates[]" value="{{itemQty}}" min="0" class="js-qty__fallback" data-id="{{key}}">
                  </div>
                </div>
                <div class="grid__item display-table-cell one-half text-right">
                  {{#if discountsApplied}}
                    <small class="ajaxcart__price--strikethrough">{{{price}}}</small>
                    <span class="ajaxcart__price">
                      {{{discountedPrice}}}
                    </span>
                  {{else}}
                    <span class="ajaxcart__price">
                      {{{price}}}
                    </span>
                  {{/if}}
                </div>
              </div>
              {{#if discountsApplied}}
                <div class="grid grid--full display-table">
                  <div class="grid__item text-right">
                    {{#each discounts}}
                      <small class="ajaxcart__discount">{{this.title}}</small>
                    {{/each}}
                  </div>
                </div>
              {{/if}}
              <div class="testing">
              {% endraw %}
              {%- assign cart_items_count = cart.items | size -%}
              
              {%- for item in cart.items -%}
              
              "{{ item.id }}": {{ item.variant.inventory_quantity }}
              {%- unless forloop.index == cart_items_count -%},{%- endunless -%}
              {%- endfor -%}
              ::
              {{cart.items.variant_id}}
              {% raw %}
              </div>
            </div>
          </div>
        </div>
      </div>

 

 

0 Likes