Cart

From Shopify Wiki

Jump to: navigation, search

The liquid variable cart represents the shopping cart and has the following attributes:

Contents

cart.item_count

Returns the number of items currently in the shopping cart.

Example usage:

There are currently {{ cart.item_count }} items in your cart

cart.items

Returns all items in the shopping cart. Each one is of the type LineItem. You will want to iterate through the return value (see example)

Example usage:

<ul>
{% for item in cart.items %}
   <li>{{ item.title }}</li>
{% endfor %}
</ul>

cart.total_price

Returns the total of all the prices added up in your shopping cart.

Example usage:

The total of your cart is: {{ cart.total_price | money }}

cart.total_weight

Returns the total weight of all items in the cart combined. Weight is always returned as grams. Use weight or weight_with_unit from the filter library to display the actual weight in your preferred unit system.

Example usage:

The total of your cart is: {{ cart.total_weight | weight_with_unit }}

cart.note

You have the option of adding a note field to your checkout template. Usage of this feature is very flexible. The general idea is that you simply define an input field named “note” in the form that submits to ”/cart” in cart.liquid. Here is an example:

<p>
  <label for="notes">Extra notes or special instructions</label>
  <input type="text" id="notes" name="note" size="60" value="{{ cart.note }}" />
</p>

When the order is submitted you will be shown the note at the top of the order details page in your admin area. You can check out this blog post for additional examples and explanation: http://jadedpixel.com/2006/8/18/attach-notes

cart.attributes

The attributes property is similar to the note property above in that it is an optional field you can add to your shop’s checkout form. Simply define an input field named “attributes[]” and it will be captured automatically and displayed on the order detail page in your admin area. So, for example, you wanted to capture the length of necklace a customer wanted you could do this:

<p>
  <label for="wrapping">Gift Wrapping:<label>
   <select name="attributes[gift_wrapping]" id="wrapping">
   <option value="none" 
       {% if cart.attributes.gift_wrapping ==  "none" %} checked="checked" {% endif %}> None needed...</option>
   <option value="wrapping" 
       {% if cart.attributes.gift_wrapping ==  "wrapping" %} checked="checked" {% endif %}> Gift Wrapping</option>
   <option value="box" 
       {% if cart.attributes.gift_wrapping ==  "box" %} checked="checked" {% endif %}> Gift Box</option>
  </select>

Again, a visual example of this technique (and how to use a checkbox input for an attribute) is available at: http://jadedpixel.com/2006/8/18/attach-notes

NOTE: If this doesn't work for you, try the following code in place of the above:

<p>
  <label for="wrapping">Gift Wrapping:<label>
   <select name="attributes[gift_wrapping]" id="wrapping">
   <option value="none" 
       {% if cart.attributes.gift_wrapping ==  "none" %} SELECTED {% endif %}> None needed...</option>
   <option value="wrapping" 
       {% if cart.attributes.gift_wrapping ==  "wrapping" %} SELECTED  {% endif %}> Gift Wrapping</option>
   <option value="box" 
       {% if cart.attributes.gift_wrapping ==  "box" %} SELECTED  {% endif %}> Gift Box</option>
  </select>
Personal tools