Assigning Pre-existing Simple Products to Configurable Products

Group simple products and assign them to configurable products.

Posted on March 18, 2016 in Magento, PHP

I’ve got a client database with a large range of stock items, which are being uploaded to Magento as simple products.

Now I need to group them up and assign them to configurable products with their size and color being their configurable attributes.

Well the notes here helped me get this running. So I thought I’d share with you the code to add a simple product to an existing configurable product.



protected function _attachProductToConfigurable($_childProduct, $_configurableProduct) 
{
    $load = Mage::getResourceModel('catalog/product_type_configurable')->load($_configurableProduct, $_configurableProduct->getId());
    $ids = $_configurableProduct->getTypeInstance()->getUsedProductIds();
    $newids = array();

    foreach($ids as $id) {
        $newids[$id] = 1;
    }

    $newids[$_childProduct->getId()] = 1;

    $loader->saveProducts ($_configurableProduct, array_keys( $newids));
}

I hope the code will help you when you encounter the simple problem.


comments powered by Disqus