示例#1
0
function cart_subtotal()
{
    $subtotal = 0;
    $cart = cart_get_items();
    foreach ($cart as $item) {
        $subtotal += $item['unit_price'] * $item['quantity'];
    }
    return $subtotal;
}
示例#2
0
        $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';