警告:在Magento错误日志中为foreach()提供的参数无效

时间:2014-02-11 15:24:41

标签: php xml magento error-handling magento-1.7

2014-02-11T11:12:15+00:00 ERR (3): Warning: Invalid argument supplied for foreach()  in /var/www/magento/app/code/local/Bintime/Sinchimport/Block/Layer/View.php on line 19
2014-02-11T11:12:17+00:00 ERR (3): Notice: Undefined variable: bgHandle  in /var/www/magento/app/code/community/Magehouse/Slider/Block/Catalog/Layer/Filter/Price.php on line 457
2014-02-11T11:12:17+00:00 ERR (3): Notice: Undefined variable: bgSlider  in /var/www/magento/app/code/community/Magehouse/Slider/Block/Catalog/Layer/Filter/Price.php on line 461
2014-02-11T11:12:17+00:00 ERR (3): Notice: Undefined variable: bgRange  in /var/www/magento/app/code/community/Magehouse/Slider/Block/Catalog/Layer/Filter/Price.php on line 465

我一直在清理旧的错误日志,这个问题让我陷入困境。我无法告诉哪个文件导致问题。上面的4个错误总是在让步中弹出,所以它可能是插件/主题冲突。第一行是指以下插件:

http://bit.ly/1dfpIID

其他三个指向我认为是产品页面主题附带的价格滑块。

链接到开发网站:http://bit.ly/1mAxK2a

该网站正在运行Magento 1.8.1,其主题名为来自themeforest的地铁商店。

这是View.php:

<?php

class Bintime_Sinchimport_Block_Layer_View extends Mage_Catalog_Block_Layer_View
{

    protected $filterableFeatures = array();

    /**
     * Prepare child blocks
     *
     * @return Mage_Catalog_Block_Layer_View
     */
    protected function _prepareLayout()
    {
        //получение списка фич, по которым строися навигация
        $filterableFeatures = $this->getLayer()->getFilterableFeatures();
        $filterBlockName = 'sinchimport/layer_filter_feature'; //block
        foreach ($filterableFeatures as $feature) {
            $this->filterableFeatures[] = $feature;
            $featureBlock = $this->getLayout()->createBlock($filterBlockName)
                    ->setLayer($this->getLayer())
                    ->setAttributeModel($feature)
                    ->init();
            $this->setChild('feature_' . $feature['feature_id'] . '_filter',
                            $featureBlock
                           );
        }
/* ------------ */
        return parent::_prepareLayout();
    }

    /**
     * Get all layer filters
     *
     * @return array
     */
    public function getFilters()
    {
        $filters = parent::getFilters();
/* ------------ */     
        foreach ($this->filterableFeatures as $feature) {
            $filters[] = $this->getChild('feature_' . $feature['feature_id'] . '_filter');
        }       
/* ------------ */

        return $filters;
    }
}

这是Price.php:

http://pastebin.com/BCyFxCe7

有人知道这些错误的含义或解决方法吗?

2 个答案:

答案 0 :(得分:2)

这意味着你将某些东西传递给不是数组的foreach。

您必须先验证:

if (count($array) > 0 && is_array($array)) {
   foreach ($array as $key=>$value) {
      //code here
   }
}

[编辑] 在您的代码“foreach($ filterableFeatures as $ feature)”和“foreach($ this-&gt; filterableFeatures as $ feature)”中没有任何保护。

答案 1 :(得分:1)

在第16行,此方法返回一个字符串,空字符串或字符串数​​据:

$filterableFeatures = $this->getLayer()->getFilterableFeatures();

我建议首先尝试解决该问题,看看为什么它没有返回任何内容,但是一个快速的解决方法是在尝试运行之前检查它是否是一个数组的foreach:

if (is_array($filterableFeatures)) {
    foreach ($filterableFeatures as $feature) {

当然,由于这不是问题的根源,这可能会打破其他事情,所以一定要先找到问题的根源。我猜这个&#39; Price.php&#39;由于&#39; View.php&#39;而错误输出。因此,如果您修复了View.php&#39;,您可能最终也会修复Price.php&#39;。