How to Create New Observer On Trigger of Magento 2 Event “catalog_product_save_before”

How to Create New Observer On Trigger of Magento 2 Event "catalog_product_save_before"

Magento 2 events and observers enable to extend the default functionality. The developers can use them to run custom code in response to a particular Magento 2 event.

I have used one of the Magento 2 events, “catalog_product_save_before” and shown the method below for the same.

Some of the examples where you can follow this solution are when you want to get notified as an admin on product quantity update, custom attribute update, etc.

Earlier, I had used this event in the method to auto change “Stock Availability” on quantity update in Magento 2

Steps to Create New Observer On Trigger of Magento 2 Event “catalog_product_save_before”:

  1. Create events.xml at app\code\[Vendor]\[Module]\etc\adminhtml\events.xml

  2.  Create ProductSaveBefore.php at app\code\[Vendor]\[Module]\Observer\ProductSaveBefore.php

That’s it.

I’d like to know how you used this solution in your store in the Comments section below. Also, if you have any doubts regarding the implementation, please mention them, and I’d be glad to help.

Do share the solution on social media to help out fellow developers.

Thanks.

5
(based on 4 Reviews)

Sanjay is a co-founder at Meetanshi. He is a certified Magento developer who loves creating Magento E-commerce solutions. When he is not engrossed with anything related to Magento, he loves to play cricket.

Leave a Reply