Magento API: Associate simple products to configurable or grouped product

By default, Magento API does not allow to assign products to any configurable or grouped product. Here is an extension that will allow you to do so by simply specifying the simple products skus to associate when creating configurable or grouped product.

Extension installation

Extension is available on GitHub: https://github.com/jreinke/magento-improve-api

You have 2 ways to install it:

  1. Manual installation : download package here, unzip in Magento root folder, then clean cache.
  2. Installation with modgit:

    modgit -e README.md clone bubble-api https://github.com/jreinke/magento-improve-api.git

    See a full example of how to use modgit here.

Usage

To associate simple products automatically and regardless of the API version of Magento you are using (v1, v2, v2 + WSI-Compliance), simply add a key associated_skus to product data array of configurable or grouped product to create/update.

Example with configurable product API v1: Show

Example with configurable product and API v2: Show

Example with configurable product and API v2 + WSI-Compliance: Show

Other improvements

The extension makes other improvements that will be described in another post. These are listed below:

  • Ability to specify category names rather than ids;
  • Ability to specify the name of the attribute set rather than the id;
  • Ability to specify options labels rather than the ids;
  • Ability to specify the website code rather than the id.

51 Responses to “Magento API: Associate simple products to configurable or grouped product”

  1. ealexis says:

    When you say “By default, Magento API does not allow to assign products to any configurable or grouped product”, thats is not exactly true, with V1 api you can do something like :

    foreach ($product->associated_skus as $associated_sku) {
    $proxy->call($sessionId, ‘product_link.assign’, array(‘grouped’, $groupedProduct>sku, $associated_sku));
    }

    Anyways, magento is poorly documented

  2. ealexis says:

    I tried to implement your code for configurable product but it doesn’t seems to works. I am on Magento ver. 1.5.1.0

  3. ealexis says:

    True ^^ Well, i followed the steps for the manual installation and use the code in “product API v1″.
    I made sure the SKU’s in ‘associated_skus’ matches with the ones from the singled product i added before.

    The products adds correctly but the link between the Configurable product and the Simple products is not made. I don’t have any soap exception to see what happens

  4. Gabriel says:

    Hey man!
    Great post!
    It is what I was looking for :)

    But I have 1 question:
    I have some configurable products that are using two attributes (color and memory card).
    Using your module I’ve created the product (like red+ 2GB, blue+4GB), but I’ve failed when trying to set up the correct price for each attribute value.

    For example:
    For the color: ‘red’ , the price increases $5.00
    For the memory card ’2GB’, $15.00.

    There is any way to do it using your module ?

    Thanks

  5. erraprod says:

    Hi,
    finally i updated my magento build to latest (1.7.0.0) and now it works !Simple products are correctly associated with configurable and grouped products via “associated_skus” column, thanks for your code snippet man !

  6. ErraProd says:

    Yes, it’s not because of Magento 1.7, i made some tests back on my 1.5.1.0 and it’s working.
    In fact i imported my simple products the wrong way (without the “dropdown list” type attribute set, in “global scope” and with “Use To Create Configurable Product” to yes).
    So the products were associated internally but not shown because they were badly configured !

  7. Great work!

    I just created the configurable products out of my ERP system written in Java over WS-I v2 SOAP API.

  8. cimi says:

    Does it work for existing configurable products? Or, do I have to assign all simple products at the creation of the configurable product?

    I created configurable product before, and now I have a new color of it and I want to add this new color to it.

  9. Carlos says:

    Hi, I try to use your code for API 1.0, but the link between the configurable and simple produtct was not made. I use your code in a simple instalattion of magento. Can you help me?

  10. Marcos Curcio Gomes says:

    Hello my friend. Your extension saved my life, and my job too. I used with c# and WebService. I’m just having one problem: I want to insert the price changes on c#. I think could be something like this:

    product.price_changes = new associativeEntity[2];
    product.price_changes[0] = new associativeEntity();
    product.price_changes[0].key = “Preta”;
    product.price_changes[0].value = “120″;
    product.price_changes[1] = new associativeEntity(); product.price_changes[1].key = “Azul”;
    product.price_changes[1].value = “114″;

    When I save the configurable product, a exception appears:”Cannot use object of type stdClass as array”

    Could someone tell me how to do the price changes???
    Thanks

  11. Marcos Curcio Gomes says:

    I think may be a problem caused by the type of price_changes inside the wsdl.xml file . I need something like a complexObjectArray. I already tried to change the configuration of to complexFilterArray but the same error is ocurring.
    I just need to set the value of the price_changes inside my program in c#.
    Thanks.

  12. Ken says:

    I have a bit of confusion.

    I’m looking for a free/opensource solution to define the “super” attributes of a configurable product via xmlrpc api. Does this patch allow for that?

    I see what appears to be super attributes “color” & “size” in the price changes array. Is this the mechanism? I do not require/want price differentiation based upon super attributes.

    Any guidance would be appreciated.
    Thanks.
    -Ken

  13. Jude says:

    I’m trying to test your API here (in v1) but it doesn’t work…

    The configurable product and simple products are correctly created but any simple product is associated to the configurable product.
    I use Magento 1.5.0.1 and also the extension “Simple configurable product”.
    I really need to create configurable products via webservices ; how can I do ?

    In the BDD, the fields has_options and required_options in the table catalog_product_entity are at 0 ; perhaps there’s a problem here ?
    Can you help me ?

    • Johann Reinke says:

      Sorry I can’t help without more info, I don’t know how you call the webservices and which params you send. Maybe you have a conflict with the other module you are talking about.

  14. Dano Blanchard says:

    Johann,

    I know this is developed for CE 1.6+… What do you think the chances are that this will work in EE(1.12)? We are running into the same issue in EE where we can push the simple products but cannot not associate them to configurable products (we can associate to grouped products as mentioned)… but that’s not what we need for color/swatch selectable product displays.

    • Johann Reinke says:

      It should work fine in EE 1.12 but let me know if something is wrong with it.

      • Johann,

        I am more than happy to report (you should see the smiles) that your API extension works just fine on Enterprise Edition v1.12. Thank you so much for this piece of work. IMO you fixed a major hole in the Magento API that Magento wasn’t taking any responsibility for. Mage staff told me this issue wasn’t even in their road-map.

        Only one odd behavior we can’t figure out… Products go in perfectly and appear 100% correct in admin… (visibility, in-stock, enabled, inventory, etc.) however, the item/s show as unavailable/out of stock on the front-end. All we have to do is open the Config product in Admin (do nothing) and save it. Then the item displays in stock on the front end. Any ideas?

        Cheers!
        Dano

        • Jerome says:

          Hi Dano,

          I am having the same problem. Products are correct in admin, but not visible in the front.

          Have you figured out what the solution is?

          • Chron-O-John says:

            Hi,

            just wanted to say that I’m investigating this right now and I’m quite positive that I’ll find a solution quite soon. By comparing the Database before and after saving in the Admin Panel, and trying out every change one by one, I found out, that the DB-Field for the configurabe Product needs to be added in catalog_product_index_price and the Stock status for the configurable also needs to be created (cataloginventory_stock_status) although I don’t manage stocks.

            Now I just need to automate this somehow – maybe by updating the Plugin…

  15. Jim says:

    Johann,

    Amazing tutorial!
    I have one issue though.

    I have a configurable product that has 2 configurable attributes available. When I create a product using the Admin I choose one of both.

    Now, when I create a product with the API, It’s as if both attributes were selected.

    Is there any way to specify on which attribute my configurable product is based?

  16. Stefano says:

    Wonderful… thank you for you work!!!
    (the extension works well with Magento ver. 1.7.0.2)

  17. Anu says:

    I have to create a grouped using magentoApi. And also I need to associate simple products to this grouped product. Using ‘associated_skus’ => array(‘SKU-001′, ‘SKU-002′) i am able to create grouped product with associated simple product.

    Instead of this sku is it possible to use custom product attribute? for example i have an product attribute “Itemcode”. Is it possible to use this attribute to add simple product…
    please guide me

    Regards
    Anu

  18. Jon says:

    Any thoughts on adding the “associated_skus” attribute to the catalogProductInfo API, so we can query which SKUs are associated for a configurable product?

  19. Renan says:

    Hello Johann, im Renan from Brazil. You are doing an amazing thing sharing your knowledge with us. Thank you very much!!!

    I did install using github, but inside visual studio when i update the service reference the catalogProductCreateEntity doesn’t have the associated_skus property inside the webservice definition(http://myurl/magento/index.php/api/v2_soap?wsdl=1), so it isn’t possible for my code to run. Can you help me find the solution?

    another doubt is about the cache you say. Is it that cache inside magento? or other cache? Can you please specify what cache is?

    Thank you very much!

    Renan

  20. Dimitris says:

    Hello, this gone work in 1.7 also?
    has anyone test it?

  21. Muhammad Raza says:

    Thanks Alot Sir…
    its really helping for me….
    i was struggling from last week to do this….by using your code i have done it……… God Bless you…. Stay blessed

  22. Dimitris says:

    thns Muhammad.
    i am facing a strange issue in my case.. i can add the configurable product, but cannot add assos=ciated products.

    Even i am in magento admin, and editing the configurable product, i canot search and add form already simple products.
    BUT if i create a new one, then its working

    • Michael says:

      Hi Johann, hi Dimitris,

      I tried to use your code. I can create an simple product with the right attributes, but I can’t associate the simple product to the configurable product. Maybe you can help me.

      I try to use this code:

      $productData = array(
      ‘associated_skus’ => array(’1000102001′), // Simple products to associate
      ‘price_changes’ => array(
      array(
      ‘usb_memory’ => array(
      56 => ’0′
      ),
      ‘usb_colour’ => array(
      63 => ’0′
      ),
      ),
      ),
      );
      // Creation of configurable product
      $result = $proxy->catalogProductUpdate($sessionId, 4120, $productData);

      I get the response “true”.
      But in the configurable product I see no associated product and can’t search for it.
      4120 is the product id for the configurable product.

      Thanks and regards,
      Michael

  23. Daver says:

    Hi Johann,
    I am using your free extension for adding configurable products through SOAP and it is mostly working well. Thank you for writing this!

    I am having a problem with setting prices for different colors. There are no errors when I run the SOAP script, but when I look at the configurable product afterwards, the price remains the same when selecting different colors. Perhaps I am not setting it correctly in the script. I am using the version from Nov 6, 2012 on enterprise 1.12.

    Magento is configured to use ‘color’ as the only attribute matching simple products to configurable.

    My script looks something like this:

    $associated_skus = array();
    $price_changes = array();
    $price_changes['color'] = array();
    $usual_price = ’10.00′;
    $product1 = array(….) // name, price, color, etc..
    $product2 = array(….) // name, price, color, etc..
    $simple_product_list = array($product1, $product2);

    foreach($simple_product_list as $product) {

    $productData = array(
    ‘name’ => $product['name'],
    ….
    ‘price’ => $product['price'],
    ‘color’ => $product['color']
    )
    $proxy->call($sessionId, ‘product.create’, array(‘simple’, ‘Default’, $product['sku'], $productData));
    array_push($associated_skus,$product['sku']);
    $price_changes['color'][$product['color']] = (float)$product['price'] – (float)$usual_price;

    }

    $productData = array(
    ‘name’ => ‘Configurable product’,
    ….
    ‘price’ => $usual_price,
    ‘associated_skus’ => $associated_skus,
    ‘price_changes’ => $price_changes
    )
    $proxy->call($sessionId, ‘product.create’, array(‘configurable’, ‘Default’, ‘SKU-TEST’, $productData));

    This approach successfully adds the simple and configurable products, associates them, and the color selection works correctly. But the prices of the different colors remain the same when I select different colors.

    The admin panel for the configurable product, in the section “Super products attribute configuration”, color is shown as the selectable attribute but there are no price differences listed.

    Am I setting the ‘price_changes’ attribute correctly?

    • Daver says:

      I found the answer to my own question. It appears that in the line:

      $price_changes['color'][$product['color']] = (float)$product['price'] – (float)$usual_price;

      $product['color'] has to be the language-dependent label color (for example “Red/Black”) and NOT the magento ID for the color (for example “243″).

      This is now working in one store in one language. But if I create a store in a different language and want to use translated words for the colors, it seems that would break this??

      It would be better if I could use the Magento ID numbers for the $product['color']. That way, the same products and price changes will work with different languages. Is my understanding correct?

      So I have a feature request: Could you add an option to use the Magento ID numbers for the color instead of the color label/name?

  24. Piernitas says:

    Hello,

    I must say, that is a great module and definitely a life-saver. How Magento willingly ignores such a basic requirement is beyond me. So thanks are in order.

    Your module seems to work exactly as advertised but I’m curious about how it handles multi-select attributes. I have, so far not been successful in specifying multi-select options when creating a product. I’ve tried associating an array to the attribute key and I’ve even tried comma-separated values but it’s all been without success.

    ex (using V1):

    ‘activities’ => array(‘activity1′,’activity2′);
    OR
    ‘activities’ => ‘activity1,activity2′;

    I am, of course, using the option labels and not the IDs because I can’t retrieve the option IDs through the standard API (another Magento miracle) and since your module seems to add the ability to use labels for options, I am assuming that the same goes for multi-select options. But I can’t make it work and I haven’t seen an example of this.

    I am looking through the code as we speak to try to figure this out but any help would be appreciated and would probably benefit others as I’d hate to think that I am the only one who needs to create configurable products having multi-select attributes through the API

    Cheers,

    • Piernitas says:

      Ok, I had a look at the code and I noticed that the getOptionKeyByLabel function in Helper/Catalog/product.php used by api.php doesn’t handle arrays, only values. So if anyone is interested, I modified it on my copy and tested it. I can confirm that it works. Here is the modified function:

      public function getOptionKeyByLabel($attributeCode, $label)
      {
      $attribute = Mage::getModel(‘catalog/product’)->getResource()
      ->getAttribute($attributeCode);
      if ($attribute && $attribute->getId() && $attribute->usesSource()) {
      if(is_array($label)){ // Added by Piernitas
      $options = array(); // Added by Piernitas
      foreach ($attribute->getSource()->getAllOptions(true, true) as $option) { // Added by Piernitas
      if (in_array($option['label'],$label)) { // Added by Piernitas
      $options[] = $option['value']; // Added by Piernitas
      } // Added by Piernitas
      } // Added by Piernitas
      return $options; // Added by Piernitas
      }else{ // Added by Piernitas
      foreach ($attribute->getSource()->getAllOptions(true, true) as $option) {
      if ($label == $option['label']) {
      return $option['value'];
      }
      }
      } // Added by Piernitas
      }

      return $label;
      }

  25. Vic85 says:

    Hi,

    How can i manually download this mod? It says “here aren’t any uploads for this repository”? Please let me know.

  26. Denzel says:

    I have got this working thank you for sharing it with us. A question If I may…

    I have created 3 extra attributes Colour,Size & Finish.

    When I create the simple products and the configurable product I have entered a value for each attribute – e.g.

    Product 1 -> Blue -> Large -> Gloss

    etc, etc

    This works fine the configurable product will show front end and I can make my choices via the drop downs.

    My issue is now, not all configurable products I have will have a finish. They will for example have a colour & size: e.g.

    Product 2 Red -> Large

    The simple products will not become accosiated with the configurable product unless I specifiy a “finish” for the simple products. Am I going wrong somewhere? Can someone point me in the right direction to fix this?

    Thanks

  27. Trung says:

    Hi,
    Can we associate the image of exist simple product to configurable product with this API ?

    Thanks

  28. Bruno says:

    Hello.
    That did not work for me.
    I use version 1.7.0.2 of Magento. So I copied the folder “app” extension to the root of magento, put the windows to merge folders. Updated the cache (but it was already disabled).
    Then I copied the example “API V1″, I set my three parameters (address of the wsdl file, user api, api password) and then immediately put it to run. Not accused any errors, BUT … simple products were not associated with the configurable product!
    I entered the configurable product edition and did not list any items associated with it.

    What can it be? How to know the version of my API? How to know if the extension is working properly?

    Thank you,
    Bruno Videira.

  29. Andrew Heron says:

    My friend, you have done a wonderful thing here! 2 minutes to install, 30 seconds to modify my script, seems to work perfectly. You have saved me a LOT of time.

  30. Hager Aly says:

    thanks for your great effort. i’m new to magento and i want to use magento improve api with magento GO store

Leave a Reply

* Required fields

Categories