Magento: change applied attribute product types

Sometimes it is necessary to change the product type(s) an attribute is applied to in Magento. A common example is, that you designed and implemented an attribute for a configurable product, because you thought its real world value is equal over all child manifestations.
But after a while you realize, that there are differences that you have to consider. Let’s say the Magento products attributes attribute1 and attribute2 are currently applied to configurable products only. The following snippet shows, how you can achieve this within a modules setup script in Magento:

<?php
$oInstaller = $this;
/* @var $oInstaller Mage_Catalog_Model_Resource_Eav_Mysql4_Setup */

$oInstaller->startSetup();

$aAttributes = [
    'attribute1',
    'attribute2'
];

foreach ($aAttributes as $sCode) {
    $oInstaller->updateAttribute(
        Mage_Catalog_Model_Product::ENTITY,
        $sCode,
        'apply_to',
        Mage_Catalog_Model_Product_Type::TYPE_SIMPLE
    );
}

$oInstaller->endSetup();

// mark index as "reindex required"
$aIndexerCodes = [
    'catalog_product_attribute',
    'catalog_product_flat',
    'catalogsearch_fulltext'
];

$oIndexer = Mage::getModel('index/process');
foreach ($aIndexerCodes as $sCode) {
    $oIndexer->load($sCode, 'indexer_code')
             ->changeStatus(Mage_Index_Model_Process::STATUS_REQUIRE_REINDEX);
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.