Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

How to add a simple product associate with existing configurable product. 
 
ndlinh
Jr. Member
 
Total Posts:  3
Joined:  2009-03-25
 

Hi all,

I met a big problem when write a module to import products from csv file.
I has a configurable product which contains 3 existing associated products.
I want to add new associated product to this configurable product, but I can’t.
I got error message like this:

Zend_Db_Statement_ExceptionSQLSTATE[HY000]General error1452 Cannot add or update a child rowa foreign key constraint fails (`magento`.`catalog_product_super_link`, CONSTRAINT `FK_SUPER_PRODUCT_LINK_ENTITYFOREIGN KEY (`product_id`) REFERENCES `catalog_product_entity` (`entity_id`) ON DELETE CASCADEin C:\project\FO\shop\lib\Zend\Db\Statement\Pdo.php on line 238

Here is my code:

......
    
$groupProd $this->_initProduct($groupSku);
    if (
$groupProd->getId() != null{
                    $groupProd
->setCanSaveConfigurableAttributes(true);                    
                    foreach (
$products as $data{
                        $savedProducts[] 
$this->_updateSimpleProduct($data);
                    
}
                    $associate 
$groupProd->getTypeInstance()->getUsedProductIds();
                    foreach (
$savedProducts as $prod{
                        
if (!$prod) continue;
                        
$associate[] $prod->getId();
                    
}
                    $associate 
array_unique($associate);
                    
$groupProd->setConfigurableAttributesData($groupProd->getTypeInstance()->getConfigurableAttributesAsArray());
                    
$groupProd->setCanSaveCustomOptions(true);
                    
$groupProd->setConfigurableProductsData($associate);
                    
$groupProd->save();
                    
    
}
....

Please help me!
Thank you very much.

 
Magento Community Magento Community
Magento Community
Magento Community
 
nasha
Sr. Member
 
Avatar
Total Posts:  138
Joined:  2009-04-15
 

i am facing the same problem.let me know if you find solution.

 
Magento Community Magento Community
Magento Community
Magento Community
 
PaulE
Member
 
Avatar
Total Posts:  43
Joined:  2008-12-09
 

This is how I add an simple product to an existing configurable product.

public function addToConfigurable($config_product_id$simple_product_id)
    
{
        $config_product    
Mage::getModel('catalog/product') -> load($config_product_id);
        
$new_ids        = array();
        
$current_ids        $config_product -> getTypeInstance() -> getUsedProductIds();
        
$current_ids[]    $simple_product_id;
        
$current_ids        array_unique($current_ids);

        foreach(
$current_ids as $temp_id)
        
{
            parse_str
("position="$new_ids[$temp_id]);
        
}

        $config_product 
-> setConfigurableProductsData($new_ids) -> save();
    
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
Alan Whitelaw
Jr. Member
 
Total Posts:  2
Joined:  2010-05-28
 
PaulE - 20 October 2009 02:56 AM

This is how I add an simple product to an existing configurable product.

public function addToConfigurable($config_product_id$simple_product_id)
    
{
        $config_product    
Mage::getModel('catalog/product') -> load($config_product_id);
        
$new_ids        = array();
        
$current_ids        $config_product -> getTypeInstance() -> getUsedProductIds();
        
$current_ids[]    $simple_product_id;
        
$current_ids        array_unique($current_ids);

        foreach(
$current_ids as $temp_id)
        
{
            parse_str
("position="$new_ids[$temp_id]);
        
}

        $config_product 
-> setConfigurableProductsData($new_ids) -> save();
    
}

Sorry to resurrect an older thread, but is simple, useful and exactly what I am looking for.

Could someone please advise where I could put the code in PaulE’s response? How can I create a file that I can run from within a browser? I presume I cannot just stick it in a view? Is there some best practice?

Thanks and regards

Alan

Magento 1.4.2

 
Magento Community Magento Community
Magento Community
Magento Community
 
naturalcrusader
Jr. Member
 
Total Posts:  16
Joined:  2010-08-12
 
PaulE - 20 October 2009 02:56 AM

This is how I add an simple product to an existing configurable product.

public function addToConfigurable($config_product_id$simple_product_id)
    
{
        $config_product    
Mage::getModel('catalog/product') -> load($config_product_id);
        
$new_ids        = array();
        
$current_ids        $config_product -> getTypeInstance() -> getUsedProductIds();
        
$current_ids[]    $simple_product_id;
        
$current_ids        array_unique($current_ids);

        foreach(
$current_ids as $temp_id)
        
{
            parse_str
("position="$new_ids[$temp_id]);
        
}

        $config_product 
-> setConfigurableProductsData($new_ids) -> save();
    
}

its not saving - not sure why here is print out

array(0{ } step 2
array(2{ [0]=> int(35[1]=> int(36} setp 3
array(2{ [0]=> int(35[1]=> int(36} step 4
array(2{ [35]=> array(1{ ["position"]=> string(0"" } [36]=> array(1{ ["position"]=> string(0"" } } going into config product
 
Magento Community Magento Community
Magento Community
Magento Community
 
Oleg Kulik
Jr. Member
 
Avatar
Total Posts:  2
Joined:  2010-07-17
 

Ressurecting this tread just in case someone have run into same trouble as 1-st post describes.
In that example argument for setConfigurableProductData function - array $associate should contain product ids in keys, not in values!
This is very important, since further function takes keys with array_keys and assums that it is product ids given.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mohammed Meabed
Jr. Member
 
Avatar
Total Posts:  6
Joined:  2010-10-28
Munich - Germany
 

as Oleg Kulik said :

File:\\app\\code\\core\\Mage\\Catalog\\Model\\Product\\Type\\Configurable.php
        
/**
         * Save product relations
         */
        
$data $this->getProduct($product)->getConfigurableProductsData();
        if (
is_array($data)) {
            $productIds 
array_keys($data);
            
Mage::getResourceModel(\'catalog/product_type_configurable\')
                ->
saveProducts($this->getProduct($product), $productIds);
        
}

The array keys must contain the products ids you want to relate them to the config products.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top