PHP购物车错误

时间:2013-09-26 20:38:06

标签: php shopping-cart

我正在制作一个购物车而且我被困在实际购物车上。我在这里读了几个问题并尝试过,但要么它们对我不起作用,要么它们都是垃圾。

PHP代码:addtocart.php

if(isset($_GET['product_id']) && isset($_GET['qty'])){
    $product_id = $_GET['product_id'];
    $qty = $_GET['qty'];
    $cart = $_SESSION['cart'];
    if($cart == null) {
        $cart = array();
    }
    if(!isset($cart[$product_id])) {
        $cart[$product_id] = 0;
    }
    $cart[$product_id] += $qty;
    //var_dump($cart);
}
$session->setSetting("cart", $cart);

PHP代码:cart.php

try {
    if(empty($_GET)) {
        $cart = $session->getSetting("cart");
        if($cart == null) {
            throw new Exception("Empty cart.");
        } else {
            $template->setVar("cart", $cart);
        } elseif($_GET['action'] == 'empty') {
            unset($_SESSION['cart']);
            header("location: /cart.php");
        }
    }
} catch (Exception $e) {
    $template->setVar("errmsg", $e->getMessage());
}

输出:

<table class="table table-bordered table-striped table-condensed">
<?php foreach($this->cart as $product_id => $qty): 
$item = $this->product->buildTotal($product_id); //var_dump($item); 
?>

<tr>
    <th>Items</th>
    <td><?php echo count($item); ?>
</td>
<tr>
    <th>Sub-total</th>
    <td>
        &pound;<?php echo $price = number_format(($item[0]['product_price'] * $qty), 2); ?>
    </td>
</tr>
<tr>
    <th>Shipping</th>
    <td>
        &pound;<?php echo $shipping = "2.95"; ?>
    </td>
</tr>
<tr class="warning">
    <th>Tax</th>
    <td>20%</td>
</tr>
<tr class="success">
    <th>Total</th>
    <td>&pound;<?php $tax = (($price * 20) / 100);
        $total = ($price + $tax + $shipping); 
        echo number_format($total, 2); ?>
    </td>
</tr>
<?php endforeach; ?>

不幸的是,我的错误是购物车底部的总计表显示'x'数量的产品的'x'次数,两个产品的示例使得表格显示两次,而不是仅仅总计不同的价格等。我已经坚持了三天,所以任何帮助都非常感谢。此外,如果您的答案有效,我会将您列入购物车的信用页面。

0 个答案:

没有答案