2

I want to customize the catalog product view controller using a plugin the following plugin. In app/etc/di.xml i have added:

<?xml version="1.0" encoding="utf-8"?>
<type name="Magento\Catalog\Controller\Product\View">
     <plugin name="product-view-controller-plugin" type="MyNamespace\MyModule\Controller\Product\View" sortOrder="10"/>
</type>

Here is View Class:

<?php

namespace MyNamespace\MyModule\Controller\Product;
class View {
public function beforeExecute() {

echo "BEFORE<BR>"; exit;

}

public function afterExecute(\Magento\Catalog\Controller\Product\View $controller, $result) { echo "AFTER ...."; exit; }

}

when i go to http://mymagento-installation.com/catalog/product/view/id/125 nothing is happening ... can you help me to know what is missing?

  • did you flush the cache on magento admin page? – Diny Na Mar 27 '17 at 19:00
0

There are few thing,you have done wrong.

di.xml location is wrong.

di.xml should be located at app\code\MyNamespace\MyModule\etc\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="product-view-controller-plugin" type="MyNamespace\MyModule\Controller\Product\View" sortOrder="10"/>
</type>
</config>

Check Basic files of the module

As you have created a module then you should check below files should be exits at at ur module

  • app\code\MyNamespace\MyModule\composer.json
  • app\code\MyNamespace\MyModule\registration.php
  • app\code\MyNamespace\MyModule\etc\module.xml

If this two details is exit then your plugin product-view-controller-plugin will works

  • Thank you Amit, yes di.xml location was wrong – Bashir Mar 29 '17 at 17:58

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.