Siraj M Siraj M - 2 months ago 16
MySQL Question

Associate Simple Product to Configurable Product

Hi I am able to associate simple product to Configurable Product.
Using Code:

<?php
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();
$productId= 86971;
$productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('type_id','configurable')
->load($productId);

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

?>





But whenever I associate new simple product to configurable product, I am loosing old associated product. Can you please help me out on this.

Thank You in Advance

Answer

You're losing the old associations because saveProducts re-establishes the associations for a parent product with the product IDs you pass it. If you want to append a new product instead, you'll need to grab the old child product list, add the new product ID, and then call saveProducts with the resulting combined array.

$childProducts = Mage::getResourceSingleton('catalog/product_type_configurable')->getChildrenIds($productId)
$childProducts[] = '35683';
Mage::getResourceSingleton('catalog/product_type_configurable')->saveProducts($productId, $childProducts);
Comments