Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MahmoudMonem's avatar

How to insert data into session with Item details

Hello guys, I managed to store item [product details ] to session. I would like to add some extra data to my item details. which are dynamic based on customer choice.

for example. Customer visit product 1 which has multiple Skus that contains details like size , color etc.

When customer add a specific product to cart. I store the item details and I would like to store the extra stuff too. like color size, SKU number etc.

I tried like below, but the dump data function is not showing the size, color and sku number.

 public function addProductToCart(Request $request,$id){
    
            $prevCart = $request->session()->get('cart');
            $cart = new Cart($prevCart);
            $sku_number =  "DO-12345";
            $size =  "L";
            $color =  "Mauve";
    
            $product = Product::find($id);
            $cart->addItem($id,$product,$color,$size,$sku_number);
            $request->session()->put('cart', $cart);
    
     
           dump($cart);

            // return back();
    
    
        }
0 likes
5 replies
MahmoudMonem's avatar

@vincent15000 Hello Vincent,

Thanks for your respond. here you are

Cart.php

public function addItem($id,$product){
 
        $price =  ($product->prod_price);
        
      // if course was not added to cart then add it       
        if(! array_key_exists($id,$this->items) ){
        
              $productToAdd = ['quantity'=> 1, 'totalSinglePrice'=> $price, 'data'=>$product];
              $this->items[$id] = $productToAdd;
              $this->totalQuantity++;
              $this->totalPrice = $this->totalPrice + $price;
              
              return back()->with('success','You added 1 Product to the Cart');
        
        }else{
            return back()->with('error','This Product already exists in the Cart');
        }
1 like
vincent15000's avatar
Level 63

@MahmoudMonem You have declared your method with only 2 parameters $id and $product and you try to pass to it 5 parameters.

$cart->addItem($id,$product,$color,$size,$sku_number);

You have to change your declaration like this.

public function addItem($id,$product,$color,$size,$sku_number)
{
		... 
}
1 like

Please or to participate in this conversation.