🔥 Just Launched! Werra Premium Template for HyväSee it in Action

How to Create Custom Options Programmatically in Magento 2

By Chandresh ChauhanUpdated on Mar 17, 2025 2 min read

Magento 2 is a popular CMS used widely used in E-commerce stores selling varied products. For some of the products, customers require to specify custom options while placing the order.

For example, an online food delivery store must have options to allow customers to customize their pizza toppings

The custom options allow customers to configure the product as per the need. To create custom options programmatically in Magento 2, one needs to follow the below method.

Method to Create Custom Options Programmatically in Magento 2:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); //instance of Object manager
$productId = 50;
$product = $objectManager->create('\Magento\Catalog\Model\Product')->load($productId);
$values = array(
                    array(
                        'title'=>'Red',
                        'price'=>10,
                        'price_type'=>"fixed",
                        'sku'=>"Red1",
                        'sort_order'=>1,
                        'is_delete'=>0,
                        'option_type_id'=>-1,
                    ),
                    array(
                        'title'=>'White',
                        'price'=>10,
                        'price_type'=>"fixed",
                        'sku'=>"White1",
                        'sort_order'=>1,
                        'is_delete'=>0,
                        'option_type_id'=>-1,
                    ),
                    array(
                        'title'=>'Black',
                        'price'=>10,
                        'price_type'=>"fixed",
                        'sku'=>"black1",
                        'sort_order'=>1,
                        'is_delete'=>0,
                        'option_type_id'=>-1,
                    )
                );
         
$options = array(
                    array(
                        "sort_order"    => 1,
                        "title"         => "Field Option",
                        "price_type"    => "fixed",
                        "price"         => "",
                        "type"          => "field",
                        "is_require"    => 0
                    ),
                    array(
                        "sort_order"    => 2,
                        "title"         => "Color",
                        "price_type"    => "fixed",
                        "price"         => "",
                        "type"          => "drop_down",
                        "is_require"    => 0,
                        "values"        => $values
                    ),
                    array(
                        "sort_order"    => 2,
                        "title"         => "Multiple Option",
                        "price_type"    => "fixed",
                        "price"         => "",
                        "type"          => "multiple",
                        "values"        => $values,
                        "is_require"    => 0
                    )
                );
         
        foreach ($options as $arrayOption) {
            $product->setHasOptions(1);
            $product->getResource()->save($product);
            $option = $objectManager->create('\Magento\Catalog\Model\Product\Option')
                    ->setProductId($productId)
                    ->setStoreId($product->getStoreId())
                    ->addData($arrayOption);
            $option->save();
            $product->addOption($option);
        }

In the code above,

$values is an array to hold the values for the select type custom option, and,

$options is an array to hold multiple custom options

When you execute the above code, the custom options of types: field, drop down and multi-select will be created in your product.

Comment us to help you out if you face any issue while creating custom options programmatically!

Chandresh Chauhan Full Image
Article byChandresh Chauhan

He has been with Meetanshi for more than three years now as a certified Magento developer. A silent guy whom you can always find solving clients' issues, is an avid reader too.