How to Change an Existing Products Attribute Set in Magento

Unfortunately Magento does not have the function built into its setup to allow you the option to change a products attribute set once you have created a simple product. However, with the following coding updates, you can add the functionality to change a simple products attribute set directly on the Catalog > Manage Products page:

In app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php around line 253 ad:

$sets = Mage::getResourceModel(‘eav/entity_attribute_set_collection’)
->setEntityTypeFilter(Mage::getModel(‘catalog/product’)
->getResource()->getTypeId())->load()->toOptionHash();

array_unshift($statuses, array(‘label’=>”, ‘value’=>”));
$this->getMassactionBlock()->addItem(‘attribute_set’, array(
‘label’=> Mage::helper(‘catalog’)->__(‘Change attribute set’),
‘url’ => $this->getUrl(‘*/*/massAttributeSet’, array(‘_current’=>true)),
‘additional’ => array(
‘visibility’ => array(
‘name’ => ‘attribute_set’,
‘type’ => ‘select’,
‘class’ => ‘required-entry’,
‘label’ => Mage::helper(‘catalog’)->__(‘Attribute Set’),
‘values’ => $sets
)
)
));

And then in app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php (anywhere in the class) add a new function:

public function massAttributeSetAction(){

$productIds = $this->getRequest()->getParam(‘product’);
$storeId = (int)$this->getRequest()->getParam(‘store’, 0);
if(!is_array($productIds)) {
$this->_getSession()->addError($this->__(‘Please select product(s)’));
} else {
try {
foreach ($productIds as $productId) {
$product = Mage::getSingleton(‘catalog/product’)
->unsetData()
->setStoreId($storeId)
->load($productId)
->setAttributeSetId($this->getRequest()->getParam(‘attribute_set’))
->setIsMassupdate(true)
->save();
}
Mage::dispatchEvent(‘catalog_product_massupdate_after’, array(‘products’=>$productIds));
$this->_getSession()->addSuccess(
$this->__(‘Total of %d record(s) were successfully updated’, count($productIds)));
} catch (Exception $e) {
$this->_getSession()->addError($e->getMessage());
}
}

$this->_redirect(‘*/*/’, array(‘store’=>(int)$this->getRequest()->getParam(‘store’, 0)));
}

Once you have added the two snippets of code you will now be able to select products from teh Manage Products list and update any products Attribute Set from the actions dropdown menu.

Simply Select ‘Change Attribute Set’ and select the appropriate attribute set from the new dropdown menu that appears to the right of Actions.

Comments

  1. I tested your snippet but it don’t works. Magento redirect me to error page 404 when i submited my changes.

  2. I found my mystake !

    I had put changes in the “local” folder !

    Little question : if magento update, the code will be overwritten ?

    Thanks for your snippet !

    nico

  3. SEM Truth says:

    That is correct. Because these changes are being made to Core Mage files these changes can be overwritten with any upgrades.

    • I just read Magento Release Notes for version 1.4.0.0-rc1 AmazonPayments moulde is removed from package and will be available as a separate extension on Magento Connect It makes sense. I just need to read every release note to be aware of potential problems.

  4. I’m somewhat confused, Can you please explain in detail on which line, or after which code the snippet should be insert into on grid.php. I;m running 1.4.01 and line 253 seems to be middle of a function, thanks

  5. Nice tutorial…

    I just got an error, maybe i have to delete previous attribute_set information for the product and then apply the new attribute_set_id

    SQLSTATE[HY000]: General error: 1452 Cannot add or update a child row: a foreign key constraint fails (`pcel_db/catalog_product_entity`, CONSTRAINT `FK_CATALOG_PRODUCT_ENTITY_ATTRIBUTE_SET_ID` FOREIGN KEY (`attribute_set_id`) REFERENCES `eav_attribute_set` (`attribute_set_id`) ON DELETE CASCADE)

    thanks,

  6. I using the code correctly in both places, the first snippet in my local files and the second snippet in my core file… and I am still getting a 404 redirect when i click “submit” and when i return to the page the change was not actually made. Is there anything I can do to fix this? I am desperate to use it… One thing i did notice is that my url after clicking submit looks like this…
    http://www.domain.com/index.php/admin/catalog_product/massAttributeSet/key/340380598230582085023840938931873

    it seems like its the /massAttributeSet/ that is causing the issues. Any ideas?

  7. SEM Truth says:

    Nico, An update to your question about updates overriding the changes. To Correct my answer, the reason for creating updates in the ‘local’ folder is because they will not be overwritten with updates.

  8. SEM Truth says:

    ERG
    I have not heard of this result when applying this update. Please provide more information including Magento version.

    I would try to remove any other extensions.
    Also if you have complex attribute sets there may be unforeseeable issues.

  9. SEM Truth says:

    Alana
    What version of Magento are you running?

    Please provide more information on how you are updating the Attribute Set Code.

    You can also just utilize this great free Extension by Flagbit for changing the attribute set of a Magento product.

  10. SEM Truth says:

    Lastly as an alternative you should be able to update the attribute_set_id in the catalog_product_entity MySql Table. I would recommend making a backup, especially if you are unfamiliar or just don’t want to take chances.

    If the site is not live or in production mode, diving into PHP MyAdmin can be the best way to learn.

  11. It turns out that i was missing the last ; when i pasted! Sorry! works great. Will this same method apply to changing a products type as well? Like changing a simple to configurable?

  12. Emily Katherine says:

    Works perfectly. Thank you for this!!

  13. Hi
    Sorry, have just found this fix to adding attributes to old products!
    I have added the code to both the files but I must be adding it in the wrong place.
    Can anyone let me see an example so that I place it correctly.
    Ta
    Pete

  14. I’m confused as to which files to edit. The original post references core mage files, but later there’s a reference to updating local files only, so that updates won’t remove the code.

  15. magento 1.7 stable is out. upgraded. Patched with instructions above.
    My admin page freezes after applying the scripts. possibly due to applying in the wrong place.
    Please post complete dump of both (ProductController.php , Grid.php) files.
    It would be a good idea so that others can copy/paste from the source.

  16. PLEASE HELP ME
    I installed the extension on local folder and when I try to change the attribute set I get 404 error:
    http://www.ingenium.ro/index.php/admin/catalog_product/massAttributeSet/key/46122332452ef31dd17d5d9e24e6ebc3/
    How to solve the problem?
    I am on Magento CE 1.6

Speak Your Mind

*

CAPTCHA image
*