Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 

 1 Answer(s)

  • 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!

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: