Skip to content
Advertisement

Adding Item to cart not working codeigniter

I am trying to add item to cart, but its not adding. I am always getting message that cart is empty. Below is my code:

controller:

  $data = array(
            'id'    => $product_id,
            'qty'   => 1,
            'price' => $product_data->prod_price,
            'name'  => $product_data->prod_name,
        );
        print_r($data);

        $this->cart->insert($data);
        if (!$this->cart->contents()) {
            echo '<br/><br/>No Item in Cart';
        }

Output: Array ( [id] => 1 [qty] => 1 [price] => 150.00 [name] => CALPOT-1L(10GM%) [Free] )

No Item in Cart

Advertisement

Answer

As per the docs

You have to fill 4 required fields id,qty,price,name if they aren’t filled you can’t able to insert data so make sure you have all four values

and then you need to check like this using count():

count($this->cart->contents()) returns the size of array.

if (count($this->cart->contents()) == 0)
            {
                 echo 'No Item in Cart';
        } else{
           print_r($this->cart->contents());
        }
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement