Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter Cart Problem
#1

[eluser]codeworxx[/eluser]
Hey Guys,

i'm working on a huge Webshop System and have a Problem with the Cart Library.
I have a Detail page of a Product where in input Box is created to set the Amount you wish to add to the cart.

If added, the product should be added to the cart by an jQuery Ajax call and the shopping cart has to be updated.

First of all, if i trace and Debug the Step - i always get the right values - everything is okay:

Here's my jQuery:

Code:
$("#products form").submit(function() {
        var postdata = $(this).serialize();

        $.ajax({
           type: "POST",
           url: link + 'addtocart/add_cart_item',
           data: postdata,
           success: function(msg){
                if( msg == 'OK' )
                {
                    $.get( link + 'addtocart/show_cart', function(data) {
                        $('.shoppingcart p.total').html( data );
                    });
                } else
                {
                    alert('Fehler');    
                }
           }
         });
        
        return false;
    });

This is my addtocart controller:
Code:
class Addtocart extends Controller { // Our Cart class extends the Controller class

    function Addtocart()
    {
        parent::Controller(); // We define the the Controller class is the parent.
    }
    
    function index()
    {
        // Nothing to be done here
    }
    
    function add_cart_item()
    {    
        $this->load->model('cart_model');
        if( $this->cart_model->validate_add_cart_item() == TRUE ){        
            echo 'OK';
        } else
        {
            echo 'FEHLER';
        }
    }
    
    function show_cart(){
        $this->load->view('system/cart');
    }
    
}

this is my cart model
Code:
class Cart_model extends Model {
    
    function validate_add_cart_item(){    
        $ret = '';
        
        $id = $this->input->post('product_id'); // Assign posted product_id to $id
        if(is_numeric($this->input->post('quantity')))
        {
            $cty = $this->input->post('quantity');
        } else
        {
            $cty = 1;
        }    
        $update = FALSE;
        foreach($this->cart->contents() as $item)
        {
            if($item['id'] == $id)
            {
                $update_data = array(
                        'rowid'    => $item['rowid'],
                        'qty'    => $item['qty'] + $cty
                );
                $this->cart->update($update_data);
                $update = TRUE;
            }
        }
        if($update != TRUE)
        {        
            $this->db->where('products_id', $id);
            $query = $this->db->get('products', 1);

            if($query->num_rows > 0){
                foreach ($query->result() as $row)
                {
                    $data = array(
                        'id'      => $id,
                        'qty'     => $cty,
                        'price'   => $row->products_price,
                        'name'    => $row->products_name
                    );
    
                    $this->cart->insert( $data );
                    return TRUE;
                }
                
            }else
            {
                return FALSE;
            }
        } else
        {
            return TRUE;    
        }
    }

}

if i let this code run through ajax, it does not work and i do not get any error messages.

if i change my controller to add a product manually:
Code:
class Addtocart extends Controller { // Our Cart class extends the Controller class

    function Addtocart()
    {
        parent::Controller(); // We define the the Controller class is the parent.
    }
    
    function index()
    {
        // Nothing to be done here
    }
    
    function add_cart_item()
    {    
        $this->load->model('cart_model');
        if( $this->cart_model->validate_add_cart_item() == TRUE ){
        /* Manually add Product to cart */    
        $data = array(
            'id'      => '3',
            'qty'     => '5',
            'price'   => 25.55,
            'name'    => 'Demo'
            );
    
                $this->cart->insert( $data );
            
            echo 'OK';
        } else
        {
            echo 'FEHLER';
        }
    }
    
    function show_cart(){
        $this->load->view('system/cart');
    }
    
}

and run it through index.php/addtocart/add_cart_item it works.

the view is this one:
Code:
$totalitem = count($this->cart->contents());
      $totalprice = $this->cart->total();
      if( $totalitem != 0 )
      {
          echo $totalitem.' Artikel: '.$totalprice;
      } else
      {
          echo 'Keine Artikel gewählt';
      }

If i dump my Session Variable i have a *RECURSION* near the Cart Controller. Could that be the Problem and how to fix??

I have downloaded a Demo Cart Tutorial from Nettuts+ and it worked?!?!?

Hope you can help?!?!

Greetings,
Sascha
#2

[eluser]Sbioko[/eluser]
Maybe, because jQuery does not "understand" serialized data in data option of $.ajax. Check the jQuery's Docs.
#3

[eluser]codeworxx[/eluser]
Thanks for the reply. I have checked the docs and traced the outputs by "Alert"... All is okay... I have no idea...




Theme © iAndrew 2016 - Forum software by © MyBB