Welcome Guest, Not a member yet? Register   Sign In
Basic Cart Example
#2

[eluser]SitesByJoe[/eluser]
Controller Code:

Code:
class Shop extends Controller {

        function __construct()
        {
                parent::Controller();
                $this->load->library('cart');
        }
        
        function index()
        {
                // show a couple products
                $products = $this->db->get('products');
                foreach ($products->result() as $product)
                {
                        echo '<h3>' . anchor('shop/detail/' . $product->product_id, $product->product_name) . '</h3>';
                        echo '<p><b>' . $product->product_price . '</b></p>';
                        echo '<p>' . $product->product_short_desc . '</p>';
                        echo '<hr>';
                }
        }
        
        function detail()
        {
                // show the product and an "add to cart" link
                $product_id = $this->uri->segment(3);
                $product = $this->db->where('product_id', $product_id)->get('products')->row();
                
                echo '<h1>' . $product->product_name . '</h1>';
                echo '<p>' . $product->product_long_description . '</p>';
                echo '<p><b>' . $product->product_price . '</b></p>';
                echo form_open('shop/add_to_cart');
                echo form_hidden('product_id',$product->product_id);
                echo form_hidden('product_name', $product->product_name);
                echo form_hidden('product_price', $product->product_price);
                echo form_input('qty', '1');
                echo form_submit('', 'Add to Cart');
                echo form_close();
        }
        
        function add_to_cart()        
        {
                // add the selected product to the cart
                $data = array(
                        'id' => $this->input->post('product_id'),
                        'qty' => $this->input->post('qty'),
                        'price' => $this->input->post('product_price'),
                        'name' => $this->input->post('product_name'),
                        'options' => array()
                );
                
                $this->cart->insert($data);                
                redirect('shop/show_cart');
        }
        
        
        function update_cart()
        {
                //print_r($this->input->post('1'));
                for ($i = 1; $i <= $this->cart->total_items(); $i++)
                {
                        $item = $this->input->post($i);
                        $data = array(
                                'rowid' => $item['rowid'],
                                'qty' => $item['qty']
                        );
                        $this->cart->update($data);
                }
                redirect('shop/show_cart');
        }
        
        function show_cart()
        {
                //$this->load->view('public/shop/cart');
                ?&gt;
                &lt;?php echo form_open('shop/update_cart'); ?&gt;

                <table cellpadding="6" cellspacing="1" style="width:100%" border="0">
                
                <p>&lt;?php echo anchor('shop', 'Continue Shopping'); ?&gt;</p>
                
                <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>
                
                &lt;?php $i = 1; ?&gt;
                
                &lt;?php foreach($this->cart->contents() as $items): ?&gt;
                
                    &lt;?php echo form_hidden($i.'[rowid]', $items['rowid']); ?&gt;
                    
                    <tr>
                      <td>&lt;?php echo form_input(array('name' => $i.'[qty]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?&gt;</td>
                      <td>
                        &lt;?php echo $items['name']; ?&gt;
                                    
                            &lt;?php if ($this->cart->has_options($items['rowid']) == TRUE): ?&gt;
                                    
                                <p>
                                    &lt;?php foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value): ?&gt;
                                        
                                        <strong>&lt;?php echo $option_name; ?&gt;:</strong> &lt;?php echo $option_value; ?&gt;<br />
                                                        
                                    &lt;?php endforeach; ?&gt;
                                </p>
                                
                            &lt;?php endif; ?&gt;
                                
                      </td>
                      <td style="text-align:right">&lt;?php echo $this->cart->format_number($items['price']); ?&gt;</td>
                      <td style="text-align:right">$&lt;?php echo $this->cart->format_number($items['subtotal']); ?&gt;</td>
                    </tr>
                
                &lt;?php $i++; ?&gt;
                
                &lt;?php endforeach; ?&gt;
                
                <tr>
                  <td colspan="2"> </td>
                  <td class="right"><strong>Total</strong></td>
                  <td class="right">$&lt;?php echo $this->cart->format_number($this->cart->total()); ?&gt;</td>
                </tr>
                
                </table>
                
                <p>&lt;?php echo form_submit('', 'Update your Cart'); ?&gt;</p>
                &lt;?php
        }
        
        function clear_cart()
        {
                $this->cart->destroy();
                redirect('shop');
        }
        
}


Messages In This Thread
Basic Cart Example - by El Forum - 09-12-2009, 04:25 PM
Basic Cart Example - by El Forum - 09-12-2009, 04:26 PM
Basic Cart Example - by El Forum - 09-13-2009, 08:11 AM
Basic Cart Example - by El Forum - 09-13-2009, 08:31 AM
Basic Cart Example - by El Forum - 09-13-2009, 12:58 PM
Basic Cart Example - by El Forum - 09-30-2009, 09:00 PM
Basic Cart Example - by El Forum - 09-30-2009, 10:06 PM
Basic Cart Example - by El Forum - 10-01-2009, 07:22 AM
Basic Cart Example - by El Forum - 10-01-2009, 09:38 PM
Basic Cart Example - by El Forum - 10-07-2009, 10:58 AM
Basic Cart Example - by El Forum - 10-24-2009, 07:56 PM
Basic Cart Example - by El Forum - 10-24-2009, 10:07 PM
Basic Cart Example - by El Forum - 10-25-2009, 10:28 AM
Basic Cart Example - by El Forum - 10-25-2009, 12:55 PM
Basic Cart Example - by El Forum - 11-02-2009, 02:21 AM
Basic Cart Example - by El Forum - 11-04-2009, 04:19 AM
Basic Cart Example - by El Forum - 12-08-2009, 04:05 AM
Basic Cart Example - by El Forum - 12-09-2009, 01:19 PM
Basic Cart Example - by El Forum - 12-10-2009, 12:03 PM
Basic Cart Example - by El Forum - 12-10-2009, 12:14 PM



Theme © iAndrew 2016 - Forum software by © MyBB