Updates to the cart.
git-svn-id: http://svn.cleancode.org/svn/pickles@90 4d10bc64-7434-11dc-a737-d2d0f8310089
This commit is contained in:
parent
656f0325fa
commit
a833e0c3cf
4 changed files with 80 additions and 10 deletions
|
@ -26,32 +26,92 @@ class store_cart_add extends store {
|
|||
|
||||
// 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'] . '";');
|
||||
$data = $this->db->getRow('
|
||||
SELECT name, sku, price, limit_per_customer
|
||||
FROM products
|
||||
WHERE id ="' . $_REQUEST['id'] . '";
|
||||
');
|
||||
|
||||
$product['name'] = $data['name'];
|
||||
$product['sku'] = $data['sku'];
|
||||
$product['price'] = $data['price'];
|
||||
$product['name'] = $data['name'];
|
||||
$product['sku'] = $data['sku'];
|
||||
$product['price'] = $data['price'];
|
||||
$product['limit_per_customer'] = $data['limit_per_customer'];
|
||||
|
||||
$product['discounts'] = $this->db->getArray('
|
||||
SELECT discounts.* , discount_rules.*
|
||||
FROM discounts
|
||||
INNER JOIN discount_xref ON discounts.id = discount_xref.discount_id
|
||||
INNER JOIN discount_rules ON discounts.id = discount_rules.discount_id
|
||||
WHERE discount_xref.xref_id = "' . $_REQUEST['id'] . '"
|
||||
AND discounts.disabled = "N";
|
||||
');
|
||||
|
||||
// @todo Should do a look up on the shipping table
|
||||
// @todo Not sure how we want to handle flat rate shipping
|
||||
$product['shipping'] = 4.95;
|
||||
|
||||
if (is_array($product['discounts'])) {
|
||||
foreach ($product['discounts'] as $discount) {
|
||||
|
||||
switch ($discount['applied_to']) {
|
||||
|
||||
case 'SHIPPING':
|
||||
switch ($discount['amount_type']) {
|
||||
case 'FLAT':
|
||||
if ($product['shipping'] < $discount['amount']) {
|
||||
$discount['amount'] = $product['shipping'];
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'PERCENT':
|
||||
if ($discount['amount'] > 100) {
|
||||
$discount['amount'] = 100;
|
||||
}
|
||||
|
||||
$discount['amount'] = $product['shipping'] * ($discount['amount'] / 100);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
$product['shipping'] -= $discount['amount'];
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Increment the quantity and update the total
|
||||
$increment = preg_match('/^[0-9]+$/', $_REQUEST['quantity']) && trim($_REQUEST['quantity']) != '' ? $_REQUEST['quantity'] : 1;
|
||||
$product['quantity'] += $increment;
|
||||
$product['total'] = round($product['price'] * $product['quantity'], 2);
|
||||
// @todo Add per customer limits across all orders.
|
||||
if (empty($product['quantity']) || $product['limit_per_customer'] == 0 || $product['quantity'] < $product['limit_per_customer']) {
|
||||
if (empty($product['quantity'])) {
|
||||
$product['quantity'] = 0;
|
||||
}
|
||||
|
||||
$increment = isset($_REQUEST['quantity']) && preg_match('/^[0-9]+$/', $_REQUEST['quantity']) && trim($_REQUEST['quantity']) != '' ? $_REQUEST['quantity'] : 1;
|
||||
$product['quantity'] += $increment;
|
||||
$product['total'] = round($product['price'] * $product['quantity'], 2);
|
||||
}
|
||||
|
||||
unset($product);
|
||||
|
||||
// References the cart as a whole
|
||||
$cart =& $_SESSION['cart'];
|
||||
$subtotal = 0;
|
||||
$shipping = 0;
|
||||
|
||||
// Loops through the products and totals them up
|
||||
if (is_array($cart['products'])) {
|
||||
foreach ($cart['products'] as $product) {
|
||||
$subtotal += $product['total'];
|
||||
$shipping += $product['shipping'];
|
||||
}
|
||||
}
|
||||
|
||||
// Set the subtotal in the cart
|
||||
$cart['subtotal'] = round($subtotal, 2);
|
||||
$cart['shipping'] = round($shipping, 2);
|
||||
unset($cart);
|
||||
|
||||
// Redirect to the cart
|
||||
|
|
|
@ -32,16 +32,19 @@ class store_cart_remove extends store {
|
|||
// References the cart as a whole
|
||||
$cart =& $_SESSION['cart'];
|
||||
$subtotal = 0;
|
||||
$shipping = 0;
|
||||
|
||||
// Loops through the products and totals them up
|
||||
if (is_array($cart['products'])) {
|
||||
foreach ($cart['products'] as $product) {
|
||||
$subtotal += $product['total'];
|
||||
$shipping += $product['shipping'];
|
||||
}
|
||||
}
|
||||
|
||||
// Set the subtotal in the cart
|
||||
$cart['subtotal'] = $subtotal;
|
||||
$cart['shipping'] = $shipping;
|
||||
unset($cart);
|
||||
|
||||
// Redirect to the cart
|
||||
|
|
|
@ -27,33 +27,40 @@ class store_cart_update extends store {
|
|||
// References the product in the cart
|
||||
$product = $_SESSION['cart']['products'][$id];
|
||||
|
||||
if ($quantity == 0) {
|
||||
if ($quantity <= 0) {
|
||||
unset($_SESSION['cart']['products'][$id]);
|
||||
}
|
||||
else {
|
||||
if ($product['limit_per_customer'] != 0 && $quantity > $product['limit_per_customer']) {
|
||||
$quantity = $product['limit_per_customer'];
|
||||
}
|
||||
|
||||
$product['quantity'] = $quantity;
|
||||
$product['total'] = round($product['price'] * $product['quantity'], 2);
|
||||
$_SESSION['cart']['products'][$id] = $product;
|
||||
}
|
||||
}
|
||||
|
||||
if (count($_SESSION['cart']['products']) == 0) {
|
||||
if (count($_SESSION['cart']['products']) <= 0) {
|
||||
unset($_SESSION['cart']['products']);
|
||||
}
|
||||
|
||||
// References the cart as a whole
|
||||
$cart =& $_SESSION['cart'];
|
||||
$subtotal = 0;
|
||||
$shipping = 0;
|
||||
|
||||
// Loops through the products and totals them up
|
||||
if (is_array($cart['products'])) {
|
||||
foreach ($cart['products'] as $product) {
|
||||
$subtotal += $product['total'];
|
||||
$shipping += $product['shipping'];
|
||||
}
|
||||
}
|
||||
|
||||
// Set the subtotal in the cart
|
||||
$cart['subtotal'] = round($subtotal, 2);
|
||||
$cart['shipping'] = round($shipping, 2);
|
||||
unset($cart);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -56,7 +56,7 @@
|
|||
<td class="right">
|
||||
${$cart.subtotal|number_format:2}<br />
|
||||
$4.99<br />
|
||||
${$cart.subtotal+4.99|number_format:2}
|
||||
${$cart.subtotal+4.99|number_format:2}<br />
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue