Magento: convert single select attribute to multiselect

7. August 2014 at 17:24

Add in your entity-model following functions:

    /**
     * Get a text for option value
     *
     * @param string $value
     * @return string|false
     */
    public function getOptionText($value)
    {
        $options = $this->getAllOptions();
        foreach ($options as $option) {
            if ($option['value'] == $value) {
                return $option['label'];
            }
        }
        return false;
    }
    public function getFlatColums()
    {
        $columns = array();
        $columns[$this->getAttribute()->getAttributeCode()] = array(
            'type'      => 'VARCHAR',
            'unsigned'  => false,
            'is_null'   => true,
            'default'   => null,
            'extra'     => null
        );
        return $columns;
    }
    /**
     * Retrieve Indexes for Flat
     *
     * @return array
     */
    public function getFlatIndexes()
    {
        $indexes = array();
        $index = 'IDX_' . strtoupper($this->getAttribute()->getAttributeCode());
        $indexes[$index] = array(
            'type'      => 'index',
            'fields'    => array($this->getAttribute()->getAttributeCode())
        );
        return $indexes;
    }
    /**
     * Retrieve Select for update Attribute value in flat table
     *
     * @param   int $store
     * @return  Varien_Db_Select|null
     */
    public function getFlatUpdateSelect($store)
    {
        return Mage::getResourceModel('eav/entity_attribute_option')
            ->getFlatUpdateSelect($this->getAttribute(), $store, false);
    }

Now the setup script to convert 2 attributes

$this->startSetup();
$installer = $this;
$attributes = array(array('name' => 'ENTER_ATTRIBUTE_KEY_HERE', 'attribute_id' => ''),  // enter the attribute_key for ENTER_ATTRIBUTE_KEY_HERE
                    array('name' => 'ENTER_ATTRIBUTE_KEY_HERE', 'attribute_id' => '')
            );
$productEntityTypeId = Mage::getModel('catalog/product')->getResource()->getTypeId();
foreach($attributes as $id => $value) {
    $attributeId = $installer->getAttributeId($productEntityTypeId, $value['name']);
    $attributes[$id]['attribute_id'] = $attributeId;
    $installer->updateAttribute($productEntityTypeId, $attributeId, array(
        'frontend_input' => 'multiselect',
        'backend_type' => 'varchar',
        'backend_model' => 'eav/entity_attribute_backend_array'
    ));
}
foreach($attributes as $id => $attributeData) {
    // now copy attribute values from old attribute to new
    $sql = 'INSERT INTO ' . $this->getTable('catalog_product_entity_varchar') . '(entity_type_id, attribute_id, store_id, entity_id, value)
            SELECT entity_type_id, attribute_id, store_id, entity_id, value
            FROM ' . $this->getTable('catalog_product_entity_int')
            . ' WHERE attribute_id = ' . $attributeData['attribute_id'] . ';';
    $installer->run($sql);
    // delete values from old attribute (you must do this)
    $sql = 'DELETE FROM ' . $this->getTable('catalog_product_entity_int')
        . ' WHERE entity_type_id = 4 and attribute_id = ' . $attributeData['attribute_id'] . ';';
    $installer->run($sql);
}
$this->endSetup();
GD Star Rating
loading...
GD Star Rating
loading...
Magento: convert single select attribute to multiselect, 10.0 out of 10 based on 5 ratings