简体   繁体   English

将项目添加到购物车不起作用 codeigniter

[英]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] )输出:数组([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您必须填写 4 个必填字段id,qty,price,name如果未填写,您将无法插入数据,因此请确保您拥有所有四个值

and then you need to check like this using count() :然后你需要使用count()像这样检查:

count($this->cart->contents()) returns the size of array. count($this->cart->contents())返回数组的大小。

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只需更改location-project_folder/system/libraries/cart.php 中$product_name_rules规则

<?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 .这将允许将所有字符(数字和非数字字符)保存在product_name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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