简体   繁体   中英

e-commerce symfony add products to the cart

On this controller I am tring to add an item to the shopping cart, facing a lot of troubles with my session variables and I can't figure what to do, the main problem is implementing count.

My controller:

public function addProduct($id){
  
    if(!$this->get('session')->set('Carrinho', ' '));
    $tupple =  $this->eshop_model->get_product_info_id($id);


    $this->session->get['Carrinho'] [count($this->session->get['Carrinho']+1] = $tupple;
    $data['Carrinho'] = $this->session->get('Carrinho');
    $data['message'] = "Foi adicionado o item ".$tupple['name']." ao seu carrinho de compras com sucesso!";
    $size = count($data['Carrinho']);
    $sum = 0;
    $subtotal = 0;
    for($i=1; $i <= $size; $i++){
            $sum += $data['Carrinho'][$i]['price'];
            $subtotal++;
    }

    $data['sum'] =$this->session->get('sum');
    $subtotal=$this->session->get('subtotal')

    return $this->render('eshop/message.html.twig', $data);
}

Error:

Notice: Undefined property: Symfony\Component\HttpFoundation\Session\Session::$get

You are not checking any moment if this property is defined the good way to do is:

if(isset($_SESSION['Carrinho'])

You error seems to show that the property Carrinh is undefined.

You are also writing planty of time: $this->session but you should write: $this->get('session') except if you set it in constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM