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

Php 将0作为价格存储在Cart codeigniter类中,php,codeigniter,cart,extends,Php,Codeigniter,Cart,Extends,在我的应用程序中,我的产品中既有免费票也有付费票,用户可以添加免费票和付费票。如果给定的价格为0,Cart类将无法添加产品。如果价格为0,要在购物车中添加产品,我必须扩展购物车类,但没有任何效果。谢谢你的帮助 <?php class MY_Cart extends CI_Cart { function __construct() { parent::__construct(); } fu

在我的应用程序中,我的产品中既有免费票也有付费票,用户可以添加免费票和付费票。如果给定的价格为0,Cart类将无法添加产品。如果价格为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;
    }

更改数字的格式

另外,为什么要使用preg_match来检查价格的数值,php中有很多函数可以用来检查数字,比如try

is_numeric($items['price'])

您可以在第198行对此进行注释

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

将0转换为数字格式可以解决您的问题吗?如何将0转换为数字格式?数字格式($priceVariable,2,“,”,“,”),它将值0转换为.00。它还修剪前导的0。不走运。它将值0转换为.00。它还修剪前导的0。不走运。试试回音数字_格式(0,2,',',')//0.00可能是您有问题,请逐行调试,以便将0.00转换为.00。然后,取.00并将其存储在数据库中。但在读取或更新时,它不会输入新值。出现了一些问题。price is float的db中字段的数据类型是什么?实际上我正在序列化product details数组并将其存储在列中。
$items['qty'] = trim(preg_replace('/(^[0]+)/i', '', $items['qty']));