Hi,
I have two simple questions on session arrays. I have created a session array for a shopping cart see below for the code [1].
When a user clicks on a url it adds the data to the session array. The problem is I want it to only add unique products (i.e. ones which have not been added to the cart beforehand, products which already have been added to the cart I want to just add one to qty and increase the price of the specific productId so we dont have duplicates in the session array).
The problem is Im not sure how to this (see [2] for the code I has written so far, could you give me any help of what I have to do?)
[1]
$_SESSION['cart'][] = array(
"qty" => $qty,
"productId" => $prodID,
"price" => $prodprice,
"prodName" => $prodname
);
[2]
//is session array has been set, then check that the product we are adding has not been added beforehand
if(isset($_SESSION['cart']
))
{
foreach($_SESSION['cart'] as $cartItems)
{
//If we have the product already
if ($cartItems['productId'] == $prodID)
{
// *****Code needed to be added here*******
// Change the product details in the session array (cart) by adding
// 1 to quantity (qty) and change the total price of the specific product
}
// if we dont have duplicates add the product to the session array
else
{
$_SESSION['cart'][] = array(
"qty" => $qty,
"productId" => $prodID,
"price" => $prodprice,
"prodName" => $prodname
);
}
}
}
//if the session array has not been created beforehand create cart session array
else
{
$_SESSION['cart'][] = array(
"qty" => $qty,
"productId" => $prodID,
"price" => $prodprice,
"prodName" => $prodname
);
}
Also if I needed to delete a specific product i.e. a product with productId=1, in a the session array ($_SESSION['cart'][]), how would I do that?
Kind Regards!
Thank-you!
Start Free Trial