Reading list Switch to dark mode

    Adding Custom Options on Product Save by Observer in Magento 2

    Updated 16 July 2021

    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

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
        <event name="catalog_product_save_after">
            <observer name="webkul_testmodule_add_custom_option" instance="CompanyName\ModuleName\Observer\AddCustomOption" />
        </event>
    </config>
    

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

    <?php
    
    namespace CompanyName\ModuleName\Observer;
    
    use Magento\Framework\Event\ObserverInterface;
    
    class AddCustomOption implements ObserverInterface
    {
        protected $_options;
    
        public function __construct(
            \Magento\Catalog\Model\Product\Option $options
        ) {
            $this->_options = $options;
        }
    
        public function execute(\Magento\Framework\Event\Observer $observer)
        {
            $product = $observer->getProduct();
            $options = [];
            $options = [
                '0' => [
                        'sort_order' => '1',
                        'title' => 'option title',
                        'price_type' => 'fixed',
                        'price' => '5',
                        'type' => 'drop_down',
                        'is_require' => '0',
                        'values' => [
                            '0' =>[
                                    'title' => 'A',
                                    'price' => '50',
                                    'price_type' => 'fixed',
                                    'sku' => 'A',
                                    'sort_order' => '0',
                                    'is_delete' => '0',
                            ],
        
                            '1' => [
                                    'title' => 'B',
                                    'price' => '100',
                                    'price_type' => 'fixed',
                                    'sku' => 'B',
                                    'sort_order' => '1',
                                    'is_delete' => '0',
                            ],
        
                            '2' => [
                                    'title' => 'C',
                                    'price' => '150',
                                    'price_type' => 'fixed',
                                    'sku' => 'C',
                                    'sort_order' => '2',
                                    'is_delete' => '0',
                            ]
                        ]
                    ]
                ];
            foreach ($options as $arrayOption) {
                $option = $this->_options
                                ->setProductId($product->getId())
                                ->setStoreId($product->getStoreId())
                                ->addData($arrayOption);
                $option->save();
                $product->addOption($option);
            }        
        }
    }
    

    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.

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

    Searching for an experienced
    Magento 2 Company ?
    Find out More

    For any queries, feel free to comment below.

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home