skip to Main Content

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

<type name="MagentoCatalogControllerAdminhtmlProductSave">
    <plugin name="ricky_catalog_save_product"
            type="RickyCatalogPluginProductSave" sortOrder="10"
    />
</type>

My plugin class is below

namespace RickyCatalogPluginProduct; 

class Save {
     public function afterExecute(
          MagentoCatalogControllerAdminhtmlProductSave $subject,
           $result)
    {
         $productId = $subject->productId; // This is not working

         /** $productId is provided in excute method in Save class 
            in MagentoCatalogControllerAdminhtmlProductSave **/
    }
}

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

Answers


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

    MagentoCatalogControllerAdminhtmlProductSave.
    

    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..!!

    Login or Signup to reply.
  2. You can get the product Id in this way

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

    Hope this helps !! Happy Coding

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search