1

On Magento 2.3.2 I switched from php 7.1 to php 7.2. When I did I started getting this error- Exception #0 (Exception): Warning: count(): Parameter must be an array or an object that implements Countable in /chroot/home/html/app/code/WeltPixel/GoogleTagManager/view/frontend/templates/cart.phtml on line 10

Here is the code-

<?php
// @codingStandardsIgnoreFile
?>
<?php if($this->isEnabled()) : ?>
<?php $helper = $this->helper('WeltPixel\GoogleTagManager\Helper\Data'); ?>
    <?php
    $crosselProductsCollection = $this->getCrosselProductCollection();
    $productImpressions = [];

    if (count($crosselProductsCollection)) :
        $i = 1;
        foreach($crosselProductsCollection as $_product) :
            $crosselProduct = array();
            $crosselProduct['name'] = html_entity_decode($_product->getName());
            $crosselProduct['id'] = $helper->getGtmProductId($_product);
            $crosselProduct['price'] = number_format($_product->getPriceInfo()->getPrice('final_price')->getValue(), 2, '.', '');
            if ($helper->isBrandEnabled()) :
                $crosselProduct['brand'] = $helper->getGtmBrand($_product);
            endif;
            $crosselProduct['category'] = $helper->getGtmCategoryFromCategoryIds($_product->getCategoryIds());
            $crosselProduct['list'] = __('Crossel Products');
            $crosselProduct['position'] = $i;
            $productImpressions[] = $crosselProduct;
            $i++ ;
        endforeach;
    endif;

    if (count($productImpressions)) :
        $this->setEcommerceData('currencyCode', $this->getCurrencyCode());
        $this->setEcommerceData('impressions', $productImpressions);
        $this->setDataLayerOption('event', 'impression');
        $this->setDataLayerOption('eventCategory', 'Ecommerce');
        $this->setDataLayerOption('eventAction', 'Impression');
        $this->setDataLayerOption('eventLabel',  \WeltPixel\GoogleTagManager\Model\Api\Remarketing::ECOMM_PAGETYPE_CART);
    endif;

    if ($helper->isAdWordsRemarketingEnabled()):
        $remarketingData = [];
        $remarketingData['ecomm_pagetype'] = \WeltPixel\GoogleTagManager\Model\Api\Remarketing::ECOMM_PAGETYPE_CART;
        $remarketingData['ecomm_prodid'] = $this->getProductIds();
        $remarketingData['ecomm_totalvalue'] = number_format($this->getCartTotal(), 2, '.', '');
        $this->setDataLayerOption('google_tag_params', $remarketingData);
    endif;

    ?>

How can I clear that error?

  • 1
    $crosselProductsCollection = $this->getCrosselProductCollection() ?? []; – danronmoon Oct 7 '19 at 18:51
  • Just went in and checked and that cleared it. You can put it as an answer and I will mark it as solved. Thank you for the help. – tjjen Oct 7 '19 at 19:17
2

Not sure how WeltPixel\GoogleTagManager\Helper\Data::getCrosselProductCollection looks exactly as it is a paid extension, but you can add a null coalesce operator to guard the return value to a countable interface implementor such as an array:

$crosselProductsCollection = $this->getCrosselProductCollection() ?? [];

|improve this answer|||||

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.