# Special Topics - Shopping Cart

- General
- Initializing the Shopping Cart Component
- Adding an Item to The Cart
- Adding Multiple Items to The Cart
- Updating The Cart
- Displaying the Cart
- Function Reference

### General ¶

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')
    );

    A::app()->getShoppingCart()->insert($data);

### 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(
    array(
        'id'      => 'SKU_11111',
        'qty'     => 1,
        'price'   => 39.95,
        'name'    => 'T-Shirt'
    ),
    array(
        'id'      => 'SKU_22222',
        'qty'     => 1,
        'price'   => 9.95,
        'name'    => 'Coffee Mug',
        'options' => array('Size' => 'L', 'Color' => 'Red')
    ),
    array(
        'id'      => 'SKU_33333',
        'qty'     => 1,
        'price'   => 29.95,
        'name'    => 'Shot Glass'
    ));

    A::app()->getShoppingCart()->insert($data);

### 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
    );

    A::app()->getShoppingCart()->update($data);

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

    A::app()->getShoppingCart()->update($data);

### 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">
    <tr>
      <th>QTY</th>
      <th>Item Description</th>
      <th style="text-align:right">Item Price</th>
      <th style="text-align:right">Sub-Total</th>
    </tr>
    <?php
    $shoppingCart = A::app()->getShoppingCart();
    $i = 1;
    ?>
    <?php foreach ($shoppingCart->contents() as $items): ?>
        <tr>
            <td><?php echo CHtml::textField($i.'[qty]', $items['qty'], array('maxlength'=>'3')); ?></td>
            <td>
            <?php echo $items['name']; ?>

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

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

### Function Reference ¶

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

    A::app()->getShoppingCart()->insert();

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

    A::app()->getShoppingCart()->update();

Displays the total amount (sum) in the cart.

    A::app()->getShoppingCart()->total();

Displays the total number of items in the cart.

    A::app()->getShoppingCart()->totalItems();

Returns an array containing everything in the cart.

    A::app()->getShoppingCart()->contents();

Returns an array of the particular product.

    A::app()->getShoppingCart()->getItem();

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.

    A::app()->getShoppingCart()->hasOptions();

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.

    A::app()->getShoppingCart()->productOptions();

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

    A::app()->getShoppingCart()->destroy();