Special Topics - Shopping Cart


The Shopping Cart functionality based on framework CShopingCart component, that permits items to be added to a session that stays active while a user is browsing your site. These items can be retrieved and displayed in a standard "shopping cart" format, allowing the user to update the quantity or remove items from the cart.

Please note that the Cart Class ONLY provides the core "cart" functionality. It does not provide shipping, credit card authorization, payment operations or other processing components.

Initializing the Shopping Cart Component

ApPHP Framework provides automatic initialization of Shopping Cart Component.
Once loaded, the Cart object will be available using: A::app()->getShoppingCart()

Note: The CShoppingCart component will load and initialize the Session component automatically, so you do not need to load the Session component.

Adding an Item to The Cart

To add an item to the shopping cart, simply pass an array with the product information to the A::app()->getShoppingCart()->insert() function, as shown below:

$data = array(
    'id'      => 'SKU_123456',
    'qty'     => 2,
    'price'   => 29.90,
    'name'    => 'T-Shirt',
    'options' => array('Size' => 'M', 'Color' => 'Black', 'Type' => 'V')


Adding Multiple Items to The Cart

You may use a multi-dimensional array, as shown below, it is possible to add multiple products to the cart in one action. This is useful in cases where you wish to allow people to select from among several items on the same page.

$data = array(
    'id'      => 'SKU_11111',
    'qty'     => 1,
    'price'   => 39.95,
    'name'    => 'T-Shirt'
    'id'      => 'SKU_22222',
    'qty'     => 1,
    'price'   => 9.95,
    'name'    => 'Coffee Mug',
    'options' => array('Size' => 'L', 'Color' => 'Red')
    'id'      => 'SKU_33333',
    'qty'     => 1,
    'price'   => 29.95,
    'name'    => 'Shot Glass'


Updating The Cart

To update the information in your cart, you must pass an array containing the RowID and quantity to the A::app()->getShoppingCart()->update() function. What is a RowID? The row ID is a unique identifier that is generated by the cart code when an item is added to the cart. The reason a unique ID is created is so that identical products with different options can be managed by the cart.

Note: If the quantity is set to zero, the item will be removed from the cart.
$data = array(
    'rowid' => 'b94ccd516068f71558sf34d130b6d8ec',
    'qty'   => 3


// Or a multi-dimensional array
$data = array(
        'rowid'   => 'b94ccd516068f71558sf34d130b6d8ec',
        'qty'     => 3
        'rowid'   => 'xw8259q36495793i8jdh973h90jikw23',
        'qty'     => 4
        'rowid'   => '4h5kdk7kao03fnjgoe9hrkdjkobec333',
        'qty'     => 2


Displaying the Cart

To display the cart you have to create a view file with code similar to the one shown below.
Please note that this example uses the CShoppingCart component.

<table cellpadding="6" cellspacing="1" style="width:100%" border="0">
  <th>Item Description</th>
  <th style="text-align:right">Item Price</th>
  <th style="text-align:right">Sub-Total</th>
$shoppingCart = A::app()->getShoppingCart();
$i = 1;
<?php foreach ($shoppingCart->contents() as $items): ?>
        <td><?= CHtml::textField($i.'[qty]', $items['qty'], array('maxlength'=>'3')); ?></td>
        <?= $items['name']; ?>

            <?php if ($shoppingCart->hasOptions($items['rowid']) == true): ?>
                    <?php foreach ($shoppingCart->productOptions($items['rowid']) as $optionName => $optionValue): ?>
                        <strong><?= $optionValue; ?>:</strong> <?= $optionValue; ?><br />
                    <?php endforeach; ?>
            <?php endif; ?>

        <td style="text-align:right"><?= CNumber::format($items['price']); ?></td>
        <td style="text-align:right">$<?= CNumber::format($items['subtotal']); ?></td>
<?php $i++; ?>
<?php endforeach; ?>
  <td colspan="2"> </td>
  <td class="right"><strong>Total</strong></td>
  <td class="right"><?= CNumber::format($shoppingCart->total()); ?></td>

Function Reference

Permits adding items to the shopping cart, as outlined above.

Permits updating items in the shopping cart, as outlined above.

Displays the total amount (sum) in the cart.

Displays the total number of items in the cart.

Returns an array containing everything in the cart.

Returns an array of the particular product.

Returns true (boolean) if a particular row in the cart contains options. This function is designed to be used in a loop with A::app()->getShoppingCart()->contents(), since you must pass the rowid to this function, as shown in the Displaying the Cart example above.

Returns an array of options for a particular product. This function is designed to be used in a loop with A::app()->getShoppingCart()->contents(), since you must pass the rowid to this function, as shown in the Displaying the Cart example above.

Permits you to destroy the cart. This function will likely be called when you are finished processing the customer's order.