-
Item Add To cart
almost 9 years ago
-
over 8 years ago
I have done this using cakephp. Hope this will be helpful to you. To add product to your cart add the following function to your Controller file:
public function product(){ //find all products $products = $this->Product->find('all'); //set counter to display number of products in a cart $counter = 0; if ($this->Session->read('Counter')) { $counter = $this->Session->read('Counter'); } //pass variable to view $this->set(compact('products', 'counter')); } function cart($id=null) { $this->Product->id = $id; if(!$this->Product->exists()) { throw new NotFoundException(__('Invalid product')); } $productsInCart = $this->Session->read('Cart'); //pr($productsInCart);die; $amount=count($productsInCart); $this->Session->write('Cart.' . $amount, $this->Product->read(null, $id)); $this->Session->write('Counter', $amount + 1); $this->Session->setFlash(__('Product added to cart')); $this->redirect('showcart'); } function showcart() { $cart = array(); if ($this->Session->check('Cart')) { $cart = $this->Session->read('Cart'); } $this->set(compact('cart')); }
and then create the view file for product and showcart functions. Kindly make changes in the terms accordingly. All the best!
-
1 Answer(s)