Welcome Guest, Not a member yet? Register   Sign In
Cart class doesn't accept product names with accented characters

Hi everyone,

I'm currently developing an e-commerce site for a wine merchant. I'm using CI's cart class, which as you probably know has this syntax for adding items to the cart:
$data = array(
               'id'      => 'sku_123ABC',
               'qty'     => 1,
               'price'   => 39.95,
               'name'    => 'T-Shirt',
               'options' => array('Size' => 'L', 'Color' => 'Red')


This works fine for me - except when the name of the item is something along the lines of "Côtes du Rhône rouge 37.5 cl 2007", or "Crème d'Abricot". Obviously the accented characters are preventing insertion of the product to the cart.

I've tried the following within the product's shopping cart form to "neutralize" the accented characters:

<?php echo form_hidden('name', ascii_to_entities($product_name)); ?>

... but even with that function in use, I cannot insert the item in the cart.

Am I doing something wrong? How can I get this to work?

Thanks for your help! :-)

I've had a look at /libraries/Cart.php and saw this at the top:

var $product_name_rules    = '\.\:\-_ a-z0-9';

... so it's the regex that's preventing my accented product names from being added to the cart.

How do I change this regex so that it will accept accents (ô, é, è, à, û, ç) and/or any characters that are produced by the ascii_to_entities() function (see Text Helper)?

Right, well ended up commenting out this bit in Cart.php:
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;

That'll have to do until I find a proper regex...

You could also extend the Cart library and add the characters you want allowed.
<?php if (!defined('BASEPATH')) exit('No direct access allowed.');
class MY_Cart extends CI_Cart {
  var $CI;

  function MY_Cart()
    parent::CI_Cart(); //this may not be required

    $this->CI =& get_instance(); //this may not be required

    $this->product_name_rules    = '\.\:\-_ a-z0-9\\\(\)\/,';

I've had issues with this too, and use [:print:] in my regex, but just found that it won't allow pound signs "#", which I like. I think you just have to customize the regex to your liking, and learning regex is part of the fun.

We used:

$this->ci->cart->product_name_rules = '^.';

Allows everything as a name.

Theme © iAndrew 2016 - Forum software by © MyBB