简体   繁体   中英

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. 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. 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.

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:

<?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? These are required

1 - price must be number and greater than 0 . 2 - name must be on alpha 3 - quantity be number and greater than 0 . 4 - must set 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

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

// 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;
    }

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