Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento changing attribute type in backend

is it possible to change type of a attribute after it has been created. I want to change type of certain attribute to Multi select list. Type of these attribute is currently 'Dropdown'. Actually When I create the attributes, there was no need of multi select when I created it originally, but, now client wants to change it to "Multi select".

Please help me out, I can not create new attributes by deleting the old ones, as, there is some data, and certain part of design is hard coded and depends on certain values of attribute.

like image 298
Ravish Avatar asked Oct 21 '11 12:10

Ravish


2 Answers

Yes it's possible programmatically thanks to the method Mage_Eav_Model_Entity_Setup::updateAttribute($entityTypeId, $id, $field, $value=null, $sortOrder=null)

It's not possible with the Attribute Management in Magento Backend because it has consequence with the existing data. In your case, changing from select to multiselect should be ok but do a database backup and test if your product are still correctly displayed.

Programmatically, the best way is to do it from an update setup script. I don't know your module but here are some information to do it.

An update setup script is launched when you provide a new number version to your module and you provide a setup script with the old and new version number as a filename.

1) Here is the header of a config.xml module, change it to provide a higher version. For example, the new version is

<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
    <Mycompany_Mymodule>
        <version>1.0.1</version><!-- the old one was 1.0.0 -->
    </Mycompany_Mymodule>
</modules>
...
</config>

2) you need to have in the config.xml file, between the tags <global>...</global> the following code, please adapt to your situation:

  <resources>
        <mymodule_setup><!-- name that you will give to the folder into the sql folder  -->
            <setup>
                <module>Mycompany_Mymodule</module>
                <class>Mage_Eav_Model_Entity_Setup</class><!-- You can have a setup class which extends this class -->
            </setup>
            <connection>
                <use>default_setup</use>
            </connection>
        </mymodule_setup>
    </resources>

3) Then you need to create a setup script in your module folder with the old and new version number app/code/local/mycompany/mymodule/sql/mymodule_setup/mysql4-upgrade-1.0.0-1.0.1.php (mysql4-upgrade-old.version.number-new.version.number.php)

4) And in this new script set a code like this, please adapt to your situation:

<?php
$installer = $this;
/*@var $installer Mage_Eav_Model_Entity_Setup */

$entityTypeId = $installer->getEntityTypeId('catalog_product');

$idAttributeOldSelect = $this->getAttribute($entityTypeId, 'myold_attribute', 'attribute_id');
$installer->updateAttribute($entityTypeId, $idAttributeOldSelect, array(
    'frontend_input'    => 'multiselect'
));

5) Refresh your Magento page and eventually flush your cache

like image 186
Sylvain Rayé Avatar answered Oct 11 '22 13:10

Sylvain Rayé


4.I think update uses database fields, i.e input should frontend_input.

<?php
$installer = $this;
/*@var $installer Mage_Eav_Model_Entity_Setup */

$entityTypeId = $installer->getEntityTypeId('catalog_product');

$idAttributeOldSelect = $this->getAttribute($entityTypeId, 'myold_attribute', 'attribute_id');
$installer->updateAttribute($entityTypeId, $idAttributeOldSelect, array(
    'frontend_input' => 'multiselect'
));
like image 45
muhsin Avatar answered Oct 11 '22 14:10

muhsin