1

How can I convert a product attribute type from Text to Select in the Magento Backend. Is there any in-built functionality or we need to write custom code for that?

I already have attribute values assigned for many products, so don't want to lose that in this conversion.

Thanks

4
  • Can you explain in more detail what you are trying to do. Are you talking about an attribute in the backend? You want to migrate the attribute from a text value to a dropdown value without loosing the values already entered for the products? Commented Aug 13, 2013 at 13:51
  • yes exactly that.
    – Kalpesh
    Commented Aug 13, 2013 at 13:56
  • No built in functionality that I know of — I believe the Magento UI prevents you from changing this to avoid the complications that might arise from converting an attribute's type like that. Commented Aug 13, 2013 at 19:47
  • @AlanStorm Hmm you are right. I am re-creating the attribute with dropdown type as Sander suggested.
    – Kalpesh
    Commented Aug 13, 2013 at 19:53

1 Answer 1

2

You would first need to have a script that stores all values per product in a CSV for example

[...]
$collection = Mage::getModel('catalog/product')
                        ->getCollection()
                        ->addAttributeToSelect('sku')
                        ->addAttributeToSelect('your-attribute');

foreach ($collection as $product) {
    echo "{$product->getId()};{$product->getData('your-attribute')}\n";
}
[...]

executing this from the commandline php /path/to/script.php > values.csv will create a CSV with the ID's and attribute values.

Now the next step would be to remove the old attribute and create a new one with the text values as dropdown options.

Then we need to import those values again retrieving the option ID from the value which should now be the admin label

[...]

$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'your-attribute'); 

$attribute_source = Mage::getModel('eav/entity_attribute_source_abstract');
$attribute_source->setAttribute($attribute);

if (($handle = fopen("your-csv-file.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
        list($_id, $_value) = $data;

        $product = Mage::getModel('catalog/product')->load($_id);
        $product->setData('your-attribute', $attribute_source->getOptionId($_value));

        try {
            $product->save();
        } catch(Exception $e) {
            echo "{$_id}: {$e}";
        }
    }
    fclose($handle);
}

[...]

This code is untested so please don't use it in a production environment. Let me know if it worked for you.

3
  • Thanks for help! There were things needed to be modified to get this to work. I have dedicated a post for this with detailed instructions on how to do it step-by-step here: ka.lpe.sh/2013/08/13/…
    – Kalpesh
    Commented Aug 13, 2013 at 23:26
  • Great you've worked it out, as I said the code was untested so I expected you needed to tweak it. Might I suggest providing a source with your article to help Magento Stackexchange grow? Commented Aug 14, 2013 at 7:03
  • 1
    Given link at the end of the post :)
    – Kalpesh
    Commented Aug 14, 2013 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.