Jump to content

[SOLVED] How to see if product is already in basket session... soo stuck!!


Recommended Posts

Hi Guys,


really stuck with this one and was hoping someone on here could help?


been all over google and can't find the answer...


I'm using a session to hold products id when added to the shopping basket.


problem is i don't know how to tell if they have already been added.


on the sales page of the website i loop through all the products in the database, pulling all the relevant details, as well as adding an add to cart button if the product has not been sold. this disapears if the product is sold.


as they only sell single items i would also like to exclude the "add to basket" button when the item has been added.


Therefore does anyone know a way of checking if the item is already in the session as i loop through to pull the products?


hope that makes sense...!


thanks in advance!


As the site only sells single products


Link to comment
Share on other sites

Hi guys,


thanks for the reply


ok so i add/remove from the session using a link like:


<a href="add_list.php?action=add&id='.$prod_id.'"><img src="images/list.jpg" border="0" alt="Add to Shopping List"></a>


as the $prod_id is set before in the while loop pulling all the products.


the add/remove code is:




//add item to shopping list
if ($action == "add") {


//remove item
else if ($action == "remove") {


//empty baasket
else if ($action = "empty") {
unset($_SESSION['sh']) ;


//if zero clear
if($_SESSION['sh'][$product_id] == 0) {	
unset($_SESSION['sh'][$product_id]) ; }


So what i need to do when pulling all the products, is see if the product ID is in the session. if it is then just leave out the add to basket button.



Link to comment
Share on other sites

You just need to check to see if a product ID is already set in the session before creating a link to it.  It would look something like this:



foreach ($products as $product)
if (!in_array($product['id'], $_SESSION['sh']))
	// print link to product

Link to comment
Share on other sites



I have tried many variations of this method.


When i use this it either just works on the last item added to the basket.


or affects all of the items cross the entire site...?


i'll try your version thou in case i was being a noob...!!  :D



Link to comment
Share on other sites

I just can't get it to work..!!


so i'm adding the items into $_SESSION['sh']


I'm then using a while loop to pull the products from the database


so as it loops through each product i'm setting the ID to $prod_id


i'd like to say something like:


if(in_array($prod_id,$_SESSION['sh'])) {

//take the button away.....



does anyone know how to do this...?



Link to comment
Share on other sites



I have added three products to the list:


the shopping list contains product id and then quantity.


i ripped this code off another site i made and so don't need quantity, but leaving it there shouldn't matter... should it...? :-\


print_r($_SESSION['sh']) ;


Array ( [93] => 1 [94] => 1 [95] => 1 )




Link to comment
Share on other sites

Hi Guys!


i've fixed it, but in a round-about way...


firstly i created a session for the product id when adding to the basket


e.g. $_SESSION['id'.$product_id] ;


i then loop through the products looking to see if the session for the product_id has been set. if not it's not in the basket.


when emptying the basket i simple clear all session as i'm not holding any customer login info.


Thanks for all the help, and i'm sure there's a better way, but this works!


hope that all made sense,


Thanks again!



Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.