Question

I am trying to override product view controller and display a message on top by following this tutorial

The message is not shown in product page and there is no error as well. Below are the code. Please advise.

module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Company_Custom" setup_version="1.0.0" >
    <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
       </module>    
</config>

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">
<type name="Magento\Catalog\Controller\Product\View">
    <plugin name="company-custom" type="Company\Custom\Controller\Catalog\Product\View" sortOrder="10"/>
</type>
</config>

View.php

namespace Company\Custom\Controller\Catalog\Product;

class View extends \Magento\Catalog\Controller\Product\View
{
    public function execute()
    {
        echo "Local controller";
        $this->messageManager->addSuccess('Message from new controller.');      
        return parent::execute();
    }
}
Was it helpful?

Solution

In di.xml

Instead of plugin ,preference needs to be used     

<preference for="Magento\Catalog\Controller\Product\View" type="Company\Custom\Controller\Catalog\Product\View" />

OTHER TIPS

you are wrong step 3:

Step:3 Overriding xml file
File path: magento2\app\code\Jute\Customer\etc\di.xml enter image description here

<?xml version=”1.0″?>
<config xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:noNamespaceSchemaLocation=”../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd”>
<preference for=”Magento\Customer\Block\Address\Book” type=”Jute\Customer\Block\Address\Book” />
<preference for=”Magento\Customer\Controller\Address\Index” type=”Jute\Customer\Controller\Address\Index” />
</config>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top