Magento WizardWelcome to the Number 1 Magento Hosting website.

Contact the site  Make Money

Changing an Existing Product to Have an Attribute Set

 Add to Favourites  
(+2 rating, 2 votes)
Loading ... Loading ...

With Magento, you have the ability of adding attribute sets really easy.  That is, if you have a group of product which all have the same options – you do not have to create those options repetitively (and tediously) with each product – instead you create an Attribute set.

For instance, say you’ve got a category on your web-site which sells computer monitors.  You also need to establish attributes within product the resoliton, screen size, and brightness of the monitor.  Rather than going into each product one by one and adding those three options, you merely create a single attribute set, called “Monitor” (for example).  Then, when you create a product in Magento, its simply a case of selecting the attibute set you want to apply to the product, and hey presto, your job is done!

HOWEVER, at the moment – if you create a “Simple product” – like many people would usually do when they are starting out with Magento, you’ll encounter a problem.  Once you’ve selected what type of product it is, you cannot go back and change or add an attribute set to the product – until now…

Pasting the code below into certain Magento files will alleviate the problem.  You can then simply go into Admin > Manage Products, select multiple products using checkboxes, and use the drop down box at the top-right of the screen to change the Attribute Sets of multiple products at once.  Now didn’t that save a lot of time..!

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)));
}

Hope this helps!

  • Digg
  • del.icio.us
  • Facebook
  • Google
  • LinkedIn
  • Live
  • Reddit
  • StumbleUpon
  • Technorati
  • TwitThis

about the author

    This article was written by Sam Davis on October 17, 2008.
    Computing over a glass of Grenache Shiraz... again!
    Sam is the Editor of Blasted Thing. Contact Us

related articles

 Product Tags on Product Page - Video Guide
 Template Path Hints - Video Guide
 Disable Cache while developing your site - Video Guide
 Import/Export Data with Profiles - Video Guide
 Google Base Integration - Video Guide

comments

15 Responses to “Changing an Existing Product to Have an Attribute Set”

  1. Conor Wyse on November 12th, 2008

    Thanks! It works like a treat.

    A word of warning: there are (word processor style) single left quotes and single right quotes in the code which need to be replaced by simple single quotes. Also there are a couple of double quotes which should actually be pairs of (empty) single quotes.

  2. Tiago Matos on July 21st, 2009

    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)));
    }

    $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
    )
    )
    ));

  3. Great mod! on July 31st, 2009

    Thanks for the trick, it worked perfectly (following the recommandation of changing quotes into simple quotes).

  4. Ashish on September 24th, 2009

    This is really a good solution for this problem. I am wondering whether the coming version will resolve the issue by default or not. Thanks for the advice mate.

  5. David on May 26th, 2010

    This worked very well, however I’m looking for something a little different. Is there a way to add an action to change the product type? I want to change 1000 products from simple products to downloadable. Does anyone know of a solution.

  6. Gordon on August 6th, 2010

    Works like a charm. Thanks mate.

  7. Jersey Web Design on January 24th, 2011

    Top work Sam. Have saved me a number of headache tablets!

  8. Martin Wann on March 25th, 2011

    Hi!
    Please look at Store Manager for Magento http://www.mag-manager.com/ and its product management section.
    It has Change product attribute set option that is very simple to use. And offers a lot of other features like csv export/import, products copy/paste/clone and other.

  9. testphp on April 13th, 2011

    Hi , I have upload the same code .it works perfactly! fantastic…

    I am working on latest Magento v 1.5.1 ,

    so i want to add extra , Also aplly for Configurable product to chagne attribute set ..
    So it is possible then

    any one provide solution or help ..kind of job to finish..

  10. chandlers on May 6th, 2011

    Thanks, this is really useful! I’ve been trying to reorganise my products to make for use of the layered navigation, and so far the only way I’ve found to update the product attribute set was to delete the products, and reimport them. Thanks for saving me a LOT of time!

  11. Sip on May 25th, 2011

    I pasted it in as above and i get the following
    Parse error: syntax error, unexpected ‘&’ in /home/XXXXX/public_html/magento/app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php on line 43

  12. TFI on June 16th, 2011

    This worked extremely well and saved me a bunch of time re-entering products that have originally been imported Simple Products.

    Thanks!

  13. Peter Lugton on August 24th, 2011

    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. Brian on March 16th, 2012

    This is great!

    But why is this not a basic feature in the Magento core? These developers baffle me sometimes. It makes zero sense!

  15. erictr1ck on May 2nd, 2012

    worked in 1.6.2.0 thanks!

Leave a Reply




Spam protection by WP Captcha-Free


Delete Test Orders in Magento

  • Sponsored Ads

  • Magento Themes
    • baljit: pls helps me gateway error :transction is not accepeted...
  • Recent Tweets

    RT @benmarks: If you are participating in the Magento SE beta (http://t.co/NvfhEjr457) please encourage OPs to accept or propose & accept …

    RT @nChannelCloud: Go Beyond Simple #Magento #eBay Extensions with Multi-Channel Management http://t.co/7Q4WQpxM9K <---

    Go Beyond Simple #Magento #eBay Extensions with Multi-Channel Management http://t.co/7Q4WQpxM9K <---

    Nieuw: FedEx Dimensional / DIM Weight Calculation Fix http://t.co/x9ztISoOcN #magento #ecommerce #magentomodule

    Nieuw: ABF (ABFS) Freight Systems Inc. Shipping Module http://t.co/5SgKC6dAlm #magento #ecommerce #magentomodule