This is the documentation for LemonStand V1, which has been discontinued. You can learn more and upgrade your store here.

LemonStand Version 1 Has Been Discontinued

This documentation is for LemonStand Version 1. LemonStand is now offered as a cloud-based eCommerce platform.
You can try the new LemonStand and learn about upgrading here.

Displaying a List of Shopping Cart Items

Creating a partial for displaying a list of cart items

Items in the LemonStand shopping cart can be postponed and so we need a way to display both active and postponed items on the cart page. As the nature of both lists are the same, it is a good idea to use the same partial for both lists.

A list of items in the shopping cart must contain an item image, if any, item title, item options and extra options controls for postponing an item and making it active, item quantity, item single price and item total price.

The following code demonstrates an example of the cart item list partial. The partial accepts 2 parameters to be passed in it - $postponed and $items. $postponed parameter must have a value TRUE for the postponed item list.

Create a new partial and name it shop:cart_table. The partial name could be anything, but we used the shop:cart_table in the cart page code example above.

<? $postponed = isset($postponed) ? $postponed : null ?>
<table>
  <tr>
    <th>Cart Items</th>
    <th><? if (!$postponed): ?>Postpone<? else: ?>Postponed<? endif ?></th>
    <th>Quantity</th>
    <th>Delete</th>
    <th>Price</th>
    <th>Discount</th>
    <th>Total</th>
  </tr>
  <? if (!count($items)): ?>
    <tr>
      <td colspan="6">Your cart is empty.</td>
    </tr>
  <? else: ?>
  <?
    foreach ($items as $item):
    $image_url = $item->product->image_url(0, 60, 'auto');
    $options_str = $item->options_str();
  ?>
    <tr>
      <td>
        <? if ($image_url): ?>
          <img src="<?= $image_url ?>" alt="<?= h($item->product->name) ?>"/>
        <? endif ?>
        <strong><?= h($item->product->name) ?></strong>
        <? if (strlen($options_str)): ?>
          <br/><?= h($options_str) ?>.
        <? endif ?>
        <? if ($item->extra_options): ?>
          <? foreach ($item->extra_options as $option): ?>
            <br/>
            + <?= h($option->description) ?>:
            <?= format_currency($option->get_price($item->product)) ?>
          <? endforeach ?>
        <? endif ?>
      </td>
      <td>
        <input type="hidden" name="item_postponed[<?= $item->key ?>]" value="0"/>
        <input type="checkbox" <?= checkbox_state($item->postponed) ?> name="item_postponed[<?= $item->key ?>]" value="1"/>
      </td>
      <td>
        <? if (!$postponed): ?>
          <input type="text" name="item_quantity[<?= $item->key ?>]" value="<?= $item->quantity ?>"/>
        <? else: ?>
          <?= $item->quantity ?>
        <? endif ?>
      </td>
      <td><input type="checkbox" name="delete_item[]" value="<?= $item->key ?>"/></td>
      <td><?= format_currency($item->single_price()) ?></td>
      <td><?= format_currency($item->total_discount()) ?></td>
      <th><?= format_currency($item->total_price()) ?></th>
    </tr>
    <? endforeach ?>
  <? endif ?>
</table>
<table>
  <thead>
    <tr>
      <th>Cart Items</th>
      <th>{% if not postponed %}Postpone{% else %}Postponed{% endif %}</th>
      <th>Quantity</th>
      <th>Delete</th>
      <th>Price</th>
      <th>Discount</th>
      <th>Total</th>
    </tr>
  </thead>
  <tbody>
    {% if items|length == 0 %}
      <tr>
        <td colspan="5">Your cart is empty.</td>
      </tr>    
    {% else %}
      {% for item in items %}
        {% set image_url = item.product.image_url(0, 60, 'auto') %}
        {% set options_str = item.options_str() %}
        <tr>
          <td>
            {% if image_url is not empty %}
              <img src="{{ image_url }}" alt="{{ item.product.name }}"/>
            {% endif %}
            
            <strong>{{ item.product.name }}</strong>
            {% if options_str|length > 0 %}
              <br/>{{ options_str }}.
            {% endif %}
            
            {% if item.extra_options|length > 0 %}
              {% for option in item.extra_options %}
                <br/>
                + {{ option.description }}:
                  {{ option.get_price(item.product)|currency }}
              {% endfor %}
            {% endif %}
          </td>
          <td>
            <input type="hidden" name="item_postponed[{{ item.key }}]" value="0"/>
            <input type="checkbox" {{ checkbox_state(item.postponed) }} name="item_postponed[{{ item.key }}]" value="1"/>
          </td>
          <td>
            {% if not postponed %}
              <input type="text" name="item_quantity[{{ item.key }}]" value="{{ item.quantity }}"/>
            {% else %}
              {{ item.quantity }}
            {% endif %}
          </td>
          <td><input type="checkbox" name="delete_item[]" value="<?= $item->key ?>"/></td>
          <td>{{ item.single_price()|currency }}</td>
          <td>{{ item.total_discount()|currency }}</td>
          <th>{{ item.total_price()|currency }}</th>
        </tr>
      {% endfor %}
    {% endif %}
  </tbody>
</table>

The code iterates over the passed $items array and creates a table row for each item.

The first column of the table contains an item image, name, and selected options.

The second column contains a checkbox for postponing or activating an item. Please note the name of the checkbox – item_postponed[item_key]. This name should not be changed in order for LemonStand to be able to process the checkbox state. You may also note a hidden element with the same name placed before the checkbox. It is used to pass 0 value to PHP in case the checkbox is not checked. By default, values of unchecked checkboxes are not sent to the server on form submit.

The third column contains a checkbox for deleting an item. The checkbox INPUT element has name delete_item[] and value corresponding to the item key. The name of the checkbox element should not be changed.

The next column contains the item quantity input field. The name item_quantity[item_key] should not be changed. Note that the postponed items the code does not output the field, replacing it with a static text.

The two last columns display the item individual price, item discount and row total price. The discount value is calculated basted on the currently active catalog-level price rules.


Previous: Displaying Bundle Items in the Shopping Cart
Return to Shopping Cart