Jump to content

Recommended Posts

Hi guys

 

this is code for a t-shirt cart im working on but i keep getting two results from it when i first add a product after that seems to be ok?

<?php 
if(isset($_POST['submit'])){
                       $productId = $_POST['id'];
                           $quantity = $_POST['quantity']; 
                           $size = $_POST['size'];
                           $colour = $_POST['colour'];
                           $design = $_POST['design'];
					  					

                 if(!isset($_SESSION['cart'][$productId])){
                 $_SESSION['cart'][$productId] = array(array('quantity' => $quantity, 'size' => $size, 'colour' => $colour, 'design' => $design));
			 } 
                 foreach ( $_SESSION['cart'][$productId] as &$product ) {
                 if($product['design'] == $design && $product['colour'] == $colour && $product['size'] == $size ) {
	         $product['quantity'] += $quantity;
			 }
			 }

                 $_SESSION['cart'][$productId][] = array('quantity' => $quantity, 'size' => $size, 'colour' => $colour, 'design' => $design);

                 foreach($_SESSION['cart'][$productId] as $prod => $value){

             echo $value['size'] .' '.$value['design'];
             }}?>

Link to comment
https://forums.phpfreaks.com/topic/265811-cart-session/
Share on other sites

This is the help i already have been given this is with comment thanks

 

<?php 
if(isset($_POST['submit'])){
                           $productId = $_POST['productId'];
                           $quantity = $_POST['quantity']; 
                           $size = $_POST['size'];
                           $color = $_POST['color'];
                           $price = $_POST['price'];


  //if the productID doesn't exist, just create it:
  if ( !isset($_SESSION['cart'][$productId]) ) {
    $_SESSION['cart'][$productId] = array(array('quantity' => $quantity, 'size' => $size, 'color' => $color, 'price' => $price));
  } 
  
  //product already exists, check to see if it's the same as another in the group:
  foreach ( $_SESSION['cart'][$productId] as &$product ) {
    //NOTE:  If you want to override pricing with new tiers, you can remove 'price' from this comparison.  You may also want to 
    //       fetch prices from the database when you DISPLAY the cart if you do bulk discounts
    if ( $product['price'] == $price && $product['color'] == $color && $product['size'] == $size ) {

      $product['quantity'] += $quantity;
    }
  }
  
  //if we've reached here, the productID is already in the cart but the current item is not a duplicate of an existing one:
$_SESSION['cart'][$productId][] = array('quantity' => $quantity, 'size' => $size, 'color' => $color, 'price' => $price);


foreach($_SESSION['cart'][$productId] as $prod => $value){


echo $value['size'] .' '.$value['price'];

}

//print_r($_SESSION['cart']);



}
?>

Link to comment
https://forums.phpfreaks.com/topic/265811-cart-session/#findComment-1362154
Share on other sites

Here it is with correct indenting.

 

It's fairly obvious that it will create it twice. The "//if we've reached here" part will ALWAYS be "reached".

 

<?php 
if(isset($_POST['submit'])){
$productId = $_POST['productId'];
$quantity = $_POST['quantity']; 
$size = $_POST['size'];
$color = $_POST['color'];
$price = $_POST['price'];


//if the productID doesn't exist, just create it:
if ( !isset($_SESSION['cart'][$productId]) ) {
	$_SESSION['cart'][$productId] = array(array('quantity' => $quantity, 'size' => $size, 'color' => $color, 'price' => $price));
} 

//product already exists, check to see if it's the same as another in the group:
foreach ( $_SESSION['cart'][$productId] as &$product ) {
	//NOTE:  If you want to override pricing with new tiers, you can remove 'price' from this comparison.  You may also want to 
	//       fetch prices from the database when you DISPLAY the cart if you do bulk discounts
	if ( $product['price'] == $price && $product['color'] == $color && $product['size'] == $size ) {
		$product['quantity'] += $quantity;
	}
}

//if we've reached here, the productID is already in the cart but the current item is not a duplicate of an existing one:
$_SESSION['cart'][$productId][] = array('quantity' => $quantity, 'size' => $size, 'color' => $color, 'price' => $price);


foreach($_SESSION['cart'][$productId] as $prod => $value){
	echo $value['size'] .' '.$value['price'];
}

//print_r($_SESSION['cart']);
}
?>

Link to comment
https://forums.phpfreaks.com/topic/265811-cart-session/#findComment-1362156
Share on other sites

Still could do with some help question if im doing it so wrong can any one point me in the write root.

 

what im trying to do is make a cart say t-shirt and have options for it like colour what they can select then if they choose same t-shirt but diffrent colour it add new product and dont over write last one

 

any help would be great have been trying to do this for some time :'(

Link to comment
https://forums.phpfreaks.com/topic/265811-cart-session/#findComment-1362360
Share on other sites

Because if you copied jesirose's code then it's already fixed. Obviously the double will only be in your code, not hers.

I'm not sure if that was supposed to be sarcasm, but I didn't fix the code, just the indenting, so it was readable. Legible? Whatever. I left the line I believe to be causing the problem in there.

Link to comment
https://forums.phpfreaks.com/topic/265811-cart-session/#findComment-1362449
Share on other sites

You should write your own code rather than using code you don't understand. If you got this code here, and didn't understand it, you should have asked for an explanation.

 

Write out the logic of what you're trying to do. Don't use the existing comments or code, just write down how it should work logically.

Link to comment
https://forums.phpfreaks.com/topic/265811-cart-session/#findComment-1362486
Share on other sites

:'( May be not solved i can put 3 items in the cart all works great then the four one it gos wrong doing my head in here is the new code

 

<?php 
if(isset($_POST['submit'])){
                       $id = $_POST['id'];
					   $sleeve = $_POST['sleeve'];
					   $colour = $_POST['colour'];
                           $size = $_POST['size'];
					   $action = $_POST['action'];
					   $quantity = 1;
                           }
					   //IF NOT IN CART
					   if(!isset($_SESSION['cart'])){
						   $_SESSION['cart'] = array(array('id' => $id, 'colour' => $colour, 'size' => $size, 'quantity' => $quantity));
						   }else{
					   
					   foreach($_SESSION['cart'] as $key => $value){
						   //IF IT IS IN THE CART IM CHECKING TO SEE IF SAME
						   if($value['id'] == $id && $value['colour'] == $colour && $value['size'] == $size){
							        // ADDING ONE TO THE CART IF SAME
							        $quantity = $value['quantity']+=1;
						            $_SESSION['cart'] = array(array('id' => $id, 'colour' => $colour, 'size' => $size, 'quantity' => $quantity));
							   }else{	
							        // IF NOT THE SAME PUTTING THIS OUT
							        $_SESSION['cart'] []= array(array('id' => $id, 'colour' => $colour, 'size' => $size, 'quantity' => $quantity));

									}
						           }
						          }
								  
					              foreach($_SESSION['cart'] as $cart => $value){
									  
									  foreach($value as $c => $v){
										  
										  echo $v;
										  
										  }
									  }
					   
					   ?>

 

Any help work be ace

Link to comment
https://forums.phpfreaks.com/topic/265811-cart-session/#findComment-1362824
Share on other sites

hi guys this seems to be a good one i thnik the problem is here

 

<?php
$_SESSION['cart'][] = array(array('id' => $id, 'colour' => $colour, 'size' => $size, 'quantity' => $quantity));
?>

 

how do i show a session with out the square brackets at the end of $_SESSION['cart']

Link to comment
https://forums.phpfreaks.com/topic/265811-cart-session/#findComment-1362859
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.

Guest
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.