$infoProduct = $this->Product->find('first', array( 'conditions' => array( 'Product.id' => $this->request->data['id'] ) )); if(!empty($infoProduct)){ $key = $this->request->data['id']; if($this->Session->check('cart')){ $infoCart = $this->Session->read('cart'); $infoCart[$key]['Cart']['Product']['id'] = $infoProduct['Product']['id']; $infoCart[$key]['Cart']['Product']['title'] = $infoProduct['Product']['title']; $infoCart[$key]['Cart']['Product']['thumbnail'] = $infoProduct['Product']['thumbnail']; $infoCart[$key]['Cart']['Product']['price'] = $infoProduct['Product']['price']; $infoCart[$key]['Cart']['Order']['quantity'] = $this->request->data['quantity']; $infoCart[$key]['Cart']['Order']['pay'] = $this->request->data['quantity'] * $infoProduct['Product']['price']; $this->Session->write('cart', $infoCart); } else { $infoCart[$key]['Cart']['Product']['id'] = $infoProduct['Product']['id']; $infoCart[$key]['Cart']['Product']['title'] = $infoProduct['Product']['title']; $infoCart[$key]['Cart']['Product']['thumbnail'] = $infoProduct['Product']['thumbnail']; $infoCart[$key]['Cart']['Product']['price'] = $infoProduct['Product']['price']; $infoCart[$key]['Cart']['Order']['quantity'] = $this->request->data['quantity']; $infoCart[$key]['Cart']['Order']['pay'] = $this->request->data['quantity'] * $infoProduct['Product']['price']; $this->Session->write('cart', $infoCart); } $numOrder = 0; foreach ($this->Session->read('cart') as $key => $listCartitem) { $numOrder++; } echo $numOrder; };