Menu Close
    Hire us Request quote Reading list Switch to dark mode

    Adding Custom Options on Product Save by Observer in Magento 2

    Hello everyone, very often we need to add some custom options while adding a product in Magento 2 admin. Today, I am going to share that how we can programmatically add custom options to a product by product_save_after observer in Magento 2.

    First of all, we have to create events.xml in the etc folder of our module structure and define our observer in that, for that write the following code in app/code/CompanyName/ModuleName/etc/events.xml

    Now, We will add the observer file i.e. AddCustomOption.php at path app/code/CompanyName/ModuleName/Observer/ with the following code,

    save the code, and try saving a product, the custom options will be created for the product as  you can see in the image below-
    I have used this code in Magento version 2.2.1 but it will work for version 2.1.x as well.

    . . .
    Subscribe Articles

    Just enter your email below to subscribe and receive weekly updates about the most interesting similar articles.

    You need to accept conditions to receive letters.
    You've Subscribed!

    Share article via

    . . .

    This is all for now. I hope it will help.

    For any queries, feel free to comment below.

    . . .


    Add Your Comment

    Be the first to comment.

    Back to Top
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again