4

I wanted to use catalog_product_save_after event in 2 different modules, Is it possible to use it?

Please help me!

4 Answers 4

6

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
  • 1
    Good solution. +1 :) Aug 5, 2019 at 9:09
  • Thanks @ShoaibMunir :) Aug 5, 2019 at 9:14
4

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.

3

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

1
  • I tried but new event didn't work and old event work
    – temper
    Aug 5, 2019 at 8:05

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.