1

I have created interceptor for catalog product controller's save action

<type name="Magento\Catalog\Controller\Adminhtml\Product\Save">
    <plugin name="ricky_catalog_save_product"
            type="Ricky\Catalog\Plugin\Product\Save" sortOrder="10"
    />
</type>

My plugin class is below

namespace Ricky\Catalog\Plugin\Product; 

class Save {
     public function afterExecute(
          \Magento\Catalog\Controller\Adminhtml\Product\Save $subject,
           $result)
    {
         $productId = $subject->productId; // This is not working

         /** $productId is provided in excute method in Save class 
            in Magento\Catalog\Controller\Adminhtml\Product\Save **/
    }
}

For some reasons I have to use Plugin (Interceptor Design Pattern), I know I can get newly created prouduct id by using observer for catalog_product_save_after event. But please provide solution for plugins.

Thanks for help :)

2 Answers2

0

If you are accessing the property $subject->productId, that means it should be defined in the class

\Magento\Catalog\Controller\Adminhtml\Product\Save.

There is no class variable defined with name productId.

You can override the controller and define one more public class variable

public $productId;

and assign product id somewhere in the execute() method:-

$this->productId = $productId;

Now in your plugin use it as:-

$subject->productId

Tested and working..!!

Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
0

You can get the product Id in this way

$productId = $subject->getRequest()->getParam('id');

Hope this helps !! Happy Coding

Vijay R
  • 160
  • 4