Osama Shaki
Osama Shaki

Reputation: 149

Add same item to shopping cart with different options

I am working on a PHP shopping cart and I have 3 products that the user can choose quantity and butter type. Items are being added to the cart with same id which is the product Id from the database.

How can I add the same item to the cart with different butter option so I can have different id/index for the added item, because in the checkout page when I try to delete one item, more than one item is been deleted because of the id issue. I hope you can guide me to do so. Here is my code for the shopping cart,

if(isset($_POST["add_to_cart"]))
{
    if(isset($_SESSION["shopping_cart"])) 
    {
        
        $item_array_id = array_column($_SESSION["shopping_cart"], "item_id"); 
        $item_array_butter = array_column($_SESSION["shopping_cart"], "butter_type");

        if(isset($_POST["butter_type"]))
        {
            $butter_type = $_POST["butter_type"];
        }
        else
        {
            $butter_type = '';
        }

        if(in_array($_GET["id"], $item_array_id) && in_array($butter_type, $item_array_butter))
        {
            //increment qty by 1
            $_SESSION["shopping_cart"][0]['item_quantity']++;
        }
        else if(in_array($_GET["id"], $item_array_id) && !in_array($butter_type, $item_array_butter))
        {
            $item = array("item_id"=>$_GET["id"],
                      "item_name"=>$_POST["hidden_name"],
                      "item_price"=> $_POST["hidden_price"],
                      "subtotal_price"=>$_POST["hidden_price"]*$_POST["quantity"],
                      "item_quantity"=>$_POST["quantity"],
                      "butter_type"=>$butter_type
                    );
            array_push($_SESSION["shopping_cart"],$item); // Items added to cart
        }
        else
        {
            $item = array("item_id"=>$_GET["id"],
                      "item_name"=>$_POST["hidden_name"],
                      "item_price"=> $_POST["hidden_price"],
                      "subtotal_price"=>$_POST["hidden_price"]*$_POST["quantity"],
                      "item_quantity"=>$_POST["quantity"],
                      "butter_type"=>$butter_type
                    );
            array_push($_SESSION["shopping_cart"],$item); // Items added to cart
        }

    }
    else
    {   
        $_SESSION["shopping_cart"] = array();

        if(isset($_POST["butter_type"]))
        {
            $butter_type = $_POST["butter_type"];
        }
        else
        {
            $butter_type = '';
        }

        $item = array("item_id"=>$_GET["id"],
                      "item_name"=>$_POST["hidden_name"],
                      "item_price"=> $_POST["hidden_price"],
                      "subtotal_price"=>$_POST["hidden_price"]*$_POST["quantity"],
                      "item_quantity"=>$_POST["quantity"],
                      "butter_type"=>$butter_type
                    );
        array_push($_SESSION["shopping_cart"],$item); // Items added to cart

    }
}

and here is the html form to add to cart:

<form method="post" action="index.php?action=add&id=<?php echo $mainrow["id"]; ?>">
<input type="submit" name="add_to_cart" id="add_to_cart" value="Add to Cart" />
</form

Upvotes: 0

Views: 702

Answers (2)

Osama Shaki
Osama Shaki

Reputation: 149

I have solved my issue with adding 'product_id' variable to the shopping cart array and I have used a session variable counter as a value, so all items in the cart has a unique id. and manage to update and delete items without any errors. Thanks you for your help

Upvotes: 0

Chandana
Chandana

Reputation: 199

A simple solution is to pass the "id" and the "butter_type" values both in your form.

<form method="post" action="index.php?action=add&id=<?php echo $mainrow["id"]; ?>&butter_type=<?php echo $butterType ?>">

Then, in the back-end, when you store the items, instead of storing the "id" of the item, create a combined key and keep it as the "id".

$item = array("item_id"=> $_GET["id"].'_'.$_GET['butter_type']...

In this way, an item defines by the item's "id" as well as it's "butter_type"

When you remove items from the shopping cart, now instead of the "id", you should remove the session item with above combined key.

Then when you checkout, you can use the explode command the get the item id as well as butter type.

Upvotes: 1

Related Questions