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

MahmoudMonem's avatar

How to dynamically store data in session based on customer input

Hello Guys, I managed to store static data in my session, however, I am struggling trying to make them dynamic. for example I have product > has many > skus [ colors ] > has many > sizes

I would like customer to choose the color and size, and then I keep his choices in the session for the next step. when he proceed with creating the order.

I tried to replace the static info like 1 / M / Mauve with something like adding Request $request parameter in the functions. but didn't work. sorry if this sounds confusing, but my head is burning and have no idea how to do this.

$color= $request->input('color');

productscontroller.php


    public function addProductToCart(Request $request,$id){

        //    $request->session()->forget("cart");
        //    $request->session()->flush();
    
            $prevCart = $request->session()->get('cart');
            $cart = new Cart($prevCart);
            $sku_number =  "1";
            $size =  "M";
            $color =  "Mauve";
            
    
            $product = Product::find($id);
            $cart->addItem($id,$product,$color,$size,$sku_number);
            $request->session()->put('cart', $cart);
    
     
        //    dump($cart);

            return back();
    
    

cart.php

public function addItem($id,$product,$color,$size,$sku_number){
 
        $price =  ($product->prod_price);
        $sku_number =  "1";
        $size =  "M";
        $color =  "Mauve";
        
      // if course was not added to cart then add it       
        if(! array_key_exists($id,$this->items) ){
        
              $productToAdd = ['quantity'=> 1, 'totalSinglePrice'=> $price, 'data'=>$product,'sku_number'=>$sku_number, 'size'=>$size,'color'=>$color];
              $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');
        }

show.blade

@foreach($product->skus->unique('color') as $sku)
                  <div class="custom-control custom-option custom-control-inline mb-2">
                    <input class="custom-control-input" type="radio" name="color" id="color_{{$sku->color->id}}" data-label="colorOption" value="{{$sku->color->color_name}}">
                    <label class="custom-option-label rounded-circle" for="color_{{$sku->color->id}}"><span class="custom-option-color rounded-circle" style="background-color: #{{$sku->color->color_code}};"></span></label>
                  </div>
                  @endforeach
               

                  <div class="d-flex justify-content-between align-items-center pb-1 ">
                      <label class="font-weight-medium" for="product-size">   <br>   <br>Size:</label><a class="nav-link-style font-size-sm" href="#size-chart" data-toggle="modal"><i class="czi-ruler lead align-middle mr-1 mt-n1"></i>Size guide</a>
                    </div>
                       <br><select class="custom-select" required id="product-size">
                      <option value="">Select size</option>
                      <option value="xs">XS</option>
                      <option value="s">S</option>
                      <option value="m">M</option>
                      <option value="l">L</option>
                      <option value="xl">XL</option>
                    </select>
                  </div>
0 likes
2 replies

Please or to participate in this conversation.