I'm not going in detail about what i'm exactly saving because this question is regards all the possible thing you can save with the ->save() function in each repository.

Anyways , when importing data from an API i use the save() method to store the data that i retrieved. Now it seems that that save() will trigger my plugin AfterSave().

Which makes sense right?

But now is the question. The AfterSave() is ment for when a customer or an admin saves an order/product/customer/... and not for when this is triggered over an autosync (cron job).

Is there somewhere where i can put a filter to determine when to use aftersave and when not?


This is where the "area" plays a vital role. You can achieve this by carefully adding your plugin in an appropriate area.

Magento 2 has different areas. For example: frontend, admin, web_api etc. If you want to use your plugin only in frontend, then place your plugin in frontend area and thus you can limit the functionality.

ie placing your di.xml

app\code\Namespace\Module\etc\di.xml -- applies everywhere app\code\Namespace\Module\etc\frontend\di.xml -- applies only for frotend app\code\Namespace\Module\etc\adminhtml\di.xml -- applies only for backend side app\code\Namespace\Module\etc\web_api\di.xml -- applies only for apis

will have different effects. So always keep "area" scope while you are codding. This is a powerful feature to avoid unnecessary config loadings and hence vital for performance.

| improve this answer | |
  • So if i'm right i want to add that to the admin area and the frontend area. And what happends over ssh won't trigger the plugin? – CompactCode Sep 25 '17 at 9:16
  • And by areas you mean put my di.xml in the etc/frontend/ || adminhtml/ folder instead of the etc/ folder. – CompactCode Sep 25 '17 at 9:18
  • Yes, exactly. For custom commands I believe we can set an area as per our need. I am not sure whether there is an "area" for native commands. – Rajeev K Tomy Sep 25 '17 at 9:23
  • I tried this and it does indeed work great! Thanks , you just reduced my api load by 80% i believe. Big vote up! – CompactCode Sep 25 '17 at 9:24
  • Please see my edit too. – Rajeev K Tomy Sep 25 '17 at 9:28

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.