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

[eluser]mudshark[/eluser]
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:
Code:
$data = array(
               'id'      => 'sku_123ABC',
               'qty'     => 1,
               'price'   => 39.95,
               'name'    => 'T-Shirt',
               'options' => array('Size' => 'L', 'Color' => 'Red')
            );

$this->cart->insert($data);

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:

Code:
<?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! :-)
#2

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

Code:
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)?
#3

[eluser]mudshark[/eluser]
Right, well ended up commenting out this bit in Cart.php:
Code:
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...
#4

[eluser]ontguy[/eluser]
You could also extend the Cart library and add the characters you want allowed.
Code:
<?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\\\(\)\/,';
  }
#5

[eluser]skunkbad[/eluser]
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.
#6

[eluser]Unknown[/eluser]
We used:

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

Allows everything as a name.




Theme © iAndrew 2016 - Forum software by © MyBB