Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Shopping cart quantity counter incrementer?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 531
    Answer it

    Hello all, I am creating my first shopping cart in cakephp but got stuck while updating the product quantity. I want to update the product quantity using a session variable. Problem here is that when ever I am adding the same product to the cart it is added as a new item to the list but what I want is the quantity counter should increment to 1 when I select the product that I have already selected. I am using cakephp version 2.8.1. Can anybody help me with the code?

    Here is my Cart.ctp code:

    <?php
    /* display message saved in session if any */
    echo $this->Session->flash();
    ?>
    
    <h2 style="text-align:center;color:white">Cart</h2>
    <div>
        <?php 
          //link to products page
          echo $this->Html->link('Back To Product List', array('action' => 'product')); 
        ?>
    </div>
    <table align="center">
        <tr>
            <th>Name</th>
            <th>Price</th>
            <th>Action</th>
        </tr>
        <?php $totalPrice = 0; ?>
        <?php foreach ($cart as $key => $product): ?>
        <tr>
            <td>
                <?php 
                  //link to product page
                  echo $this->Html->link($product['Product']['product_id'], array('action'=> 'view', $product['Product']['id'])); 
                ?>
            </td>
            <td>
                <?php 
                  //show product price
                  echo $product['Product']['product_price']; 
                ?>
            </td>
            <td>
                <?php 
                  //remove product from a cart
                  echo $this->Html->link('delete', array('action' => 'delete',$key)); 
                ?>
            </td>
        </tr>
        <?php 
        //calculate total price of all products in a cart
        $totalPrice = $totalPrice + $product['Product']['product_price']; 
        ?>
    <?php endforeach; ?>
        <tr>
            <th>Total Price: </th>
            <th><?php 
              //show total price
               echo $totalPrice; 
               ?>
            </th>
            <th>
              <?php
                //delete all elements from a cart
                echo $this->Html->link('empty', array('controller'=>'users','action'=>'empty_cart'));
              ?>
            </th>
        </tr>
    </table>
    

     

    UserController code:

     

    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));  
          
            //pr($ans);die;
            $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'));
        }

     

    Any help will be appreciated. Thanks in advance.

 0 Answer(s)

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: