0

Currently I'm experiencing issues with a third party ajax add to cart popup module. The issue is that, on very rare occasion the get product method will return null, and this causes the following error in the getProductCollection method, as the product collection will return null when the product is null:

report.ERROR: Warning: count(): Parameter must be an array or an object that implements Countable

If I protect against null values by passing an empty array (countable) when the product is null then I see the following error in the getImage method:

Uncaught TypeError: Argument 1 passed to Magento\\Catalog\\Block\\Product\\ImageBuilder::setProduct() must be an instance of Magento\\Catalog\\Model\\Product, null given

It seems to be that the issue is that on rare occasion, the product will return null. The problem is that I'm unable to reproduce this issue. The related error might log once a day, or sometimes once a week. The only way I can reproduce is by explicitly setting the get product method to return null, or by deleting a product and then adding it to cart before refreshing the page. Otherwise, when testing the add to cart popup seems to work perfectly.

One option might be to only load the popup when the product is not null:

if ($this->getProduct() != null)

This prevents any unexpected errors when the product is null, but I'm not quite understanding why the get product method would return null in the first place. Is this normal, and the issue here is the lack of protection against null values?

The mentioned methods are as follows:

getProduct method:

public function getProduct()
{
    if(!$this->_product && $this->getProductId()) {
        $this->_product = $this->_initProduct($this->getProductId());
    }
    return $this->_product;
}

getProductCollection method:

public function getProductCollection() {        
    if (!$this->_productCollection && $this->getProduct()) {
        $this->_productCollection = $this->_crosssellBlock->getItemCollection();
    }
    return $this->_productCollection;
}

getImage method:

public function getImage($product, $imageId, $attributes = []) {
        return $this->imageBuilder->setProduct($product)
            ->setImageId($imageId)
            ->setAttributes($attributes)
            ->create();
}

Any help would be appreciated.

Cheers, Max

0

A few question from my end and my observations.

  1. How does the call to the method getImage() take place? I want to look at the entire flow. I need to understand how the $product is passed to this.
  2. What is the implementation of the method getProductId()?
  3. What is the name of the third party module that you are working on?
  4. On which page are you getting this error? PDP or PLP?
  5. I believe that, when a product is added to the cart, you have to show a popup and provide some options there like Edit Cart, Proceed to checkout, Related products and etc. So not showing the popup when the product is null is not the logical approach.

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.