在CodeIgniter中删除对购物车中字符的限制

时间:2014-02-02 08:29:47

标签: php codeigniter cart

我在Codeigniter中使用购物车库。我正在使用这个:

$product = array(
  'id'      => $product_id,
  'qty'     => $product_qty,
  'price'   => $product_price,
  'name'    => $product_name,
  'attr_id' => $product_attr_id,
  'short_desc'=> $product_short_desc,
  'options' => array('size_id' => $product_size_id)
);
$this->cart->insert($product); 

某些产品未添加,因为它名称中不允许使用字符。我想删除这个限制。我在my_project/system/libraries

中对此代码进行了评论
if ( ! preg_match("/^[".$this->product_name_rules."]+$/i", $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;
}

但它还没有加入购物车。有人可以帮助我吗?

3 个答案:

答案 0 :(得分:4)

试试:

在system / libraries / Cart.php中你可以找到:

  <?php
class CI_Cart {

    // These are the regular expression rules that we use to validate the product ID and product name
    var $product_id_rules   = '\.a-z0-9_-'; // alpha-numeric, dashes, underscores, or periods
    var $product_name_rules = '\.\:\-_ a-z0-9'; // alpha-numeric, dashes, underscores, colons or periods
 ?>

不要在上面的文件中进行更改,但在应用程序/库中创建一个名为My_Cart.php的新文件,并输入以下代码:

<?php 
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Cart extends CI_Cart
{
    function __construct()
    {
        parent::__construct();
        $this->product_name_rules = '\d\D'; //This will override `$product_name_rules` rule from system/libraries/Cart.php
    }
}
?>

此解决方案涉及覆盖Cart类并清除名称限制​​。

答案 1 :(得分:1)

我在向购物车添加任何内容之前使用此功能..产品名称或ID或任何内容..

function quote_smart($string)
{
    $string = html_entity_decode($string);
    $string = strip_tags($string);
    $string = trim($string);
    $string = htmlentities($string);
    $string = preg_replace('/\s+/', ' ',$string); // Removing more than one space/Tab.

    // Quote if not integer
    if (!is_numeric($string)) 
    {
        $string = mysql_real_escape_string($string);
    }

    return $string;
}

我刚删除它并且它有效!! 感谢你的回答。

答案 2 :(得分:1)

请尝试以下代码:

$this->cart->product_name_rules = '[:print:]';
$this->cart->insert(array());