3

i´ve got a code like this:

$product_config = new Mage_Catalog_Model_Product_Type_Configurable();
$product_config->setProduct($product_grouped);
$assigned_products = $product_config->getUsedProducts();

So i create a new config, set the product and get all used products.

Here´s my problem: I want to add another product to the "list" of "used Products", so when I save the product later, the new product should also be added as new "configurable option" with a new price.

With Grouped Products it was much easiert, but i can´t find any way to do this also with a configurable product :(

Thanks for Help!

1 Answers1

0

You can use the product configurable type resource model to add a simple product to a configurable, e.g.:

$configurableId = 135;
$simpleId = 91;
$configurableProduct = Mage::getModel('catalog/product')->load($configurableId);

$simples = $configurableProduct->getTypeInstance('configurable', $configurableProduct)
    ->getUsedProductIds();
$simples[] = $simpleId;

Mage::getResourceModel('catalog/product_type_configurable')
    ->saveProducts($configurableProduct, $simples);
Oleg Ishenko
  • 2,243
  • 1
  • 15
  • 16
  • it seems to work (no error), but... i can´t see the results in the backend. the script writes the dataset in the table "catalog_product_super_link", but i can´t see the product and it´s options in the backend at "associated products". both, the config and the simple, uses the same attributeset :( – Chris Schrut Feb 14 '13 at 09:56
  • The new simple must have a value for the configurable attribute, which is not already available in other simples. Can you check if this is so with the product you are trying to add? – Oleg Ishenko Feb 14 '13 at 10:10
  • i´ve the attribute "color". and the other simples have i.e. the values blue and red, and i want to add white. but now where you said that i see, that theres also an error in my script, the attribute color for the new product wasn´t set. it was already, i will check this bug and then tell you, if everything works fine :) – Chris Schrut Feb 14 '13 at 10:14
  • ok, it works, i had a little typo error by getting the attribute values... thanks for your help, the script is great :) i think it works now. i will test it and i hope, i won´t comment here again ;) – Chris Schrut Feb 14 '13 at 10:18
  • ok, i have :) there just 1 nice2have left, maybe you can help also by that :) of course there wasn´t added a pricing value. but how can i handle this, if i just can save the simple id's? – Chris Schrut Feb 14 '13 at 10:42
  • Sorry, but I don't know an easy way to do so. Once I did it and it took a hack, which I am ashamed to share. – Oleg Ishenko Feb 14 '13 at 11:19
  • haha, i hope you haven´t overwritten core-files! ;) ok, i don´t think it´s that bad for the customer, i need to tell him that. maybe there´s no extra price for that – Chris Schrut Feb 14 '13 at 12:27
  • No, I didn't fall that low :) – Oleg Ishenko Feb 14 '13 at 12:41