M
M
mihanickq2019-06-05 17:10:04
PHP
mihanickq, 2019-06-05 17:10:04

How to solve error with Warning: count(): Parameter must be an array or an object that implements Countable in ....?

Magento logs the following errors:
ERR (3): Warning: count(): Parameter must be an array or an object that implements Countable in .../app/code/local/Mage/Catalog/Block/Product/List. php on line 299
Code like this:

/**
     * Retrieve block cache tags based on product collection
     *
     * @return array
     */
    public function getCacheTags()
    {
        $data = array(self::CACHE_TAG);
        if ($category = Mage::registry('current_category')) {
            $data[] = Mage_Catalog_Model_Category::CACHE_TAG . "_" . $category->getId();
        }
!!!Это строка 299 --->       if (count($products = $this->getProductList())) {
            foreach ($products as $p) {
                $data[] = Mage_Catalog_Model_Product::CACHE_TAG . "_" . $p->getId();
            }
        }
        return $data;
    }

    public function getCacheLifetime()
    {
        return ($this->getData('cache_lifetime'))?intval($this->getData('cache_lifetime')):3600;
    }
  
}

I hope there is a simple way to fix this, tell me.
There is no good specialist on the hook, but I don’t trust unfamiliar people, there was a sad experience, more than once. Here, after all, there is a community of intellectuals and a collective mind ...
But in general, yes, it’s still a problem to find a reliable and sensible admin / proger for periodic remote work. Some ambitions and requests do not meet the level, others are irresponsible slobs. Unfortunately, I understood this from my own experience and excessive trust only aggravated the situation. I know that there are smart guys, but without the appropriate knowledge and experience it is difficult to immediately assess professional suitability.
And this is my average online store, plans and potential for growth like Napoleon’s, but unfortunately everything is stalling precisely in the human factor.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaliy Orlov, 2019-06-05
@mihanickq

This error is returned by count when null is passed to it. Prior to php 7.2 it will return 0, but from php 7.2 there will be the described error.
You can fix it like this

$products = $this->getProductList();
if ($products) {

it's better like this
$products = $this->getProductList();
if (is_iterable($products)) {

and even better like this
$products = $this->getProductList();
if (is_countable($products)) {

but this option requires either php 7.3 or a polyfill which can be found in the comments to the function description https://www.php.net/manual/en/function.is-countable.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question