在Cart codeigniter类中将0存储为价格

时间:2014-03-28 10:56:40

标签: php codeigniter cart extends

在我的申请中,我的产品中既有免费门票,也有付费门票,用户可以添加免费门票和付费门票。如果价格为0,则购物车类将无法添加产品。如果价格为0,则在购物车中添加产品,我必须扩展购物车类,但没有任何问题。帮助赞赏。

<?php

    class MY_Cart extends CI_Cart
    {
        function __construct()
        {
            parent::__construct();
        }



    function insert_item($items = array())
    {
        // Was any cart data passed? No? Bah...
        if ( ! is_array($items) OR count($items) == 0)
        {
            log_message('error', 'The insert method must be passed an array containing data.');
            return FALSE;
        }

        $save_cart = FALSE;
        if (isset($items['id']))
        {
            if (($rowid = $this->_insert_item($items)))
            {
                $save_cart = TRUE;
            }
        }
        else
        {
            foreach ($items as $val)
            {
                if (is_array($val) AND isset($val['id']))
                {
                    if ($this->_insert_item($val))
                    {
                        $save_cart = TRUE;
                    }
                }
            }
        }

        // Save the cart data if the insert was successful
        if ($save_cart == TRUE)
        {
            $this->_save_cart();
            return isset($rowid) ? $rowid : TRUE;
        }

        return FALSE;
    }

    function _insert_item($items = array())
    {
        // Was any cart data passed? No? Bah...
        if ( ! is_array($items) OR count($items) == 0)
        {
            log_message('error', 'The insert method must be passed an array containing data.');
            return FALSE;
        }

        // --------------------------------------------------------------------

        // Does the $items array contain an id, quantity, price, and name?  These are required
        if ( ! isset($items['id']) OR ! isset($items['qty']) OR ! isset($items['price']) OR ! isset($items['name']))
        {
            log_message('error', 'The cart array must contain a product ID, quantity, price, and name.');
            return FALSE;
        }

        // --------------------------------------------------------------------

        // Prep the quantity. It can only be a number.  Duh...
        $items['qty'] = trim(preg_replace('/([^0-9])/i', '', $items['qty']));
        // Trim any leading zeros
        $items['qty'] = trim(preg_replace('/(^[0]+)/i', '', $items['qty']));

        // If the quantity is zero or blank there's nothing for us to do
        if ( ! is_numeric($items['qty']) OR $items['qty'] == 0)
        {
            return FALSE;
        }

        if ( ! preg_match("/^[".$this->product_id_rules."]+$/i", $items['id']))
        {
            log_message('error', 'Invalid product ID.  The product ID can only contain alpha-numeric characters, dashes, and underscores');
            return FALSE;
        }

        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;
        }

        // --------------------------------------------------------------------

        // Prep the price.  Remove anything that isn't a number or decimal point.
        $items['price'] = trim(preg_replace('/([^0-9\.])/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;
        }

        if (isset($items['options']) AND count($items['options']) > 0)
        {
            $rowid = md5($items['id'].implode('', $items['options']));
        }
        else
        {
            $rowid = md5($items['id']);
        }

        // --------------------------------------------------------------------

        // Now that we have our unique "row ID", we'll add our cart items to the master array

        // let's unset this first, just to make sure our index contains only the data from this submission
        unset($this->_cart_contents[$rowid]);

        // Create a new index with our new row ID
        $this->_cart_contents[$rowid]['rowid'] = $rowid;

        // And add the new items to the cart array
        foreach ($items as $key => $val)
        {
            $this->_cart_contents[$rowid][$key] = $val;
        }

        // Woot!
        return $rowid;
    }

2 个答案:

答案 0 :(得分:1)

你可以在第198行发表评论

$items['qty'] = trim(preg_replace('/(^[0]+)/i', '', $items['qty']));

答案 1 :(得分:0)

更改数字number_format()

的格式
number_format($variable, 2, '.', ',')

为什么你使用preg_match来检查数字的价格有多少功能在PHP中为此检查数字,如试试

is_numeric($items['price'])