Question

Can you help me with my code. I am creating a super simple shopping cart that can add cart/update and remove. But as for now what I already did is the add to cart and the displaying of items. But I have an error. Everytime I refresh the page the session is automatically appending to the session array.

Now what I need to do is this.

Validate if the product is in the session list. If not create a session for that if yes just skip it.

And for the additional question. How can I create an update/remove function?

Here's my code so far. This process is only 1 PHP file.

$category   =   $_GET['cat'];
$product_id =   $_GET['product'];

//fn_print_r($_SESSION);
//unset($_SESSION['cart']);

$product_detail = get_allproduct_detail($product_id);

$prod_price = $product_detail['prod_price'];
$sale_price = $product_detail['sale_price'];
$prod_type  = $product_detail['prod_type'];

if(!empty($_POST['product_id']) && !empty($_POST['product_name']) && !empty($_POST['product_price']) && !empty($_POST['sale_price']) && !empty($_POST['qty'])) {

    $sess_id = $_POST['product_id'];
    $sess_name = $_POST['product_name'];
    $sess_price = $_POST['product_price'];
    $sess_sale_price = $_POST['sale_price'];
    $sess_qty = $_POST['qty'];

    $compute_total = $sess_sale_price * $sess_qty;

    $cart_row = array(
        'product_id' => $sess_id,
        'product_name' => $sess_name,
        'product_price' => $sess_price,
        'sale_price' => $sess_sale_price,
        'qty' => $sess_qty,
        'total' => $compute_total
    );

    if(!isset($_SESSION['cart'])){
        $_SESSION['cart'] = array();
    }

    $_SESSION['cart'][] = $cart_row;

    //fn_print_r($_SESSION);

} 

Here's the form process

<form method="POST" action="?page=product&cat=<?php echo $_GET['cat']; ?>&product=<?php echo $_GET['product']; ?>" onsubmit="">

    <div id="item_detail_right">

        <label>Qty:<input type="text" name="qty" value="1" size="5" style="text-align: center" />
        <input type="hidden" name="product_price" id="product_price" value="<?php echo $prod_price; ?>" />
        <input type="hidden" name="sale_price" id="sale_price" value="<?php echo $sale_price; ?>" />
        <input type="hidden" name="product_id" id="product_id" value="<?php echo $_GET['product']; ?>" />
        <input type="hidden" name="product_name" id="product_name" value="<?php echo strtoupper(get_product_name($_GET['product'])); ?>" />
        <input type="submit" value="+CART" />
        <input type="button" value="+Wishlist" id="mywishlist" data-wishlist-id="<?php echo $_GET['product']; ?>" />

    </div>

</form>

Here's the display of cart

if(!isset($_SESSION['cart']) || (count($_SESSION['cart']) == 0)) {
                echo "<p>Your cart is empty</p>";
            } else {

                echo "<table border='0' style='font-size: 12px; width: 100%' cellpadding='5'>";
                    echo "<tr>";
                        echo "<td style='background-color: white; color: black; text-align: center'>Product ID</td>";
                        echo "<td style='background-color: white; color: black; text-align: center'>Name</td>";
                        echo "<td style='background-color: white; color: black; text-align: center'>Price</td>";
                        echo "<td style='background-color: white; color: black; text-align: center'>Sale Price</td>";
                        echo "<td style='background-color: white; color: black; text-align: center'>Quantity</td>";
                        echo "<td style='background-color: white; color: black; text-align: center'>Total</td>";
                        echo "<td style='background-color: white; color: black; text-align: center'></td>";
                    echo "</tr>";

                    $total = 0;

                    foreach($_SESSION['cart'] as $item) {

                        echo "<tr>";
                            echo "<td style='text-align: center; background-color: gray; color: black'>".$item['product_id']."</td>";
                            echo "<td style='text-align: left; background-color: gray; color: black'>".$item['product_name']."</td>";
                            echo "<td style='text-align: right; background-color: gray; color: black'>".number_format($item['product_price'],2)."</td>";
                            echo "<td style='text-align: right; background-color: gray; color: black'>".number_format($item['sale_price'],2)."</td>";
                            echo "<td style='text-align: center; background-color: gray; color: black'><input type='text' name='cart_qty[]' value='".$item['qty']."' size='10' style='text-align: center'></td>";
                            echo "<td style='text-align: right; background-color: gray; color: black'>".number_format($item['total'],2)."</td>";
                            echo "<td style='text-align: center; background-color: gray; color: black'><a href='#'>Update this?</a> | <a href='#'>Remove this?</div></td>"; //how can I use this to remove and update the session?
                        echo "</tr>";

                        $total += ($item['sale_price'] * $item['qty']);

                    }

                    echo "<tr>";
                        echo "<td colspan='7' style='text-align: right'>";
                            echo "<label>Subtotal Amount: </label><input type='text' name='subtotal' value='".number_format($total,2)."' readonly='readonly'>";
                            echo "<input type='submit' value='Place Item' />";
                        echo "</td>";
                    echo "</tr>";

                echo "</table>";

            }   

Here's my sample output of array

Array
(
    [visit] => nsrAROum86lb8VK
    [slideshow] => 04-15-14
    [cart] => Array
        (
            [0] => Array
                (
                    [product_id] => 1
                    [product_name] => AJNA
                    [product_price] => 90
                    [sale_price] => 81
                    [qty] => 1
                    [total] => 81
                )

            [1] => Array
                (
                    [product_id] => 1
                    [product_name] => AJNA
                    [product_price] => 90
                    [sale_price] => 81
                    [qty] => 1
                    [total] => 81
                )

            [2] => Array
                (
                    [product_id] => 1
                    [product_name] => AJNA
                    [product_price] => 90
                    [sale_price] => 81
                    [qty] => 1
                    [total] => 81
                )

            [3] => Array
                (
                    [product_id] => 1
                    [product_name] => AJNA
                    [product_price] => 90
                    [sale_price] => 81
                    [qty] => 1
                    [total] => 81
                )
           )
    )

i uploaded my whole code to this link

http://www.mediafire.com/view/g6jah2bxbzda04l/aroma_shop.php

Was it helpful?

Solution

You can check wether the product is already in the cart.

if(!isset($_SESSION['cart'])){
    $_SESSION['cart'] = array();
}

// Instead of appending $cart_row immediately,
// $_SESSION['cart'][] = $cart_row;

// only add the $cart_row which has not been added previously
$found = false;
foreach ($_SESSION['cart'] as $c) {
    if ($c['product_id'] == $cart_row['product_id']) {
        $found = true;
    }
}

if (!$found) {
    $_SESSION['cart'][] = $cart_row;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top