pickles/common/modules/store/cart/add.php
Josh Sherman c6af785100 Latest and greatest.
git-svn-id: http://svn.cleancode.org/svn/pickles@85 4d10bc64-7434-11dc-a737-d2d0f8310089
2008-12-31 02:45:40 +00:00

63 lines
1.6 KiB
PHP

<?php
/**
* Adds an item to the cart
*
* Adds the passed item to the cart with a quantity of 1.
*
* @package PICKLES
* @subpackage store
* @author Joshua Sherman <josh@phpwithpickles.org>
* @copyright 2007-2008 Joshua Sherman
*/
class store_cart_add extends store {
public function __default() {
if ($this->db->getField('SELECT COUNT(id) FROM products WHERE id = "' . $_REQUEST['id'] . '";') != 1) {
/**
* @todo Add handling for an invalid product
*/
}
else {
// References the product in the cart
$product =& $_SESSION['cart']['products'][$_REQUEST['id']];
// If the data is not set then grab it and set it
if (!isset($product['name'], $product['sku'], $product['price'])) {
$data = $this->db->getRow('SELECT name, sku, price FROM products WHERE id ="' . $_REQUEST['id'] . '";');
$product['name'] = $data['name'];
$product['sku'] = $data['sku'];
$product['price'] = $data['price'];
}
// Increment the quantity and update the total
$product['quantity']++;
$product['total'] = round($product['price'] * $product['quantity'], 2);
unset($product);
// References the cart as a whole
$cart =& $_SESSION['cart'];
$subtotal = 0;
// Loops through the products and totals them up
if (is_array($cart['products'])) {
foreach ($cart['products'] as $product) {
$subtotal += $product['total'];
}
}
// Set the subtotal in the cart
$cart['subtotal'] = round($subtotal, 2);
unset($cart);
// Redirect to the cart
header('Location: /store/cart');
exit();
}
}
}
?>