{"id":40,"date":"2017-12-15T06:13:59","date_gmt":"2017-12-15T06:13:59","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2017\/12\/15\/create-custom-options-programmatically-in-magento-2\/"},"modified":"2025-03-17T06:27:32","modified_gmt":"2025-03-17T06:27:32","slug":"create-custom-options-programmatically-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/create-custom-options-programmatically-in-magento-2\/","title":{"rendered":"How to Create Custom Options Programmatically in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, an online food delivery store must have options to allow customers to customize their pizza toppings<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The custom options allow customers to configure the product as per the need. To&nbsp;<em><strong>create custom options programmatically in Magento 2<\/strong><\/em>, one needs to follow the below method.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Create Custom Options Programmatically in Magento 2:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$objectManager = \\Magento\\Framework\\App\\ObjectManager::getInstance(); \/\/instance of Object manager\n$productId = 50;\n$product = $objectManager->create('\\Magento\\Catalog\\Model\\Product')->load($productId);\n$values = array(\n                    array(\n                        'title'=>'Red',\n                        'price'=>10,\n                        'price_type'=>\"fixed\",\n                        'sku'=>\"Red1\",\n                        'sort_order'=>1,\n                        'is_delete'=>0,\n                        'option_type_id'=>-1,\n                    ),\n                    array(\n                        'title'=>'White',\n                        'price'=>10,\n                        'price_type'=>\"fixed\",\n                        'sku'=>\"White1\",\n                        'sort_order'=>1,\n                        'is_delete'=>0,\n                        'option_type_id'=>-1,\n                    ),\n                    array(\n                        'title'=>'Black',\n                        'price'=>10,\n                        'price_type'=>\"fixed\",\n                        'sku'=>\"black1\",\n                        'sort_order'=>1,\n                        'is_delete'=>0,\n                        'option_type_id'=>-1,\n                    )\n                );\n         \n$options = array(\n                    array(\n                        \"sort_order\"    => 1,\n                        \"title\"         => \"Field Option\",\n                        \"price_type\"    => \"fixed\",\n                        \"price\"         => \"\",\n                        \"type\"          => \"field\",\n                        \"is_require\"    => 0\n                    ),\n                    array(\n                        \"sort_order\"    => 2,\n                        \"title\"         => \"Color\",\n                        \"price_type\"    => \"fixed\",\n                        \"price\"         => \"\",\n                        \"type\"          => \"drop_down\",\n                        \"is_require\"    => 0,\n                        \"values\"        => $values\n                    ),\n                    array(\n                        \"sort_order\"    => 2,\n                        \"title\"         => \"Multiple Option\",\n                        \"price_type\"    => \"fixed\",\n                        \"price\"         => \"\",\n                        \"type\"          => \"multiple\",\n                        \"values\"        => $values,\n                        \"is_require\"    => 0\n                    )\n                );\n         \n        foreach ($options as $arrayOption) {\n            $product->setHasOptions(1);\n            $product->getResource()->save($product);\n            $option = $objectManager->create('\\Magento\\Catalog\\Model\\Product\\Option')\n                    ->setProductId($productId)\n                    ->setStoreId($product->getStoreId())\n                    ->addData($arrayOption);\n            $option->save();\n            $product->addOption($option);\n        }<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the code above,<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">$values is an array to hold the values for the select type custom option, and,<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">$options is an array to hold multiple custom options<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When you execute the above code, the custom options of types: field, drop down and multi-select will be created in your product.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Comment us to help you out if you face any issue while creating custom options programmatically!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":14,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-40","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":2,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":9384,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/40\/revisions\/9384"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}