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
         /**
         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 :)

6
  • Try this $subject->getProduct->getId(); @KapilDevSingh Mar 19, 2019 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 Mar 19, 2019 at 4:28
  • use $result variable . Mar 19, 2019 at 4:29
  • Thankyou for reply @AnandOntigeri, I have tried this one also but this also throws same exception. Mar 19, 2019 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() Mar 19, 2019 at 5:12

1 Answer 1

2

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,
       $result)
    {
        //get the product from registry
        $product = $this->registry->registry('current_product');
        $productId = $product->getId();

        return $result;
    }
3
  • 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. Mar 19, 2019 at 5:25
  • 1
    @helgeb Registry is deprecated in magento 2.3, Should not be used github.com/magento/magento2/blob/… Mar 19, 2019 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, 2019 at 5:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.