Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter Cart cannot add items

So i'm trying to create an application with cart and when i tried adding the item, it's not working. By the way i already have a working cart application that's why i'm wondering why it's not working. I almost copied everything from the working one. here's the code

Cart Controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cart extends CI_Controller {

public function __construct()
{
    parent::__construct();
    $this->load->library('cart');
}

public function add_to_cart(){

    $id = $this->uri->segment(3);
    if($this->cart->contents()){
        foreach ($this->cart->contents() as $item){
            if ($item['id']==$id){
                $data = array('rowid'=>$item['rowid'],
                    'qty'=>++$item['qty']);
                $process = $this->cart->update($data);
            }
            else{
                $data = array(
                    'id'=>$id,
                    'qty'=>1,                       
                    'name' => $this->get_data->get_value('product_name','products','product_id', $id),
                    'price' => $this->get_data->get_value('product_price','products','product_id', $id)
                    );
                $process = $this->cart->insert($data);
            }
        }
    }
    else{
        $data = array('id'=>$id,
                    'qty' =>1,
                    'name' => $this->get_data->get_value('product_name','products','product_id', $id),
                    'price' => $this->get_data->get_value('product_price','products','product_id', $id),
                    );
                    $process = $this->cart->insert($data);
    }


    if($process){
        $this->session->set_flashdata('success', 'Successful');
        redirect('products');
    }
    else{
        $this->session->set_flashdata('failed', 'Failed');
        redirect('products');
        //var_dump($process);
    }
}

Here's the button

<div class="button pull-right" style="margin-top: 10px;"><a href="<?php echo base_url().'cart/add_to_cart/'.$row->product_id;?>"><span class="glyphicon glyphicon-shopping-cart"></span>Add to Cart</a></div>

I really can't see the problem, i'm using session database, the sess_us_database is already TRUE. I tried using var_dump($process) and it's false, i tried var_dump($data) and the data seems to be fine but the insert part isn't working. Any idea guys? it would be a big help for me, thank you.

like image 247
Alvinrightback Avatar asked Sep 28 '26 08:09

Alvinrightback


1 Answers

CI Default cart allows only alpha-numeric, dashes, underscores, colons or periods in Product Name and If Price of product is 0 then also it will not add the product to cart.

Please check them first.

like image 177
Rajasekhar Avatar answered Sep 30 '26 21:09

Rajasekhar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!