0

How to get the current product id.

I don't want to get the product information from id (I want to get only the current id dynamically, not by assigning some value and get through object manager).

Currently I have used something like:

Event.xml

<event name="catalog_product_delete_after">
    <observer name="catalog_product_delete_after_handler" instance="Vendor\Module\Observer\SyncProductDeleteObserver"/>
</event>

Observer:

public function execute(EventObserver $observer)
{
    $productdel = $observer->getEvent()->getProduct();
    $this->productDelete($productdel);
    return $this;

}

public function productDelete($productdel)
{
    $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
    $productdel = $objectManager->get('Magento\Framework\Registry')->registry('current_product');  
    $productid=$productdel->getEntityId(); 
  .... 

ERROR:

I get getEntityid() on null during product delete.

I want to use an alternative to registry().

Please provide any other method?

2 Answers 2

1

you can inject an instace of the registry class in your class.

class myClass
{
    protected $registry;
    public function __construct(
        ....
        \Magento\Framework\Registry $registry,
        ....
    )
    {
        ....
        $this->registry = $registry;
        ...
    }
}

then you can use this inside your class.

$product = $this->registry->registry('current_product');
8
  • I tried that method...but during product delete event current product id is not getting captured in registry...I face issue like this magento.stackexchange.com/questions/132624/…
    – Sushivam
    Commented Aug 23, 2016 at 12:38
  • So i wanted to know if there is any alternate to registry method
    – Sushivam
    Commented Aug 23, 2016 at 12:38
  • then you should use an event for before delete, or a plugin for that.
    – Marius
    Commented Aug 23, 2016 at 12:40
  • I m using catalog_product_delete_after event and in my observer i say: $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $productdel = $objectManager->get('Magento\Framework\Registry')->registry('current_product'); $productid=$productdel->getEntityId();
    – Sushivam
    Commented Aug 23, 2016 at 12:42
  • Please see my updated answer
    – Sushivam
    Commented Aug 23, 2016 at 12:44
0

You could use a plugin for this. Declare plugins in your module's di.xml:

<type name="Magento\Catalog\Model\Product">
    <plugin name="acme_foo_catalog_product_plugin" type="Acme\Foo\Plugin\Catalog\ProductPlugin" sortOrder="1" disabled="false" />
</type>

Then create the plugin class at app/code/Acme/Foo/Plugin/Catalog/ProductPlugin.php:

<?php

namespace Acme\Foo\Plugin\Catalog;

class ProductPlugin {

    public function afterDelete(\Magento\Catalog\Model\Product $product, $result) {
        // do something with $product->getId();

        return $result;
    }
}

Read more about how plugins work at http://devdocs.magento.com/guides/v2.0/extension-dev-guide/plugins.html

Your Answer

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

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