Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make shopping cart in Codeigniter

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 673
    Comment on it

    Hello Reader's if you are developing the project in Codeignter then CI offers you a lot more featured libraries. Shopping cart is on of them. Now in this blog we will see how to develop shopping cart by Library.

    Suppose your website offers you the product with attribute id,qty,price,name and option.
    Then you just need to fill every product booked by user in a array. This array can be also multidimensional. Lets say array name is $UserBookedArray.

    $UserBookedArray= array(
                   array(
                           'id'      => 'sku_123ABC',
                           'qty'     => 1,
                           'price'   => 39.95,
                           'name'    => 'T-Shirt',
                           'options' => array('Size' => 'L', 'Color' => 'Red')
                        ),
                   array(
                           'id'      => 'sku_567ZYX',
                           'qty'     => 1,
                           'price'   => 9.95,
                           'name'    => 'Coffee Mug'
                        ),
                   array(
                           'id'      => 'sku_965QRS',
                           'qty'     => 1,
                           'price'   => 29.95,
                           'name'    => 'Shot Glass'
                        )
                );
    

    Now in your constructor load the 'shopping cart' library by the syntax as below:-

    $this->load->library('cart');
    

    Now in the last of code you have to insert the $UserBookedArray array in this shopping cart. And its code will go like this:-

    $this->cart->insert($data);
    

    Now finally its time to show your cart. So create a view display.php call load it. Its code will go like this:-

    <?php echo form_open('path/to/controller/update/function'); ?>    
    <table cellpadding="6" cellspacing="1" style="width:100%" border="0">    
    <tr>
      <th>QTY</th>
      <th>Item Description</th>
      <th style="text-align:right">Item Price</th>
      <th style="text-align:right">Sub-Total</th>
    </tr>    
    <?php $i = 1; ?>    
    <?php foreach ($this->cart->contents() as $items): ?>    
        <?php echo form_hidden($i.'[rowid]', $items['rowid']); ?>    
        <tr>
          <td><?php echo form_input(array('name' => $i.'[qty]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?></td>
          <td>
            <?php echo $items['name']; ?>    
                <?php if ($this->cart->has_options($items['rowid']) == TRUE): ?>    
                    <p>
                        <?php foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value): ?>
    
                            <strong><?php echo $option_name; ?>:</strong> <?php echo $option_value; ?><br />    
                        <?php endforeach; ?>
                    </p>    
                <?php endif; ?>    
          </td>
          <td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td>
          <td style="text-align:right">$<?php echo $this->cart->format_number($items['subtotal']); ?></td>
        </tr>    
    <?php $i++; ?>    
    <?php endforeach; ?>    
    <tr>
      <td colspan="2"> </td>
      <td class="right"><strong>Total</strong></td>
      <td class="right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
    </tr>    
    </table>    
    <p><?php echo form_submit('', 'Update your Cart'); ?></p>
    

    Now when this view will load you can see all the entries are showing .The benefit of using the shopping cart library is you never have to assign or pass the details from Controller to view. You can display any cart item from any view. Similarly you can update/delete your cart in the same way.

 0 Comment(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: