LemonStand Documentation

shop:cart

The shop:cart action is designed for creating the Cart page. This action can handle the shopping cart content management requests like removing items from the cart, moving items to the Postponed list and changing item quantities. For details about using the action, please refer to the Creating the Cart Page article.

Generated PHP variables

  • $countries – a collection of countries for populating the country list for the Get Shipping Rate feature. The collection is an object of the Db_DataCollection class. Each element in the collection is an object of the Shop_Country class.
  • $states - a list of states for a currently selected country. The collection is an object of theDb_DataCollection class. Each element in the collection is an object of the Shop_CountryState class.
  • $shipping_info - an object of the Shop_CheckoutAddressInfo class, representing a customer's shipping location. For new customers all fields in the object are empty.
  • $discount - discount value, calculated using the price rules, defined on the Shop/Discounts page. The discount value can be not accurate on the Cart page, because price rules can refer to customer details (for example a shipping location) which are not known on the Cart page and will become available only during the checkout process.
  • $applied_discount_rules - array, represents a list of discount rules applied to the cart products. Each element in the array is an object with two fields - $rule(Shop_CartPriceRule class) and $discount (numeric value). You can use this variable for displaying a list of applied discounts. Example:
    <h3>Applied discounts</h3>
    <? foreach ($applied_discount_rules as $rule_info): ?>
      <p>
        <?= $rule_info->rule->name ?> 
        <?= $rule_info->rule->description ?> - 
        <?= format_currency($rule_info->discount) ?><br/>
      </p>
    <? endforeach ?>
    
  • $cart_total - a sum of all cart items.
  • $cart_total_tax_incl - cart total, including tax. If the Display catalog/cart prices including tax feature is enabled, this variable value will match the $cart_total variable value.
  • $cart_tax - total tax amount for all cart items.
  • $cart_taxes - a list of all taxes applied to the cart items. An array of objects. Each element in the array is an object with two fields: $name, $total.
  • $coupon_code - a coupon code provided by a visitor

Supported form fields

For each item in the shopping cart you could define the following form fields.

  • item_postponed[item_key] – a checkbox-type type INPUT element, responsible for moving an item to or from the postponed items list. Example:
    <input type="hidden" name="item_postponed[<?= $item->key ?>]" value="0"/>
    <input type="checkbox" <?= checkbox_state($item->postponed) ?> name="item_postponed[<?= $item->key ?>]" value="1"/>
    The hidden element used here to provide the default value 0 for cases when the checkbox is not checked. By default browsers do not send checkbox values if they are not checked. Please always use this method with the item_postponed checkbox.
  • item_quantity[$item->key] – allows users to manage quantities of items in the cart. Example:
    <input type="text" name="item_quantity[<?= $item->key ?>]" value="<?= $item->quantity ?>"/> 
  • coupon - a coupon code (optional). If specified, LemonStand will apply the coupon code and re-evaluate the $discount variable

Supported AJAX handlers

  • shop:on_deleteCartItem – removes an item from the cart. Required POST value: key – specifies a key of a shopping cart item to remove. You can use this handler for creating the Remove Item links for individual cart items. Example:
    <a onclick="return $(this).getForm().sendRequest(
      'shop:on_deleteCartItem', 
      {update: {'cart_page': 'cart_partial'}, 
      confirm: 'Do you really want to remove this item from the cart?', 
      extraFields: {key: '<?= $item->key ?>'}})" 
    href="#">Remove Item</a>
    
    The code creates the Remove Item link which sends the shop:on_deleteCartItem AJAX request to LemonStand.
  • shop:on_evalShippingRate - allows to add the "Get shipping rates" feature to the cart page. The action requires 3 POST variables country, state and zip. The action generates a list of available shipping options which should be rendered using a partial. Please read the Implementing the Shipping Cost Estimator Feature article for more information.
  • shop:on_setCouponCode - allows to create a text field for entering a coupon code and a button for  processing the coupon code and redirecting the browser to a specific page. You can use this handler for creating the AJAX driven Checkout button on the Cart  page. Example: 
    <label for="coupon_code">Do you have a coupon?</label> <input id="coupon_code" type="text" name="coupon"/>
    <input type="button" value="Checkout!" onclick="return $(this).getForm().sendRequest('shop:on_setCouponCode')"/>
    <input type="hidden" name="redirect" value="/checkout_start"/>
    The input element with the coupon  name and the hidden field with the redirect name are required. Note: if your copy of LemonStand is installed in a subdirectory, you need to use theroot_url() function in the redirect field value to generate correct URLs referring to LemonStand pages.
  • on_action – applies item quantities and removes marked items from the cart. This request is equal to the regular form POST method, but works through AJAX. Use this request to create the Apply Changes button on the Cart page. Example:
    <input onclick="return $(this).getForm().sendRequest(
      'on_action', 
      {update: {'cart_page': 'cart_partial'}})" 
    type="image" src="/resources/images/btn_apply.gif" />

Managing content of a specific shopping cart

LemonStand supports multiple shopping carts. Using the shop:cart action you can manage a content of any specific shopping cart. All handlers and actions which work with shopping cart accept the $cart_name parameter, which specifies a name of a cart. By default LemonStand works with a shopping cart named "main". You don't need to pass the "main" shopping cart name to LemonStand, because it implies this name by default.

In order to display and manage a content of a specific shopping cart, need:

  1. Pass the cart_name parameter to the shop:cart action before the action is executed. You can do it using the following code on the Pre Action Code field of the Create/Edit Page form:
    $_POST['cart_name'] = 'my_second_cart';
    
  2. Pass the cart_name parameter to all AJAX handlers which you invoke on the Cart page. The simplest way to do it is to add a hidden field to the form which wraps your cart content: 
    <input type="hidden" name="cart_name" value="my_second_cart"/>

You will also need to pass the cart_name parameter to LemonStand during the checkout, if you want LemonStand to create an order from a specific shopping cart items. Please read about the shop:checkout action for details.

Next: shop:category
Previous: Shop_Manufacturer
Return to Reference