{"id":225,"date":"2018-11-02T12:22:16","date_gmt":"2018-11-02T12:22:16","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2018\/11\/02\/convert-attribute-type-from-text-to-dropdown-in-magento\/"},"modified":"2025-05-22T17:14:53","modified_gmt":"2025-05-22T11:44:53","slug":"convert-attribute-type-from-text-to-dropdown-in-magento","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/convert-attribute-type-from-text-to-dropdown-in-magento\/","title":{"rendered":"How to Convert Attribute type from TEXT to DROPDOWN in Magento"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento, by default, does not facilitate to change the attribute type.&nbsp;The admin may require to migrate the attribute from a text value to a drop-down value without losing the values already entered for the products. This happens when the text attribute is selected by mistake. The actual requirement would be a conditional selection which is not satisfied in the text attribute.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, a brand store manufactures multiple types of products and the user inputs product that does not come under the manufacturer\u2019s list. To restrict the user selecting out of scope options, a drop-down attribute is useful. If by mistake the text attribute is selected in this case, it can be converted to a dropdown list without losing the entered values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, if you have selected text attribute to add true-false values, i.e., enable or disable, yes or no; the below method is at your rescue!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A custom script is required to&nbsp;<em><strong>convert attribute type from text to dropdown in Magento&nbsp;<\/strong><\/em>to overcome the default Magento limitation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The below method facilitates to convert an attribute type in Magento.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Convert Attribute type from TEXT to DROPDOWN in Magento:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create root script&nbsp;<strong>changeAttr.php<\/strong>&nbsp;and place the below code to get all the value of attributes related to the products.&nbsp; Here, we are creating this for changing the attribute \u201c<strong>genero<\/strong>\u201d as an example.&nbsp; You may replace the code as per your need with your attribute.<\/p>\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=\"\">require 'app\/Mage.php';\nMage::app();\n \n$p = 0;\nwhile (1) {\n \n    $p++;\n    $collection = Mage::getModel('catalog\/product')\n        ->getCollection()\n        ->addAttributeToSelect('*')\n        ->setPageSize(200)\n        ->setCurPage($p)\n        ->setOrder('id', 'ASC');\n \n    foreach ($collection as $product) {\n        echo $product->getId() . \",\" . $product->getGenero().\"&lt;br>\";\n    }\n \n    if ($p >= $collection->getLastPageNumber()) {\n        break;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Copy all this value and generate CSV file name&nbsp;<strong>genero.csv<\/strong>&nbsp;with 2 columns; id and genero.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3. The next step is to delete the text attribute genero.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">4. Now, create a new attribute with same code genero with drop-down type and add all the options and assign to the corresponding attribute set.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">5. Place the CSV file&nbsp;<strong>genero.csv<\/strong>&nbsp;to root.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">6. Change below code to&nbsp;<strong><strong>changeAttr.php<\/strong><\/strong><\/p>\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=\"\">require 'app\/Mage.php';\nMage::app();\n \n \n$attribute = Mage::getModel('eav\/config')->getAttribute('catalog_product', 'genero');\n$allOptions = $attribute->getSource()->getAllOptions(true, true);\nforeach ($allOptions as $instance) {\n    $myArray[strtolower($instance['label'])] = $instance['value'];\n}\n \necho '&lt;pre>';\nprint_r($myArray);\necho '&lt;\/pre>';\n \nif (($handle = fopen(\"genero1.csv\", \"r\")) !== FALSE) {\n    while (($data = fgetcsv($handle, 1000, \",\")) !== FALSE) {\n \n        $data = array_map(\"utf8_encode\", $data);\n \n        list($_id, $_value) = $data;\n \n        $product = Mage::getModel('catalog\/product')->load($_id);\n        $product->setGenero($myArray[strtolower($_value)]);\n \n \n        echo $_id.'--'.$myArray[strtolower($_value)].'--'.$_value.\"&lt;br>\";\n \n        try {\n            $product->getResource()->saveAttribute($product, 'genero');\n        } catch(Exception $e) {\n            echo $_id .\":\". $e->getMessage() .\"\\n\";\n        }\n    }\n    fclose($handle);\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento, by default, does not facilitate to change the attribute type.&nbsp;The admin may require to migrate the attribute from a text value to a drop-down&#8230;<\/p>\n","protected":false},"author":5,"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-225","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/225","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\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=225"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/225\/revisions"}],"predecessor-version":[{"id":15566,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/225\/revisions\/15566"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}