6

How can I change attribute set of existing products in magento version 1.9. I searched the solution in google got this Flagbit change attribute (for upto 1.6). But I want it for version 1.9 or more.

1
  • this is not good, better you recreate the product, even if you change the attribute set id of the product, you will have to work more to remove database entries of previous attribute set (garbage values) – MTM Sep 23 '16 at 8:24
3

Yes. We can change product attribute set programmatically. I prefer to create massaction in catalog product grid to multiselect product and then select massaction for the products.

Creating massaction in grid.php

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

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

Creating controller action for change attribute sets of the selected products.

public function changeattributesetAction()
{
    $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()->addException($e, $e->getMessage());
            }
    }
    $this->_redirect('adminhtml/catalog_product/index/', array());
}
1
  • -2 :P Do NOT use this. What about orphan records or configurable product ... ? – sv3n Oct 22 '18 at 21:06
3

Flagbit-ChangeAttributeSet just wasn't updated at Magento Connect. Make sure to use version from Github. It definitly works for Magento 1.9.

this is not good, better you recreate the product, even if you change the attribute set id of the product, you will have to work more to remove database entries of previous attribute set (garbage values) – MTM Sep 23

This has been patched while ago, there is a config option to delete orphan records.

If something does not work ... please open ticket there or leave comment here!


Edit: Latest version is available at new marketplace: https://marketplace.magento.com/flagbit-magento-changeattributeset.html


Edit: If you have used this before the patch, you can use this to remove old orphan data.

$entityType = Mage::getModel('catalog/product')->getResource()->getTypeId();
$attributeSetCollection = Mage::getResourceModel('eav/entity_attribute_set_collection')
    ->setEntityTypeFilter($entityType);

foreach ($attributeSetCollection as $attributeSet) {
    $productIds = Mage::getResourceModel('catalog/product_collection')
        ->addFieldToFilter('attribute_set_id', $attributeSet->getId())
        ->getAllIds();

    $attributeIds = Mage::getResourceModel('catalog/product_attribute_collection')
        ->setAttributeSetFilter($attributeSet->getId())
        ->getColumnValues('attribute_id');

    $resource = Mage::getSingleton('core/resource');
    $write = $resource->getConnection('core_write');

    $condition = array(
        $write->quoteInto('entity_id IN (?)', $productIds),
        $write->quoteInto('attribute_id NOT IN (?)', $attributeIds)
    );

    $tables = array(
        'catalog_product_entity_datetime',
        'catalog_product_entity_decimal',
        'catalog_product_entity_int',
        'catalog_product_entity_text',
        'catalog_product_entity_varchar'
    );

    foreach ($tables as $table) {
        $write->delete($resource->getTableName($table), $condition);
    }
}
2

Once set you can't change the attribute set of a product. There is no such functionality in default Magento 1.x, so you have to delete the items and re-create new one with correct set. A work-around to avoid filling all the details will be:

  • Export goods s you want to change set for
  • After that remove that goods
  • Open the file and input new attribute set name (put existing)
  • Import file back with changes

Other solutions are - make changes directly in database (if you have tech skills and know what you are doing) or use extension: I recommend Store Manager for Magento supporting Magento 1.6.x-1.9.x and 2.0x. It is paid, but for one-time operations you can use 14-day free version (no fees, no cc or paypal needed).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.