0

I am using magento 2.3.2. My site takes too much to load. I enabled flat catalog category and Flat catalog product. See https://prnt.sc/pgr3xu. But on site when i click on any product it gives me error "Invalid argument supplied for foreach() in /var/www/html/ihouzit/app/code/Mageplaza/Search/Helper/Data.php on line 345" See https://prnt.sc/pgr3zx What can be the issue.

Getting error from this function

protected function getCategoryOptions($category, &$options, $level, $htmlPrefix = '')
{
    if ($level <= 0) {
        return $this;
    }
    $level--;

    $options[$category->getId()] = $htmlPrefix . $category->getName();

    $htmlPrefix .= '- ';
    foreach ($this->getChildCategories($category) as $childCategory) {
        $this->getCategoryOptions($childCategory, $options, $level, $htmlPrefix);
    }

    return $this;
}
  • Have re reindex all after enable flat category and product? php bin/magento indexer:reindex – Shoaib Munir Oct 9 at 5:31
  • @ShoaibMunir Done, still i get the same error. – Nafisa Oct 9 at 5:49
  • Please share function of file app/code/Mageplaza/Search/Helper/Data.php where you are getting this error. – Shoaib Munir Oct 9 at 6:13
  • @ShoaibMunir Edited my ans – Nafisa Oct 9 at 6:36
  • @Nafisa .. as you mentioned re-indexing is done. did you found any issue or error in re-indexing. And please confirm if you disable flat option still you get this issue or not. ? – nishu Oct 9 at 6:45
0

Update your function to this, this should work

protected function getCategoryOptions($category, &$options, $level, $htmlPrefix = '')
{
    if ($level <= 0) {
        return $this;
    }
    $level--;

    $options[$category->getId()] = $htmlPrefix . $category->getName();

    $htmlPrefix .= '- ';
    $childCategories = $this->getChildCategories($category);
    if(count($childCategories) > 0){
        foreach ($childCategories as $childCategory) {
            $this->getCategoryOptions($childCategory, $options, $level, $htmlPrefix);
        }
    }

    return $this;
}

Hope this will resolve your issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.