3

I wanted to use catalog_product_save_after event in 2 different modules, Is it possible to use it? Please help me!

3

Observer doesn't provide sort order. So, it may be create issue when create same events in multiple events.

So, I would like to suggest you to create plugin for that and avoid same events use in multiple module. Plugin provide you to set sort order if same function's plugin create in multiple action. You can use plugin in method type will be public. Otherwise, you can use preferences for that.

Hope, It will helpful for you.

2

Yes you can use it multiple times in your project.

in events.xml

<event name="catalog_product_save_after">
    <observer name="my_custom_product_save_observer" instance="Vendor\module\Observer\Productsaveafter" />
</event>  

And your observer code:

<?php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;

class Productsaveafter implements ObserverInterface
{

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $_product = $observer->getProduct();  // you will get product object
        //Your code here
    }
}

I am unable to find the order of event, like if you use it in two different module then which observer will run first is a mystery :)
But you can use it multiple times in any module you want

  • I tried but new event didn't work and old event work – Himani Aug 5 at 8:05
2

Yes you could use them in any number of Observers.

Events are dispatched in broadcasting manner, so any number of Observers can be listening to the same event. Either in same module or different module.

2

Yes, We could use multiple modules :

    <event name="catalog_product_save_after">
        <observer name="adams_mage_add_custom_option" instance="CompanyName\ModuleName\Observer\AddCustomOption" />
    </event>

for another module same event :

<event name="catalog_product_save_after">
    <observer name="inventory" instance="Magento\CatalogInventory\Observer\SaveInventoryDataObserver"/>
</event>

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.