Saturday, September 10, 2011

How to Change an Existing Products Attribute Set

I am working on a project which has different type of products having different product fields, initially i have created all the product under same default Attribute set, but after some time i realize that it will be tough to manage the products so i created different set of attribute sets, since i have already inserted lot of products and at such stage changing each products attribute set is something difficult process.

After doing some research i got a code to add the functionality to change a simple products attribute set directly on the Catalog > Manage Products page.

To achieve this go to file app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php
around line 253 add below line of code in function _prepareMassaction():

$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 in this file: 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)));
}

Now go to manage product in admin panel now you will be able to select the product and from action dropdown you can select the attribute set.

2 comments:

  1. I seem to get this error?

    Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in /home/mizled/public_html/app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php on line 925

    ReplyDelete
  2. This works 100% - Very easy and works. No excuses to make this not work. You need to know how PHP works, where it ends and starts. This is one basic requirement!

    ReplyDelete