0
votes

I am trying to create a total quantity figure that I can echo out into my shopping cart button. As of now the only thing is does is read zero unless I click update. If there is 2 or more different products in the cart, the total quantity part I added will double the last item in the shopping cart and not add in any of the other items.

<?php
// Initialize cart
if(!isset($_SESSION['shopping_cart'])) {
    $_SESSION['shopping_cart'] = array();
}
// Update Cart
if(isset($_POST['update_cart'])) {
    $quantities = $_POST['quantity'];
    foreach($quantities as $id => $quantity) {
        if(!isset($products[$id])) {
            $message = "Invalid product!";
            break;
        }
        $_SESSION['shopping_cart'][$id]['quantity'] = $quantity;
    }
    if(!$message) {
        $message = "Cart updated!<br />";
    }
}

// Empty cart
if(isset($_GET['empty_cart'])) {
    $_SESSION['shopping_cart'] = array();
}
            if(empty($_SESSION['shopping_cart'])){
                echo "Your cart is empty<br />";
            }
            else {
        echo $message;
?>
                        <form action='./shoppingcart.php?view_cart=1' method='POST'>
                            <table class="carttable">
                                <tr>
                                    <th class="cartth">Name</th>
                                    <th class="cartth">Price</th>
                                    <th class="cartth">Category</th>
                                    <th class="cartth">Quantity</th>
                                </tr>
<?php                               
                        $base_price = 0;
                        foreach($_SESSION['shopping_cart'] as $id => $product) {
                                    $product_id = $product['product_id'];
                                    $base_price += $products[$product_id]['price'] * $product['quantity'];
                                    $shipping_price += $products[$product_id]['shippingprice'] * $product['quantity'];
?>
                                <tr>
                                        <td class="carttd"><?php echo "<a href='./viewProduct.php?view_product=$id'>" . $product['name'];?><?php echo $products[$product_id]['name']; ?> </a></td>
                                        <td class="carttd"><?php echo '$' . $products[$product_id]['price']; ?></td> 
                                        <td class="carttd"><?php echo $products[$product_id]['category']; ?></td>
                                        <td class="carttd">
                                        <?php echo "<input type='text' name='quantity[$product_id]'  value='" . $product['quantity'] . "' />"; ?> </td>                                         
                                </tr>
<?php
                                }
                                //Calculates total
                                $total_price += $base_price + $shipping_price;
?>
                                <tr>
                                    <td colspan="2"></td>
                                    <td class="tdcarttotal">Subtotal</td>
                                    <td class="tdcarttotal"><?php echo "$" .  $base_price; ?> </td>
                                </tr>
                                <tr>
                                    <td colspan="2"></td>
                                    <td class="tdcarttotal">Tax</td>
                                    <td class="tdcarttotal">$0</td>
                                </tr>
                                <tr>
                                    <td colspan="2"></td>
                                    <td class="tdcarttotal">Shipping Price</td>
                                    <td class="tdcarttotal"><?php echo "$" .  $shipping_price; ?></td>
                                </tr>
                                <tr>
                                    <td colspan="2"></td>
                                    <td class="tdcarttotal">Total</td>
                                    <td class="tdcarttotal"><?php echo "$" .  $total_price; ?></td>
                                </tr>
                            </table>
                                <input type='submit' id='button' name='update_cart' value='Update Cart'>
                        </form>
                <form action="checkout.php?checkout=1">
                    <input type="submit" class="checkoutbutton" value="Proceed to Checkout">
                </form><br><br><br><br><br>
<?php
            }
//Shopping Cart Quantity Count

        if(isset($_SESSION['shopping_cart']) && is_array($_SESSION['shopping_cart'])) {
        $totalquantity = 0;
        foreach($_SESSION['shopping_cart'] AS $product['quantity']) {
            $totalquantity = $totalquantity + $quantity;
        }
  }
  else {
       $totalquantity = 0;
  }
  echo $totalquantity;
?>

Does anyone see what I'm doing wrong?

1
you have foreach($_SESSION['shopping_cart'] AS $product['quantity']) but are using + $quantity. Shouldn't that be foreach($_SESSION['shopping_cart'] AS $product) and + $product['quantity']Sean
Awesome that fixed it. Thanks!Paul

1 Answers

0
votes

based on the comments, change

foreach($_SESSION['shopping_cart'] AS $product['quantity']) {
    $totalquantity = $totalquantity + $quantity;
}

to

foreach($_SESSION['shopping_cart'] AS $product) {
    $totalquantity = $totalquantity + $product['quantity'];
}