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"

My plugin class is below

namespace Ricky\Catalog\Plugin\Product; 

class Save {
     public function afterExecute(
          \Magento\Catalog\Controller\Adminhtml\Product\Save $subject,
         $productId = $subject->productId; // This is not working
         echo $result->getProduct->getId(); die("prodid");
         echo $subject->getProduct->getId(); die("prodid");
         Both throw exception: Undefined property $getProduct

         /** $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.

I am asking this after visiting This Solution

Thanks for help :)

  • Try this $subject->getProduct->getId(); @KapilDevSingh – Prathap Gunasekaran Mar 19 '19 at 4:21
  • I tried this, but its throws exception Undefined property: Magento\Catalog\Controller\Adminhtml\Product\Save\Interceptor::$getProduct well thanks for response @PrathapGunasekaran – Kapil Dev Singh Mar 19 '19 at 4:28
  • use $result variable . – Anand Ontigeri Mar 19 '19 at 4:29
  • Thankyou for reply @AnandOntigeri, I have tried this one also but this also throws same exception. – Kapil Dev Singh Mar 19 '19 at 4:36
  • @PrathapGunasekaran , This also not worked, even it ended with FATAL ERROR Fatal error: Uncaught Error: Call to undefined method Magento\Backend\Model\View\Result\Redirect\Interceptor::getAllItems() – Kapil Dev Singh Mar 19 '19 at 5:12

The variable $product is not accessible from outside the save method. The only exceptioon is the event controller_action_catalog_product_save_entity_after. But the product builder puts the product into registry, so you may access the product from there. This code should work in my opinion, but I didn't test it:

    public function __construct(
        \Magento\Framework\Registry $registry
        $this->registry = $registry;

    public function afterExecute(
      \Magento\Catalog\Controller\Adminhtml\Product\Save $subject,
        //get the product from registry
        $product = $this->registry->registry('current_product');
        $productId = $product->getId();

        return $result;
  • You didn't tested but I applied, It works like a charm. ThankYou. So basically if we want to pass variables from save (Controller) to outside then we can use registry. – Kapil Dev Singh Mar 19 '19 at 5:25
  • 1
    @helgeb Registry is deprecated in magento 2.3, Should not be used github.com/magento/magento2/blob/… – Vishwas Bhatnagar Mar 19 '19 at 5:35
  • 1
    @VishwasBhatnagar yes, I know, but it is the only way I see to access the product using a plugin. I would rather use the event but the question was about the plugin usage – HelgeB Mar 19 '19 at 5:39

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.