#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Red face Updating the quantity if item already exists in shopping cart


    Hi all so I managed to get the subtotal of all items in my cart thankyou. The last functionality that im having problems with is adding 1 to the quantity of a particular item in the cart if that item already exits. At the minute if I click add to basket twice then the same item is listed twice. Instead it would be great to have the item listed once with the quantity of two if this makes sense. I have managed to get something working but my code is being weird. If I add an item then add another item the quantity will update but not always! Thankyou in advance.

    Code Below

    Code:
    <?php 
    
    //Start the session
    session_start();
    
    function checkCartForItem($addItem, $cartItems) {
         if (is_array($cartItems)){
              foreach($cartItems as $key => $item) {
                  if($item['name'] === $addItem)
                      return $key;
              }
         }
         return false;
    }
    
    
    //Create 'cart' if it doesn't already exist
    if (!isset($_SESSION['SHOPPING_CART'])){ $_SESSION['SHOPPING_CART'] = array(); }
    
    //Add an item only if we have the threee required pices of information: name, price, qty
    if (isset($_GET['add']) && isset($_GET['price']) && isset($_GET['qty'])){
    				
    
    	//Store it in a Array
    	$ITEM = array(
    		//Item name		
    		'name' => $_GET['add'], 
    		//Item Price
    		'price' => $_GET['price'], 
    		//Qty wanted of item
    		'qty' => $_GET['qty']	
    			
    		);
    		
    		$addItem = $_GET['add'];
    
    $itemExists = checkCartForItem($addItem, $_SESSION['SHOPPING_CART']);
    
    if ($itemExists){
         // item exists - increment quantity value by 1
         $_SESSION['SHOPPING_CART'][$itemExists]['qty']++;
    }
    		  
    	
    	//Add this item to the shopping cart
    	$_SESSION['SHOPPING_CART'][] =  $ITEM;
    				
    	header('Location: ' . $_SERVER['PHP_SELF']);
    	
    	
    }
    
    else if (isset($_GET['remove'])){
    	//Remove the item from the cart
    	unset($_SESSION['SHOPPING_CART'][$_GET['remove']]);
    	//Re-organize the cart
    	//array_unshift ($_SESSION['SHOPPING_CART'], array_shift ($_SESSION['SHOPPING_CART']));
    	//Clear the URL variables
    	header('Location: ' . $_SERVER['PHP_SELF']);
    
    }
    else if (isset($_GET['empty'])){
    	//Clear Cart by destroying all the data in the session
    	session_destroy();
    	//Clear the URL variables
    	header('Location: ' . $_SERVER['PHP_SELF']);
    
    }
    
    else if (isset($_POST['update'])) {
    	
    	//Updates Qty for all items
    	foreach ($_POST['items_qty'] as $itemID => $qty) {
    		
    		//If the Qty is "0" remove it from the cart
    		if ($qty == 0) {
    			//Remove it from the cart
    			unset($_SESSION['SHOPPING_CART'][$itemID]);
    			
    		}
    
    		
    		else if($qty >= 1) {
    			//Update to the new Qty
    			$_SESSION['SHOPPING_CART'][$itemID]['qty'] = $qty;
    			
    			//Update quantity if item exists
    			$addItem = $_GET['add'];
    			
    			
    		}
    	}	
    		
    	//Clear the POST variables
    	header('Location: ' . $_SERVER['PHP_SELF']);
    } 
    ?>
  2. #2
  3. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,303
    Rep Power
    9400
    Well you have
    PHP Code:
    $itemExists checkCartForItem($addItem$_SESSION['SHOPPING_CART']);

    if (
    $itemExists){
        
    // item exists - increment quantity value by 1
        
    $_SESSION['SHOPPING_CART'][$itemExists]['qty']++;

    which seems right so far. Only problem is that checkCartForItem() will return the array key if the item is in there, and if that key is 0 then if($itemExists) will fail.

    You have to actually look for the value false, not just something that is equivalent.
    PHP Code:
    if ($itemExists !== false) { 
    Meanwhile
    PHP Code:
    //Add this item to the shopping cart
    $_SESSION['SHOPPING_CART'][] =  $ITEM
    should only execute if the item doesn't exist. Use an else block.

    PHP Code:
    $itemExists checkCartForItem($addItem$_SESSION['SHOPPING_CART']);

    if (
    $itemExists !== false){
        
    // item exists - increment quantity value by 1
        
    $_SESSION['SHOPPING_CART'][$itemExists]['qty']++;
    } else {
        
    //Add this item to the shopping cart
        
    $_SESSION['SHOPPING_CART'][] =  $ITEM;

    And rather than simply incrementing you probably should be adding the $ITEM['qty'], right?

    Side comment:
    Whenever you do a header() redirect you should exit; or die; immediately after so PHP doesn't continue running your script and do things you didn't intend. Your script is safe from this because all the redirections happen inside if/else blocks and are the last things to be executed, but consider exiting after regardless so you get in the habit of doing it.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Talking Thankyou Problem solved


    You were right forgot to add
    PHP Code:
    !== false){ 
    and also the else block thanks for the help and also the side comments

IMN logo majestic logo threadwatch logo seochat tools logo