Opencart 3.0.3.7 尝试访问 bool 类型值的数组偏移量

问题描述

我在 PHP 7.4 上运行 Opencart 3.0.3.7 并且我收到此消息错误日志:

PHP Notice: Trying to access array offset on value of type bool in /home/site/public_html/catalog/model/extension/module/so_filter_shop_by.PHP on line 313

PHP Notice: Trying to access array offset on value of type bool in /home/site/public_html/catalog/model/extension/module/so_filter_shop_by.PHP on line 314

代码是:

    foreach($query->rows as $result)
    {
        $data = $this->model_catalog_product->getProduct($result['product_id']);
        $price = $this->tax->calculate($data['price'],$data['tax_class_id'],$this->config->get('config_tax'));
        if ((float)$data['special']) {
            $price = $this->tax->calculate($data['special'],$this->config->get('config_tax'));
        }

        $price = $this->currency->format($price,$this->session->data['currency']);
        if ($this->language->get('decimal_point') == ',') {
            $price = trim(str_replace(',','.',$price));
        }
        else {
            $price = trim(str_replace(','',$price));
        }
        $price = trim(str_replace($currencies,$price));
        
        $data['price_soFilter'] = $price;

        $product_data[] = $data;
    }
    return $product_data;
}

谁能提出解决错误解决方案。

解决方法

您必须检查 $price NOT NULL 是否尝试这样的操作:

if(is_null($price))
  {
   $price = '';
  } else {
   $price = $this->tax->calculate($data['price'],$data['tax_class_id'],$this->config->get('config_tax'));
  }
,

你可以试试:

if(!empty($price)) {
   $price = $this->tax->calculate((float)$data['price'],$this->config->get('config_tax'));
} else {
   $price = '';
}