Hi
I'm trying to override product list page but changes are not getting reflected in front end as it still referring to the core block.
Following is the code,
di.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Catalog\Block\Product\ListProduct" type="TestApp\Blog\Block\Rewrite\Product\ListProduct" /> </config>
ListProduct.php
<?php namespace TestApp\Blog\Block\Rewrite\Product; use Magento\Framework\View\Element\Template; class ListProduct extends \Magento\Catalog\Block\Product\ListProduct{ protected function _getProductCollection() { return null; } }
Solved! Go to Solution.
Hello,
have you added into the etc/frontend/di.xml file?
make sure you flush the cache, di compile if you already did
if works then mark as solution
Hello,
your block related to frontend action so need to add into a frotend folder.
make sure you added sequence as well if you want to run your module at the last.
Hope it will help you
Hello,
have you added into the etc/frontend/di.xml file?
make sure you flush the cache, di compile if you already did
if works then mark as solution
Thank you for the quick reply.
I have 2 modules
One of then worked when i placed di.xml in etc folder but this one didn't.
now i placed di.xml of this module in etc/frontend folder as you said and it worked.
can u explain me why ?
Hello,
your block related to frontend action so need to add into a frotend folder.
make sure you added sequence as well if you want to run your module at the last.
Hope it will help you
I got it.
Thanks again.
@Sunil Patel I try to override the block class Magento\Catalog\Block\Product\View\GalleryOptions
I put my di.xml in "app/code/<<VENDOR>>/<<MODULE>>/etc/frontend/di.xml"
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Catalog\Block\Product\View\GalleryOptions" type="<<VENDOR>>\<<MODULE>>\Block\Product\View\GalleryOptions" /> </config>
And the class in "app/code/<<VENDOR>>/<<MODULE>>/Block/Product/View/GalleryOptions.php"
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ namespace <<VENDOR>>\<<MODULE>>\Block\Product\View; use Magento\Framework\Serialize\Serializer\Json; use Magento\Catalog\Block\Product\Context; use Magento\Framework\Stdlib\ArrayUtils; use Magento\Catalog\Block\Product\View\Gallery; /** * Gallery options block. */ class GalleryOptions extends \Magento\Catalog\Block\Product\View\GalleryOptions { public function __construct( Context $context, ArrayUtils $arrayUtils, Json $jsonSerializer, Gallery $gallery, array $data = [] ) { $this->gallery = $gallery; $this->jsonSerializer = $jsonSerializer; parent::__construct($context, $arrayUtils, $jsonSerializer, $gallery, $data); } /** * Retrieve gallery options in JSON format * * @return string * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ElseExpression) */ public function getOptionsJson() { exit("getOptionsJson override called"); ... } /** * Retrieve gallery FS (fullscreen) options in JSON format * * @return string * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ElseExpression) */ public function getFSOptionsJson() { exit("getOptionsJson override called"); ... } }
Then I executed sudo -u projectuser php bin/magento setup:di:compile and php bin/magento cache:flush
There were no errors.
But nothing happens. If I put an "exit('original')" in the original methods then it exits with "original" so my override is not working.
Solved it: I had to run `php bin/magento setup:upgrade`
Why is there no documentation entry about overriding classes??! The documentation sucks.
IMPORTANT:
If there are errors, then check the access levels of the properties of the extended class!
Sometimes the magento developers declared some properties as private instead of protected and make it hard for us.