简体   繁体   中英

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

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

你的名字有不允许的特殊字符

Just change the $product_name_rules rules in location- project_folder/system/libraries/cart.php

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

class MY_Cart extends CI_Cart {
    var $product_name_rules = '\d\D';
}

This will allow all characters (digits and non-digit characters) to be saved in product_name .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM