1

我在一个电子商务网站上工作,当用户第一次将产品添加到购物篮时,我收到以下错误,

遇到 PHP 错误

严重性:通知

消息:未定义的索引:orderDetails

文件名:库/MY_Cart.php

行号:59 遇到 PHP 错误

严重性:警告

消息:无法修改标头信息 - 标头已发送(输出开始于 /var/www/vhosts/akulaliving.com/httpdocs/CI-1.7.3/libraries/Exceptions.php:166)

文件名:库/Session.php

行号:662

使用以下代码将产品添加到购物篮中,

if ($this->input->post('btnAddToBag'))
        {

            $derivativeId = $this->input->post('selDerivative-1');
            $quantity = $this->input->post('selQuantity');
            $derivative = $this->Product_model->GetProducts(array('pdId' => $derivativeId), 'small');

            // Add item to shopping bag.
            $attributes = $this->Product_model->GetProductDerivatives(array('pdId' => $derivativeId));
            $this->Checkout_model->AddProduct($derivative, $attributes, $quantity);
            $this->data['message'] = 'Item added to Shopping Bag.';

            // Update Delivery Price
            $this->Checkout_model->updateDelivery(49);

            //get the bag details
            $this->data['items'] = $this->Checkout_model->GetProducts();        
        }

被调用的模型函数是这样的,

function AddProduct($derivative, $attributes, $quantity)
{
    $data = array(
       'id'         => $derivative->pdId,
       'qty'        => $quantity,
       'price'      => ($derivative->productSavingType == 'none' ? $derivative->productPrice : $derivative->productSavingPrice),
       'name'       => $derivative->productTitle,
       'attributes' => $attributes['attributeValues'],
       'refNo'      => $derivative->pdRefNo,
       'productId'  => $derivative->productId,
       'set'        => $derivative->productIsSet,
       'hasImage'   => $derivative->hasImage,
       'imageUrl'   => $derivative->imageUrl,
       'imageAlt'   => $derivative->imageAlt,
       'stockLevel' => $derivative->pdStockLevel,
       'leadTime'   => $derivative->pdLeadTime
    );

    $data['nonDiscountedPrice'] = $data['price'];
    if ($derivative->productSavingType == 'end-of-season')
    {
        $data['nonDiscountedPrice'] = $derivative->productPrice;
    }

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

错误抱怨的代码如下,

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

    if (isset($this->_cart_contents[$rowid]))
    {
        if (!isset($items['qty']))
        {
            return FALSE;
        }

        // Already Exists, we need to update the total for this item
        $new_qty = $items['qty'];
        $items['qty'] = $items['qty'] + $this->_cart_contents[$rowid]['qty'];

        $items['rowid'] = $rowid;

        if ($this->_update($items))
        {
            return TRUE;
        }
        return FALSE;
    }

    // Doesn't exist, we need to insert this item.
    if (parent::_insert($items))
    {
        // Update our total.
        if (isset($this->_cart_contents[$rowid]))
        {
            $this->real_total_items += $this->_cart_contents[$rowid]['qty'];
            if ($this->_cart_contents['orderDetails']['discount'] > 0)
            {
                $this->_cart_contents[$rowid]['price'] = $this->_cart_contents[$rowid]['nonDiscountedPrice'];
                $this->_save_cart();
            }
        }
        return TRUE;
    }
    return FALSE;
}
4

5 回答 5

1

乔姆拉?这是$this->_cart_contents['orderDetails']在使用前未定义的通知。您可以事先定义它,或者关闭通知,它应该会消失。

于 2011-04-28T12:55:48.890 回答
1

我敢打赌,您的 PHP 已经输出了 NOTICE 并且它导致会话标头已经发送错误。

在您的 php.ini 中找到该error_reporting行并将其更改为

error_reporting = E_ALL & ~E_NOTICE

重新启动您的 apache 实例,看看是否可以解决问题。

于 2011-04-28T12:56:34.010 回答
0
if (**$this->_cart_contents['orderDetails']['discount'] > 0**)
        {
            $this->_cart_contents[$rowid]['price'] = $this->_cart_contents[$rowid]['nonDiscountedPrice'];
            $this->_save_cart();
        }

CI 抛出通知消息,因为 ::_cart_contents['orderDetails'] 变量不存在。

如果您不想更改 php.ini 的错误设置,您可以如下更改并尝试:

if (isset($this->_cart_contents['orderDetails']['discount'])&& ($this->_cart_contents['orderDetails']['discount']>0))
于 2011-04-28T13:10:22.293 回答
0

您可能将服务器配置设置为显示 E_NOTICE 的 PHP 错误(这对于开发来说是正常的)。因为生成了 E_NOTICE (MY_Cart.php),所以首先将其回显到浏览器。并且因为已经有浏览器输出,这会导致您的警告“无法修改标头信息”(libraries/Session.php)。可能是因为它正在执行 header() 函数或类似的东西。

您可以通过修复 E_NOTICE 的原因(忘记对 'orderDetails' 键进行 isset 检查?)或通过 在代码开头设置error_reporting(E_ALL & ~E_NOTICE)来隐藏 E_NOTICE 来解决此问题。

于 2011-04-28T13:03:40.820 回答
0

你必须在使用它之前为你的变量定义一个默认值

解决这个警告。

于 2011-04-28T13:08:10.747 回答