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

nhayder's avatar
Level 13

Adding options to product in cart session

In my shopping cart app every product can have 2 main options 1 is color and 2 paid or free variations for example you can order black iphone with 246 GB ram ???? the Ram option is paid but the colors is totally free.

in similar way I need to implement same terminology in my code to achieve this type of functionality.

so I have gone ahead and created a simple cart class to add and remove products from the cart without any options for my Laravel app like this.

class Cart
{
    public $items = [];
    public $totalQty;
    public $totalPrice;

    public function __Construct($cart = null) {
        if($cart) {
            $this->items = $cart->items;
            $this->totalQty = $cart->totalQty;
            $this->totalPrice = $cart->totalPrice;
        } else {
            $this->items = [];
            $this->totalQty = 0;
            $this->totalPrice = 0;
        }
    }

    // public function add($product) {
        
         $item = [
             'id' => $product->id,
             'name' => $product->name,
             'slug' => $product->slug,
             'price' => $product->price,
             'qty' => 0,
             'poster' => $product->poster,
             'color' => $product->color,
             'variation' => $product->variation,
             'variation_price' => $product->variation_price,
             'subtotal' => $product->subtotal,
         ];

         if( !array_key_exists($product->id, $this->items)) {
             $this->items[$product->id] = $item ;
             $this->totalQty +=1;
             $this->totalPrice += $product->price * $product->qty; 
         } else {
             $this->totalQty +=1 ;
             $this->totalPrice += $product->price * $product->qty; 
         }

         $this->items[$product->id]['qty']  += $product->qty ;
    // }

    public function remove($id) {

        if( array_key_exists($id, $this->items)) {
            $this->totalQty -= $this->items[$id]['qty'];
            $this->totalPrice -= $this->items[$id]['qty'] * $this->items[$id]['price'];
            unset($this->items[$id]);

        }

    }

    public function updateQty($id, $qty) {
        
        //reset qty and price in the cart ,
        $this->totalQty -= $this->items[$id]['qty'];
        $this->totalPrice -= $this->items[$id]['price'] * $this->items[$id]['qty'];

        // add the item with new qty
        $this->items[$id]['qty'] = $qty;

        // total price and total qty in cart
        $this->totalQty += $qty ;
        $this->totalPrice += $this->items[$id]['price'] * $qty;

    }

Now I need to add the options features so I updated the cart sturcture like this


//dd($cart);
^ array:1 [▼
  20 => array:9 [▼ // <----- In checkout page??? I'm looping through cart items at this point
    "id" => 20
    "name" => "Aut distinctio est aut esse voluptas fugiat."
    "slug" => "ut-et-voluptas-atque-aut-aut-distinctio"
    "price" => 100
    "qty" => 1
    "subtotal" => 106
    "poster" => "/image/sm/e0b2ece3-5ece-45a4-8cba-108e30217ae4"
    "options" => array:1 [▼ //<---- I need to loop here to show list of added options for this item
      "#a83ae4" => array:4 [▼ // this is color value as key, this will help me when i deleted this option from cart
        "qty" => 1
        "color" => "#a83ae4"
        "variation" => "264 Ram memory"
        "variation_price" => "50"
      ]
    ]
  ]
]

And I have update the cart class to add these new features like this

public function add($product) {
        
        $item = [
            'id' => $product->id,
            'name' => $product->name,
            'slug' => $product->slug,
            'price' => $product->price,
            'qty' => 0,
            'subtotal' => $product->subtotal,
            'poster' => $product->poster,
            'options' => [ // this is where i loop throught these option in checkout page

                $product->color => array(

                        'qty' => $product->qty,
                        'color' => $product->color,
                        'variation' => $product->variation,
                        'variation_price' => $product->variation_price,

                    )
            ]
        ];

if( !array_key_exists($product->id, $this->items)) {
            $this->items[$product->id] = $item;

            array_push($this->items[$product->id], array('qty' => $product->qty,
                        'color' => $product->color,
                        'variation' => $product->variation,
                        'variation_price' => $product->variation_price,));

            $this->totalQty +=1;
            $this->totalPrice += $product->price * $product->qty; 
        } else {
            $this->totalQty +=1 ;
            $this->totalPrice += $product->price * $product->qty; 
        }

        $this->items[$product->id]['qty']  += $product->qty ;

ok the problem starts here, …

Example: if a user ordered additional phone lets say its a Red iPhone with 512 GB Ram I need to check if that iPhone exists in the cart, if yes ??? then I need to update these

1- update the total quantity for this option in the options->qty array 2- Add the new options array as a 2nd row in the options array (above) so i can loop through them in the checkout page. 3- update the price of this item options in the options->variation_price.

the implementation that I have above is adding the new options but its not updating if the product exists in the cart ( QTY + price neither adding the new option as row in the options array).

any ideas on how to solve this problem?????

0 likes
0 replies

Please or to participate in this conversation.