I'm trying to add a negative price in Codeingiter Cart but cannot.
$coupon = array(
'id' => $result->id,
'name' => $result->name,
'qty' => '1',
'price' => $result->discount,
'options' => array(
'info' => 'coupon',
'qty_description' => '')
);
$this->cart->insert($coupon);
Where $result->discount
is taken from a DB and is a Decimal number -20
.
When I use this code I get the item in the cart but it turns it into (+)20 and not -20. Any reason and help on this?
Check the cart library in system/libraries/Cart.php line #194:
// Prep the price. Remove anything that isn't a number or decimal point.
$items['price'] = trim(preg_replace('/([^0-9\.])/i', '', $items['price']));
// Trim any leading zeros
$items['price'] = trim(preg_replace('/(^[0]+)/i', '', $items['price']));
// Is the price a valid number?
if ( ! is_numeric($items['price']))
{
log_message('error', 'An invalid price was submitted for product ID: '.$items['id']);
return FALSE;
}
so basically codeigniter is only accepting positive values, you can change those lines but be-careful when upgrading or you can extend the library class and add another custom key.
You should only add a - sign after . in this function
preg_replace('/([^0-9\.])/i', '', $items['price'])
in cart library exist here system/libraries/Cart.php line #194: after adding it will be look like
preg_replace('/([^0-9\.-])/i', '', $items['price'])