Welcome Guest, Not a member yet? Register   Sign In
problem with Cart Library, Cart Class
#1

[eluser]sihijau[/eluser]
i think therese bugs in the cart class, because i try using this library when i insert new item or update cart, the item in shoppingcart is reset to 1 item.
my problem exactly same tothis problem

my controller code:
Code:
<?php
class cart extends Controller{
    //
    function cart(){
        parent::Controller();
        
        $this->load->library('cart');
    }
    //
    function index(){
        
        $this->view_cart();
    }
    
    function add(){
        $pid = $this->input->post('product_id');
        //
        $q = $this->db->get_where('product',array('product_id'=>$pid),1);
        if($q->num_rows() > 0){
            $item = $q->row();
            
            //
            $data = array('id' => $item->product_id,
                          'qty' => 1,
                          'price' => $item->product_price,
                          'name' => $item->product_sku."".$item->product_name
            );
            
            $this->cart->insert($data);
        }
        redirect('cart/view_cart');
    }
    
    ////
    function view_cart(){
        
        $data['custom_jquery'] = '
        $("input[name=\'delete\']").click(function(){
           var status = $(this).val();
           location.href = "'.site_url('cart/delete').'/" + status;
        })';
        $data['ptitle'] = 'CoderShop | View Cart';
        $this->load->view('view_cart',$data);
    }
    
    function update(){
        
        //Get number of items in cart
        $count = $this->cart->total_items();
        
        //Get info from POST
        $item = $this->input->post('rowid');
        $qty = $this->input->post('qty');
        
        //Step through items
        for($i=0;$i < $count;$i++)
        {
            $data = array(
               'rowid' => $item[$i],
               'qty'   => $qty[$i]
            );
            $this->cart->update($data);
        }
        
        redirect('cart/view_cart');
    }
    
    function delete()
    {
        $row_id = $this->uri->segment(3,FALSE);
        $data = array('rowid'=>$row_id,
                      'qty' => 0);
        $this->cart->update($data);
        
        redirect('cart/view_cart');
    }
}
?&gt;

could anyone help me fix this? :down:
#2

[eluser]skunkbad[/eluser]
This isn't a bug. Your code should check for the item in the cart before trying to add it twice. If the item is already in the cart, you should do an update, not an add.

So, when your controller is getting post values and determining what to do with them, that is where you would provide logic to add vs. update.

This method hasn't been added to Community Cart yet, but this is something I came up with:

Code:
public function add()
{
    // check that the input is numeric
    if(is_numeric($this->input->post('qty')))
    {
        $add_qty = $this->input->post('qty');
    }

    // if no quantity, or quantity not numeric, quantity = 1
    else
    {
        $add_qty = 1;
    }

    if($product_id = $this->input->post('product_id'))
    {
        // if item is already in cart, update the qty
        $update = FALSE;
        foreach($this->cart->contents() as $item)
        {
            if($item['id'] == $product_id)
            {
                // set cart update array
                $update_data = array(
                        'rowid'    => $item['rowid'],
                        'qty'    => $item['qty'] + $add_qty
                );

                // update the cart
                $this->cart->update($update_data);
                $update = TRUE;
            }
        }

        // if the item is not already in the cart, add it
        if($update != TRUE)
        {
            // get product price and other data from database
            $this->load->model('basket_model');
            if($add = $this->basket_model->get_product_data($product_id))
            {
                // set cart add array
                $add_data = array(
                    'id'    => $product_id,
                    'qty'    => $add_qty,
                    'price'    => $add->product_price,
                    'name'    => $add->product_name,
                    'options'=> array ('weight' => $add->product_weight )
                );
                
                // add the item to the cart
                $this->cart->insert($add_data);
            }
        }
    }

    $this->_display();
}
#3

[eluser]sihijau[/eluser]
Owwhh my add method was too simple, there's not mechanism to check that an item already in cart, thanks for pointing me that..




Theme © iAndrew 2016 - Forum software by © MyBB