Tuesday 18 September 2012

Magento: Change Simple Product to Configurable Product

One of the big mysteries - at least to me - is why magento admin is not able to change a simple product to a configurable product.
It is obvious that this is a real world usecase!
Anyway: The guys from Flagbit GmbH delivered a free module to change the Attribute Set:
http://www.magentocommerce.com/magento-connect/flagbit-change-attribute-set.html

So far so good. But what is the use of a product with an different attribute set, when you still need it as configurable product.
I hacked the code. Now if you change the attribute set to 'default' then the product type is changed to 'simple'. Setting any other attribute set to the product leads to a product_type of 'configurable'.

Important: This code does not work with 'bundle' products.

What you need to do:

  • Find \app\code\community\Flagbit\ChangeAttributeSet\controllers\Adminhtml\Catalog\ProductController.php
  • Make a backup of this file
  • Find the public function changeattributesetAction()
  • Change it to:


 
public function changeattributesetAction()
{
  $productIds = $this->getRequest()->getParam('product');
  $storeId = (int)$this->getRequest()->getParam('store', 0);
    $attribute_set = $this->getRequest()->getParam('attribute_set');

    $entityTypeId = Mage::getModel('eav/entity')
                  ->setType('catalog_product')
                  ->getTypeId();
    $attributeSetName = Mage::getModel('eav/entity_attribute_set')
                    ->getCollection()
                    ->setEntityTypeFilter($entityTypeId)
                    ->addFieldToFilter('attribute_set_id', $attribute_set)
                    ->getFirstItem()
                    ->getAttributeSetName();
    if ($attributeSetName == 'Default') { $product_type = 'simple'; } else { $product_type = 'configurable'; }
    
  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();
          $product = Mage::getSingleton('catalog/product')
            ->unsetData()
            ->setStoreId($storeId)
            ->load($productId)
            ->setTypeId($product_type)
            ->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());
 } 


  • That's all!


Feel free to add comments.

Credits: Thanks to http://www.craftyfella.com/2010/01/syntax-highlighting-with-blogger-engine.html for the syntax highlighter tutorial.