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?

  • It can be an indexing issue did you reindexed the site after the sample data deployment? – ABHISHEK TRIPATHI Apr 17 at 9:08
  • I lunch this command php bin/magento indexer:reindex but still not work – sayou Apr 17 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 Apr 17 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 – Dava Gordon Apr 17 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 Apr 17 at 9:16
1

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

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
    I do it, but when I access to a product I get 404 page – sayou Apr 17 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();
    }
  • It's get a 404 page – sayou Apr 17 at 9:25
  • better if you can post your whole Product class so that we can check why it returns 404 – fmsthird Apr 17 at 9:30
  • @magefms, can you help me on this please, magento.stackexchange.com/questions/270442/… – jafar pinjar Apr 17 at 10:41
  • @magefms I added the product.php code – sayou Apr 17 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 Apr 17 at 11:40

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.