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.
ApPHP Framework provides automatic initialization of Shopping Cart Component.
Once loaded, the Cart object will be available using: A::app()->getShoppingCart()
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);
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);
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.
$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);
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>
A::app()->getShoppingCart()->insert();
A::app()->getShoppingCart()->update();
A::app()->getShoppingCart()->total();
A::app()->getShoppingCart()->totalItems();
A::app()->getShoppingCart()->contents();
A::app()->getShoppingCart()->getItem();
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();
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();
A::app()->getShoppingCart()->destroy();