{"id":57360,"date":"2016-08-17T15:31:59","date_gmt":"2016-08-17T15:31:59","guid":{"rendered":"http:\/\/webkul.com\/blog\/?p=57360"},"modified":"2024-02-23T14:09:48","modified_gmt":"2024-02-23T14:09:48","slug":"add-custom-options-product-magento-2","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/","title":{"rendered":"How to add custom options of product in Magento 2"},"content":{"rendered":"\n<p>Here we learn, how to add custom options of product in Magento 2.<\/p>\n\n\n\n<p><strong>Note:<\/strong> We also have a separate module (<strong>Magento 2 Dependent Custom Options<\/strong>) that gives the ability to admin to create dependent custom options for the products. These options will be presented on the product page and will be dependent on one another. For more information, you can check it on our\u00a0<a href=\"https:\/\/store.webkul.com\/magento2-dependent-custom-options.html\">store<\/a>.<\/p>\n\n\n\n<p>I have created <strong>Drop-down Type<\/strong> custom option here.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">$objectManager = \\Magento\\Framework\\App\\ObjectManager::getInstance();\n$productId = 12490;\n$product = $objectManager-&gt;create(&#039;\\Magento\\Catalog\\Model\\Product&#039;)-&gt;load($productId);\n$productOptions = &#091;\n    &#091;\n        &quot;is_delete&quot; =&gt; &#039;&#039;,\n        &quot;sort_order&quot; =&gt; 1,\n        &quot;title&quot; =&gt; &quot;Size&quot;,\n        &quot;type&quot; =&gt; &quot;drop_down&quot;,\n        &quot;is_require&quot; =&gt; 0,\n        &quot;values&quot; =&gt; &#091;\n            &#091;\n                &quot;sort_order&quot; =&gt; 0,\n                &quot;is_delete&quot; =&gt; &#039;&#039;,\n                &quot;title&quot; =&gt; &#039;M&#039;,\n                &quot;price&quot; =&gt; 5,\n                &quot;price_type&quot; =&gt; &#039;fixed&#039;,\n                &quot;sku&quot; =&gt; &#039;&#039;\n            ],\n            &#091;\n                &quot;sort_order&quot; =&gt; 1,\n                &quot;is_delete&quot; =&gt; &#039;&#039;,\n                &quot;title&quot; =&gt; &#039;L&#039;,\n                &quot;price&quot; =&gt; 5,\n                &quot;price_type&quot; =&gt; &#039;fixed&#039;,\n                &quot;sku&quot; =&gt; &#039;&#039;\n            ]\n        ]\n    ]\n];\n\/**\n * Initialize product options\n *\/\nif ($productOptions &amp;&amp; !$product-&gt;getOptionsReadonly()) {\n\/\/ mark custom options that should to fall back to default value\n    $customOptions = &#091;];\n    foreach ($productOptions as $customOptionData) {\n        if (empty($customOptionData&#091;&#039;is_delete&#039;])) {\n            if (isset($customOptionData&#091;&#039;values&#039;])) {\n                $customOptionData&#091;&#039;values&#039;] = array_filter($customOptionData&#091;&#039;values&#039;], function ($valueData) {\n                    return empty($valueData&#091;&#039;is_delete&#039;]);\n                });\n            }\n            $customOptionFactory = \\Magento\\Framework\\App\\ObjectManager::getInstance()-&gt;get(\n                &#039;Magento\\Catalog\\Api\\Data\\ProductCustomOptionInterfaceFactory&#039;\n            );\n            $customOption = $customOptionFactory-&gt;create(&#091;&#039;data&#039; =&gt; $customOptionData]);\n            $customOption-&gt;setProductSku($product-&gt;getSku());\n            $customOption-&gt;setOptionId(null);\n            $customOptions&#091;] = $customOption;\n        }\n    }\n    $product-&gt;setOptions($customOptions);\n}\n$product-&gt;save();<\/pre>\n\n\n\n<p>That&#8217;s all.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here we learn, how to add custom options of product in Magento 2. Note: We also have a separate module (Magento 2 Dependent Custom Options) that gives the ability to admin to create dependent custom options for the products. These options will be presented on the product page and will be dependent on one another. <a href=\"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":69,"featured_media":54828,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[302,3286,1],"tags":[3522,861,3521],"class_list":["post-57360","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-magento2","category-magento2-1","category-uncategorized","tag-create-drop-down-type-custom-options","tag-custom-options","tag-magento2-custom-options"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>how to add custom options of product in Magento 2<\/title>\n<meta name=\"description\" content=\"Here we learn, how to add custom options of product in Magento 2. you can add dropdown custom options in Magento 2.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"how to add custom options of product in Magento 2\" \/>\n<meta property=\"og:description\" content=\"Here we learn, how to add custom options of product in Magento 2. you can add dropdown custom options in Magento 2.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2016-08-17T15:31:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-23T14:09:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/07\/Magneto-Code-Snippet.png\" \/>\n\t<meta property=\"og:image:width\" content=\"825\" \/>\n\t<meta property=\"og:image:height\" content=\"260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Mahesh Singh\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mahesh Singh\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/\"},\"author\":{\"name\":\"Mahesh Singh\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/53d3b977a0ab5adcf32aef9f97e595bd\"},\"headline\":\"How to add custom options of product in Magento 2\",\"datePublished\":\"2016-08-17T15:31:59+00:00\",\"dateModified\":\"2024-02-23T14:09:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/\"},\"wordCount\":82,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/07\/Magneto-Code-Snippet.png\",\"keywords\":[\"Create Drop-down type custom options\",\"Custom Options\",\"Magento2 custom options\"],\"articleSection\":[\"Magento2\",\"Magento2.1\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/\",\"url\":\"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/\",\"name\":\"how to add custom options of product in Magento 2\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/07\/Magneto-Code-Snippet.png\",\"datePublished\":\"2016-08-17T15:31:59+00:00\",\"dateModified\":\"2024-02-23T14:09:48+00:00\",\"description\":\"Here we learn, how to add custom options of product in Magento 2. you can add dropdown custom options in Magento 2.\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/#primaryimage\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/07\/Magneto-Code-Snippet.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/07\/Magneto-Code-Snippet.png\",\"width\":825,\"height\":260},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to add custom options of product in Magento 2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/53d3b977a0ab5adcf32aef9f97e595bd\",\"name\":\"Mahesh Singh\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/f4c013ebf7008223382b8a49203e6d354677e8baff0eca373e6e4266efa762da?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/f4c013ebf7008223382b8a49203e6d354677e8baff0eca373e6e4266efa762da?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Mahesh Singh\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/mahesh721\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"how to add custom options of product in Magento 2","description":"Here we learn, how to add custom options of product in Magento 2. you can add dropdown custom options in Magento 2.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/","og_locale":"en_US","og_type":"article","og_title":"how to add custom options of product in Magento 2","og_description":"Here we learn, how to add custom options of product in Magento 2. you can add dropdown custom options in Magento 2.","og_url":"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2016-08-17T15:31:59+00:00","article_modified_time":"2024-02-23T14:09:48+00:00","og_image":[{"width":825,"height":260,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/07\/Magneto-Code-Snippet.png","type":"image\/png"}],"author":"Mahesh Singh","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Mahesh Singh","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/"},"author":{"name":"Mahesh Singh","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/53d3b977a0ab5adcf32aef9f97e595bd"},"headline":"How to add custom options of product in Magento 2","datePublished":"2016-08-17T15:31:59+00:00","dateModified":"2024-02-23T14:09:48+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/"},"wordCount":82,"commentCount":2,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"image":{"@id":"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/#primaryimage"},"thumbnailUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/07\/Magneto-Code-Snippet.png","keywords":["Create Drop-down type custom options","Custom Options","Magento2 custom options"],"articleSection":["Magento2","Magento2.1"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/","url":"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/","name":"how to add custom options of product in Magento 2","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/#primaryimage"},"image":{"@id":"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/#primaryimage"},"thumbnailUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/07\/Magneto-Code-Snippet.png","datePublished":"2016-08-17T15:31:59+00:00","dateModified":"2024-02-23T14:09:48+00:00","description":"Here we learn, how to add custom options of product in Magento 2. you can add dropdown custom options in Magento 2.","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/#primaryimage","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/07\/Magneto-Code-Snippet.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/07\/Magneto-Code-Snippet.png","width":825,"height":260},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/add-custom-options-product-magento-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to add custom options of product in Magento 2"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/53d3b977a0ab5adcf32aef9f97e595bd","name":"Mahesh Singh","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/f4c013ebf7008223382b8a49203e6d354677e8baff0eca373e6e4266efa762da?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f4c013ebf7008223382b8a49203e6d354677e8baff0eca373e6e4266efa762da?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Mahesh Singh"},"url":"https:\/\/webkul.com\/blog\/author\/mahesh721\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/57360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/69"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=57360"}],"version-history":[{"count":7,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/57360\/revisions"}],"predecessor-version":[{"id":424282,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/57360\/revisions\/424282"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media\/54828"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=57360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=57360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=57360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}