简体   繁体   English

Codeigniter购物车无法添加商品

[英]Codeigniter Cart cannot add items

So i'm trying to create an application with cart and when i tried adding the item, it's not working. 所以我正在尝试使用购物车创建一个应用程序,当我尝试添加该项目时,它无法正常工作。 By the way i already have a working cart application that's why i'm wondering why it's not working. 顺便说一句,我已经有一个工作车应用程序,这就是为什么我想知道为什么它不工作。 I almost copied everything from the working one. 我几乎从工作中复制了所有东西。 here's the code 这是代码

Cart Controller 推车控制器

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

class Cart extends CI_Controller {

public function __construct()
{
    parent::__construct();
    $this->load->library('cart');
}

public function add_to_cart(){

    $id = $this->uri->segment(3);
    if($this->cart->contents()){
        foreach ($this->cart->contents() as $item){
            if ($item['id']==$id){
                $data = array('rowid'=>$item['rowid'],
                    'qty'=>++$item['qty']);
                $process = $this->cart->update($data);
            }
            else{
                $data = array(
                    'id'=>$id,
                    'qty'=>1,                       
                    'name' => $this->get_data->get_value('product_name','products','product_id', $id),
                    'price' => $this->get_data->get_value('product_price','products','product_id', $id)
                    );
                $process = $this->cart->insert($data);
            }
        }
    }
    else{
        $data = array('id'=>$id,
                    'qty' =>1,
                    'name' => $this->get_data->get_value('product_name','products','product_id', $id),
                    'price' => $this->get_data->get_value('product_price','products','product_id', $id),
                    );
                    $process = $this->cart->insert($data);
    }


    if($process){
        $this->session->set_flashdata('success', 'Successful');
        redirect('products');
    }
    else{
        $this->session->set_flashdata('failed', 'Failed');
        redirect('products');
        //var_dump($process);
    }
}

Here's the button 这是按钮

<div class="button pull-right" style="margin-top: 10px;"><a href="<?php echo base_url().'cart/add_to_cart/'.$row->product_id;?>"><span class="glyphicon glyphicon-shopping-cart"></span>Add to Cart</a></div>

I really can't see the problem, i'm using session database, the sess_us_database is already TRUE. 我真的看不出问题,我正在使用会话数据库,sess_us_database已经是TRUE。 I tried using var_dump($process) and it's false, i tried var_dump($data) and the data seems to be fine but the insert part isn't working. 我尝试使用var_dump($process)并且它是假的,我尝试了var_dump($data)并且数据看起来很好但插入部分不起作用。 Any idea guys? 伙计们好吗? it would be a big help for me, thank you. 对我来说这将是一个很大的帮助,谢谢。

CI Default cart allows only alpha-numeric, dashes, underscores, colons or periods in Product Name and If Price of product is 0 then also it will not add the product to cart. CI默认购物车仅允许产品名称中的alpha-numeric, dashes, underscores, colons or periods ,如果产品价格为0则它也不会将产品添加到购物车。

Please check them first. 请先检查一下。

A nice way to change this variable is putting a MY_Cart.php on your application\\libraries\\MY_Cart.php with this code: 更改此变量的一个好方法是使用以下代码将MY_Cart.php放在application \\ libraries \\ MY_Cart.php上:

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

class MY_Cart extends CI_Cart {

    var $product_name_rules = '[:print:]';

}

Check if you forgot on of the following : 检查您是否忘记了以下内容:

// Does the $items array contain an id, quantity, price, and name? // $ items数组是否包含id,数量,价格和名称? These are required 这些都是必需的

1 - price must be number and greater than 0 . 1 - 价格必须是数字且大于0。 2 - name must be on alpha 3 - quantity be number and greater than 0 . 2 - 名称必须在alpha 3上 - 数量为数字且大于0。 4 - must set id 4 - 必须设置id

and if you need to add any name you can remove this code : 如果您需要添加任何名称,可以删除此代码:

if ($this->product_name_safe && ! preg_match('/^['.$this-product_name_rules.']+$/i'.(UTF8_ENABLED ? 'u' : ''), $items['name']))
    {
        log_message('error', 'An invalid name was submitted as the product name: '.$items['name'].' The name can only contain alpha-numeric characters, dashes, underscores, colons, and spaces');
        return FALSE;
    }

from system/libraries/Cart.php : line 227 来自system / libraries / Cart.php:第227行

When product add into cart replace special character from product name. 当产品添加到购物车时,从产品名称中替换特殊字符。

Example: EKEN H9R / H9 Action Camera Ultra HD 4K / 25fps WiFi 2.0" 170D Underwater Waterproof Helmet Video Recording 示例:EKEN H9R / H9运动相机超高清4K / 25fps WiFi 2.0“170D水下防水头盔视频录制

// Trim special character from text
    public function trim_special_char($text)
    {
        $str = str_replace("(", '_:', $text);
        $str = str_replace(")", ':_', $str);
        $str = str_replace("/", '_slash', $str);
        $str = str_replace("+", '_plus', $str);
        $str = str_replace("&", '_and', $str);
        $str = str_replace("'", '_ss', $str);
        $str = str_replace("x", '_X', $str);
        $str = str_replace('"', '_cot', $str);

        return $str;
    }

    // Set special character from previous text
    public function set_special_char($text)
    {
        $str = str_replace('_:',  "(", $text);
        $str = str_replace(':_', ")", $str);
        $str = str_replace('_slash', "/", $str);
        $str = str_replace('_plus', "+", $str);
        $str = str_replace('_and', "&", $str);
        $str = str_replace('_ss', "'", $str);
        $str = str_replace('_X', "x", $str);
        $str = str_replace('_cot', '"', $str);

        return $str;
    }

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

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