i am working on a cart system Plugin, i am setting a variable $cart in cart controller, and now i have to use the same variable in dish controller , how to do this? here is my code, i am working with:
Cart Controller:
class CartsController extends CartAppController {
public function view($cartId = null) {
if (!empty($this->request->data)) {
$this->CartManager->updateItems($this->request->data['CartsItem']);
}
if (!empty($cartId)) {
$cart = $this->Cart->view($cartId, $this->Auth->user('id'));
} else {
$cart = $this->CartManager->content();
}
$this->request->data = $cart;
$this->set('cart', $cart); // This $cart variable is needed to be used in a different controller.
$this->set('requiresShipping', $this->CartManager->requiresShipping());
}
}
Dish Controller
class DishesController extends AppController {
public function index($id='') {
**// here i need to print that **$cart** variable**
$this->layout = false;
$menu=$this->Menu->find('first', array('conditions' => array("Menu.id"=>$id),'limit'=>1));
if(empty($id) || empty($menu)){
$this->redirect(array('controller'=>'menus', 'action'=>'index'));
}
$dishes=$this->Dish->find('all', array('conditions' => array("Dish.status"=>"1", "Dish.menu_id"=>$id), 'limit'=>9, 'recursive'=>0, 'order' => array('Dish.id' => 'DESC')));
$this->set('menu_name',$menu['Menu']['name']);
$this->set('dishes',$dishes);
$this->set('page_name','dishes');
}
}
Any help would be appreciated :)
Store your $cart
data in the database and then store the record's primary key in a session called something like cart_id
:-
$this->Session->write('cart_id', $this->Cart->id);
You can then get the cart from other controllers using the session data to retrieve it from the database:-
$this->loadModel('Cart');
$this->Cart->findById($this->Session->read('cart_id'));
(Above is just an example and can be written much better depending on your particular needs.)
This tends to be the way we approach an ecommerce site. I personally think it makes sense to store the data in the database and just keep the key in the session.