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.
I tested your snippet but it don’t works. Magento redirect me to error page 404 when i submited my changes.
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
That is correct. Because these changes are being made to Core Mage files these changes can be overwritten with any upgrades.
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
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,
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?
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.
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.
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.
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.
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?
Works perfectly. Thank you for this!!
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
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.
You want to copy over the core files, edit them, and upload them to the local directories.