0

我正在尝试在 Codeingiter 购物车中添加负价格,但不能。

$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取自数据库,是十进制数-20

当我使用此代码时,我将商品放入购物车,但它会将其变为 (+)20 而不是 -20。有什么理由和帮助吗?

4

2 回答 2

2

您应该只在 . 之后添加 - 号。在这个函数中

preg_replace('/([^0-9\.])/i', '', $items['price'])

在购物车库中存在此处 system/libraries/Cart.php 第 194 行:添加后将如下所示

preg_replace('/([^0-9\.-])/i', '', $items['price'])
于 2014-12-18T18:36:47.227 回答
1

检查 system/libraries/Cart.php 第 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;
        }

所以基本上codeigniter只接受正值,您可以更改这些行,但升级时要小心,或者您可以扩展库类并添加另一个自定义键。

于 2013-08-25T11:50:57.743 回答