0

I have two table with OneToMany relation eg:

item

id|product_name|price|description
id|product xyz |12$  |blah blah

sizeAttriButes

 id: size
  1: XL
  2: XXL

On the product view page the size is in Dropdown.

what I want to achieve here is when I click on the add to cart I want to fetch exact size attr in Cart. In single add to cart, it works, but in multi product the size is repeating, I want to achieve the following:

        id|product_name|price|description|size
        1 |product xyz |12$  |blah blah  | XL
        2 |product abc |15$  |blah blah  | XXL

I'm doing following:

        $basket = $session->get('basket',[]);
        if($request->isMethod('POST')){
            $product_size = $request->get('size');
            $basket[$product->getId()] = $product;
            $session->set('basket', $basket);   
            $session->set('size', $product_size);    
        }    

cart:

            {

    $basket = $session->get('basket',[]);
    $size = $session->get('size');
    if($request->isMethod('POST')){
        unset($basket[$request->request->get('id')]);
        $session->set('basket', $basket);
    }

    $price = array_sum(array_map(function($product){
       return $product->getPrice();
    },
    $basket
));

    return $this->render('cart/cart.html.twig',[
        'basket' => $basket,
        'price' => $price,
        'size' => $size
    ]);
}

enter image description here enter image description here

I might be doing wrong.. Please guide me...

5
  • 1
    Does this answer your question? Put arrays in session symfony2 Commented Jun 9, 2021 at 16:46
  • You haven't described a problem or included any errors. Can you please tell us what happens when you run/employ your code. Commented Jun 9, 2021 at 19:14
  • @Burkely91 there is no error ,but Im lacking in logic. As said above when customer clicks on size(from dropdown) I want to display in cart product data along with size clicked by customer. In single product it working but in multiple same size is repeating. Commented Jun 10, 2021 at 5:33
  • Can you show any output then if there are no errors? It helps us solve the problem if we can see the code you've written as well as it's output. Commented Jun 10, 2021 at 8:02
  • @Burkely91 I have added the image. Commented Jun 10, 2021 at 9:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.