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.

15 comments:

  1. This is neat. I've been looking for something like this. Your blog really helped me a lot.

    ReplyDelete
  2. You are my hero! Worked perfectly.. converts simple to configurable products (with help of extension above) without any that I can see

    ReplyDelete
  3. Thanks for sharing this information in very detail ,really useful .




    Magento Developers



    ReplyDelete
  4. This works but after selecting associated products I end up with duplicate attributes (two dropdowns with same choices). Any ideas on how to fix that?

    ReplyDelete
    Replies
    1. Thats strange. Tell me more about your structure of your website, stores and languages.

      Delete
  5. Whoa--- I found it(!) under 'custom option'.... I'm not sure but perhaps that came across with migration. I'm on track to figuring this out. Thanks so much!

    ReplyDelete
    Replies
    1. You are wellcome. especially for the last part you found out by yourself. ;)

      Delete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Hi Mr.Bosch,

    after i replaced the file with the code you provided, when i click "manage product" on admin, no products will show up with just a blank page. any idea why? if possible, can you upload the updated file so i can download it since i think maybe i did something wrong when replacing the code. thanks in advance, Jason

    ReplyDelete
    Replies
    1. Did you have Code Compilation on? Maybe. Just try again with a backup of the orig file. And be carefull to copy the code right. This code works for 1.6.2. I am remote from my code currently, so I can't send you.

      Delete
  8. Thank you so much, worked beautifully! Only problem is new configurable products are no longer searchable in frontend? Any ideas how to fix?

    ReplyDelete
    Replies
    1. Are other products with this attribute found? Clear cache. Rebuild index ... the usual stuff.

      Delete
  9. You're a legend!! Just saved me DAYS of additional work for a client. Worked perfectly for 1.7.0.2 :-)

    ReplyDelete
  10. Thank you for such a great help. But i am not sure how to use it. I created two products with default attribute set. Default attribute set having 2 configurable attributes, i.e. "color" and "manufacturer". My need is if i select those both simple product and change attribute set, it should create new configurable product with selected products as associated products.
    Using your code i also need to reopen each product and set associated products manually.
    Please suggest something to achieve my need.Thank you.

    ReplyDelete
    Replies
    1. Hi, the code above is not supposed to handle your request. Sorry.

      Delete

Thanks for commenting. Your comments are reviewed before listed here.