Tutorialsplane

Magento assign simple product to configurable programmatically


Magento assign simple product to configurable programmatically : You can assign simple products to configurable product programatically. First get the configurable product object and then get product ids. Now assign these ids to the configurable product with the help of the catalog/product_type_configurable resource. Here we are going to explain the example of assign simple products to the configurable products programmatically.


Magento assign simple product to configurable programmatically

Here is syntax to assign products to the configurable products-

Magento assign simple product to configurable programmatically


$productId = 23 // id of configurable product
$productCollection = Mage::getResourceModel('catalog/product_collection')
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('type_id','configurable')
             ->load($productId); 

foreach ($productCollection as $product) {
  $simpleProductIds = array('1','9','5'); // Simple product Ids
  Mage::getResourceSingleton('catalog/product_type_configurable')
    ->saveProducts($product, $simpleProductIds);
}

First get the product collection of configurable product and then get object of the product $product now assign simple products to the configurable by simple products id ie. $simpleProductIds = array(‘1′,’9′,’5’); which is array of simple product ids.