$cart = cart_get_items(); break; case 'add': $product_id = $_GET['product_id']; $quantity = $_GET['quantity']; $product = get_product($product_id); // validate the quantity entry if (empty($quantity)) { display_error('You must enter a quantity.'); } elseif (!is_valid_number($quantity, 1)) { display_error('Quantity must be 1 or more.'); } cart_add_item($product_id, $quantity); $cart = cart_get_items(); break; case 'update': $items = $_POST['items']; foreach ($items as $product_id => $quantity) { if ($quantity == 0) { cart_remove_item($product_id); } else { cart_update_item($product_id, $quantity); } } $cart = cart_get_items(); break; default: add_error("Unknown cart action: " . $action); break; } include './cart_view.php';
<tr id="cart_header"> <th class="left">Item</th> <th class="right">Price</th> <th class="right">Quantity</th> <th class="right">Total</th> </tr> <?php foreach ($cart as $product_id => $item) { ?> <?php $error = false; $product = get_product($product_id); $available_stock = $product['qtyOnHand'] - $product['pending']; if ($item['quantity'] > $available_stock) { $item['quantity'] = $available_stock; cart_update_item($product_id, $item['quantity']); $error = true; } ?> <tr> <td><?php echo $item['name']; ?> <?php if ($error) { ?> <br><span class="error">Quantity exceeds available stock. <b>Quantity adjusted</b>.</span> <?php } ?> </td>