[英]Adding Item to cart not working codeigniter
我正在嘗試將項目添加到購物車,但它沒有添加。 我總是收到消息說購物車是空的。 下面是我的代碼:
控制器:
$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';
}
輸出:數組([id] => 1 [qty] => 1 [price] => 150.00 [name] => CALPOT-1L(10GM%) [Free])
購物車中沒有商品
根據文檔
您必須填寫 4 個必填字段
id,qty,price,name
如果未填寫,您將無法插入數據,因此請確保您擁有所有四個值
然后你需要使用count()
像這樣檢查:
count($this->cart->contents())
返回數組的大小。
if (count($this->cart->contents()) == 0)
{
echo 'No Item in Cart';
} else{
print_r($this->cart->contents());
}
你的名字有不允許的特殊字符
只需更改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';
}
這將允許將所有字符(數字和非數字字符)保存在product_name
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.