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

nhayder's avatar
Level 13

adding multiple products to cart with same ID

in my shopping cart app i'm building customers carts using addToCart method, the method checks if the cart is empty or if product already exist in the cart, and if the newly added product ID exist in the cart i'm only adding the qty to existing product in the cart.

    public function store($id)
    {

        $product = Product::find($id);
 
        if(!$product) {
 
            abort(404);
 
        }
 
        $cart = session()->get('cart');
 
        // if cart is empty then this the first product
        if(!$cart) {
 
            $cart = [
                    $id => [
                        "name" => $product->name,
                        "qty" => $request->quantity,
                        "price"=>$product->price,
                        "color" => $request->color,
                        "size" => $request->size,
                        "image" => $product->poster,
                    ]
            ];
 
            session()->put('cart', $cart);
        
            return redirect()->back()->with('success', 'Product added to cart successfully!');
        }
 
        // if cart not empty then check if this product exist then increment quantity
        if(isset($cart[$id])){
            
                $cart[$id]['qty'] = $request->quantity;
     
                session()->put('cart', $cart);
     
                return redirect()->back()->with('success', 'Product added to cart successfully!');
 
        }
 
        // if item not exist in cart then add to cart with quantity
        $cart[$id] = [

            "name" => $product->name,
            "qty" => $request->quantity,
            "price"=>$product->price,
            "color" => $request->color,
            "size" => $request->size,
            "image" => $product->poster,

        ];
 
        session()->put('cart', $cart);
 
        return redirect()->back()->with('success', 'Product added to cart successfully!');


    }

I'm updating the the app product so every product has multiple options (size, Color and qty), so basically a user can add the same product to the cart multiple times as not and it has deferent option (size, color, qty).

a good example for this will be a shirt? a customer can add same shirt multiple times in the cart as long as the (size or colors ) are deferent

any idea on how to achive this type of cart.

0 likes
4 replies
martinbean's avatar

@nhayder Traditionally, each variant of a product has an individual SKU (stock-keeping unit). So a red t-shirt will have a different SKU to a black t-shirt. A large shirt will have a different SKU to a medium shirt. So, I usually group SKUs under a parent product record.

If you have a product that has many SKUs, you can then have a single product details page in your website, and when a customer clicks through, they’re presented with all the different size, colour, etc. options for that product. Based on the combination selection, that will match a SKU, and you would add the SKU, not the product, to your cart.

So you can think of “product” as a top-level description (name, description) but SKUs as a particular configuration. If you work with SKUs in your cart, a customer can then add multiple variations of a product, and see them listed individually.

1 like
nhayder's avatar
Level 13

@martinbean unfortunately i'm way ahead make this type of change to the app now.

you seen, the main issue that i'm facing with the cart code above is that i'm building the cart according to product id.

and checking it if the ide exist in the cart to code the logic, SKU can solve this issue but i dont think it doable at this point.

automica's avatar

its never too late to refactor code. if you aren't using unique identifiers in form of SKU codes you are going to cause so much problems down the line.

even if you don't have a SKU as a separate field, constructing one on the fly which could be a combination of ID, SIZE + COLOUR.

Will make it much easier when you are processing the orders too.

1 like
martinbean's avatar

@nhayder It’s always better to refactor when you discover a missing domain concept (i.e. SKUs) than trying to hammer a square peg in a round hole, because that never ends up being the better solution in the long run, no matter how far you’re into your project.

Please or to participate in this conversation.