Siraj M Siraj M - 10 months ago 82
MySQL Question

Associate Simple Product to Configurable Product

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

require_once('app/Mage.php'); //Path to Magento
$productId= 86971;
$productCollection = Mage::getResourceModel('catalog/product_collection')

foreach ($productCollection as $product) {
$simpleProductIds = array('35683'); // Simple product Ids
->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


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);