PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, March 15, 2022

[FIXED] Adding Item to cart not working codeigniter

 March 15, 2022     codeigniter, php     No comments   

Issue

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


Solution

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());
        }


Answered By - Mr. Pyramid
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing