Magento, by default, does not facilitate to change the attribute type. 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.
For example, a brand store manufactures multiple types of products and the user inputs product that does not come under the manufacturer’s 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.
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!
A custom script is required to convert attribute type from text to dropdown in Magento to overcome the default Magento limitation.
The below method facilitates to convert an attribute type in Magento.
Method to Convert Attribute type from TEXT to DROPDOWN in Magento:
1. Create root script changeAttr.php and place the below code to get all the value of attributes related to the products. Here, we are creating this for changing the attribute “genero” as an example. You may replace the code as per your need with your attribute.
require 'app/Mage.php'; Mage::app(); $p = 0; while (1) { $p++; $collection = Mage::getModel('catalog/product') ->getCollection() ->addAttributeToSelect('*') ->setPageSize(200) ->setCurPage($p) ->setOrder('id', 'ASC'); foreach ($collection as $product) { echo $product->getId() . "," . $product->getGenero()."<br>"; } if ($p >= $collection->getLastPageNumber()) { break; } }
2. Copy all this value and generate CSV file name genero.csv with 2 columns; id and genero.
3. The next step is to delete the text attribute genero.
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.
5. Place the CSV file genero.csv to root.
6. Change below code to changeAttr.php
require 'app/Mage.php'; Mage::app(); $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'genero'); $allOptions = $attribute->getSource()->getAllOptions(true, true); foreach ($allOptions as $instance) { $myArray[strtolower($instance['label'])] = $instance['value']; } echo '<pre>'; print_r($myArray); echo '</pre>'; if (($handle = fopen("genero1.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $data = array_map("utf8_encode", $data); list($_id, $_value) = $data; $product = Mage::getModel('catalog/product')->load($_id); $product->setGenero($myArray[strtolower($_value)]); echo $_id.'--'.$myArray[strtolower($_value)].'--'.$_value."<br>"; try { $product->getResource()->saveAttribute($product, 'genero'); } catch(Exception $e) { echo $_id .":". $e->getMessage() ."\n"; } } fclose($handle); }
Thank you.