1

I added sample data to my magento2 website, but when I want to access to a product I always get this error :

Fatal error: Uncaught Error: Call to a member function getId() on null in /var/www/html/test/vendor/magento/module-catalog/Helper/Product.php:313 Stack trace: #0 /var/www/html/test/vendor/magento/module-catalog/Helper/Product.php(430): Magento\Catalog\Helper\Product->canShow(NULL) #1 /var/www/html/test/vendor/magento/module-catalog/Helper/Product/View.php(263): Magento\Catalog\Helper\Product->initProduct(1562, Object(Magento\Catalog\Controller\Product\View\Interceptor), Object(Magento\Framework\DataObject)) #2 /var/www/html/test/vendor/magento/module-catalog/Controller/Product/View.php(118): Magento\Catalog\Helper\Product\View->prepareAndRender(Object(Magento\Framework\View\Result\Page\Interceptor), 1562, Object(Magento\Catalog\Controller\Product\View\Interceptor), Object(Magento\Framework\DataObject)) #3 /var/www/html/test/vendor/magento/framework/App/Action/Action.php(108): Magento\Catalog\Controller\Product\View->execute() #4 /var/www/html/test/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Framewor in /var/www/html/test/vendor/magento/module-catalog/Helper/Product.php on line 313

The Product.php code :

public function canShow($product, $where = 'catalog')
    {
        if (is_int($product)) {
            try {
                $product = $this->productRepository->getById($product);
            } catch (NoSuchEntityException $e) {
                return false;
            }
        } else {
       /*313*/     if (!$product->getId()) {
                return false;
            }
        }
        return $product->isVisibleInCatalog() && $product->isVisibleInSiteVisibility();
    }

when I var_dump($product) it's return NULL

Product.php class code : https://snipsave.com/saadsaad/#/snippet/iDIEafixLtWgzWO9sK

is there any solution?

7
  • It can be an indexing issue did you reindexed the site after the sample data deployment? Commented Apr 17, 2019 at 9:08
  • I lunch this command php bin/magento indexer:reindex but still not work
    – sayou
    Commented Apr 17, 2019 at 9:10
  • did you check the $product like print_r($product) to check that the data is coming in in your $product or not
    – Asad Khan
    Commented Apr 17, 2019 at 9:11
  • Id suggest redeploying your site if it still doesnt work try accessing the site via the admin and seeing if you can access the products via the admin if so save one via the admin and try and review that product Commented Apr 17, 2019 at 9:11
  • @AsadKhan I don't really understand what you mean, but I get the list of products on the home page, but when I try to a specific one I get this error
    – sayou
    Commented Apr 17, 2019 at 9:16

3 Answers 3

2

After a lot of search, I found that the problem was on di.xml

because I added this:

<preference for="Magento\Catalog\Api\ProductRepositoryInterface" 
        type="Testing\HelloWorld\Model\CustomImplementation" />

so I remove it and it's work

2
  • I am getting the same kind of error using paypal, could you pls help me stackoverflow.com/q/64463480/11697039 @sayou Thanks
    – zus
    Commented Oct 23, 2020 at 10:33
  • 2
    @zus I apologize for being late, but unfortunately, my current job is very far from Magento 2, by the way, do not forget to share the solution with the rest, if you find it.
    – sayou
    Commented Jan 6, 2021 at 15:27
0

Change your code from if (!$product->getId()) to if (!$product) it should work

complete code would be:

public function canShow($product, $where = 'catalog')
{
    if (is_int($product)) {
        try {
            $product = $this->productRepository->getById($product);
        } catch (NoSuchEntityException $e) {
            return false;
        }
    } else {
   /*313*/     
        if (!$product) {
            return false;
        }
    }
    return $product->isVisibleInCatalog() && $product->isVisibleInSiteVisibility();
}
1
  • 1
    I do it, but when I access to a product I get 404 page
    – sayou
    Commented Apr 17, 2019 at 9:16
0

Try this one:
null will be returned if the object is null.

public function canShow($product, $where = 'catalog')
    {
        if (is_int($product)) {
            try {
                $product = $this->productRepository->getById($product);
            } catch (NoSuchEntityException $e) {
                return false;
            }
        } else {
                return $product ? $product->getId() : null;
        }
        return $product->isVisibleInCatalog() && $product->isVisibleInSiteVisibility();
    }
5
  • It's get a 404 page
    – sayou
    Commented Apr 17, 2019 at 9:25
  • better if you can post your whole Product class so that we can check why it returns 404
    – fmsthird
    Commented Apr 17, 2019 at 9:30
  • @magefms I added the product.php code
    – sayou
    Commented Apr 17, 2019 at 11:08
  • @sayou I tried your code but it works. kindly double check if you have some custom class that extends product helper class
    – fmsthird
    Commented Apr 17, 2019 at 11:40
  • @magefms, yes sir I found the error on di.xml on this code <preference for="Magento\Catalog\Api\ProductRepositoryInterface" type="Testing\HelloWorld\Model\CustomImplementation" />
    – sayou
    Commented Apr 17, 2019 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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