Automatically link simple/configurable products – HummingbirdUK

This was an interesting one the other day – we had the simple products created, and we had the configurable products created, but since they’d been created separately, they weren’t associated with each other. Could we come up with a script to do just that? Turns out…yes!

How?


The starting point is a CSV file with two columns – on the left the SKU of the simple product, and on the right the SKU of the configurable product with which it is to be associated. Don’t worry about putting in a header row – really not necessary.

That CSV then gets uploaded to the web root of the site called “skus.csv” (or whatever you want, really). The script which does the magic is nice and simple and looks like this (I’ll run through the salient points further down) :

<?php
require_once 'app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$file_handle = fopen("skus.csv", "r");
while (!feof($file_handle) ) {
	$line_of_text = fgetcsv($file_handle, 1024);
	$simpleSku = $line_of_text[0];
	$configurableSku = $line_of_text[1];
	$simpleProduct = Mage::getModel('catalog/product')->loadByAttribute('sku',$simpleSku);
	$configurableProduct = Mage::getModel('catalog/product')->loadByAttribute('sku',$configurableSku);
	$simpleId = $simpleProduct->getId();
	$ids = $configurableProduct->getTypeInstance()->getUsedProductIds();
	$newids = array();
	foreach ( $ids as $id ) {
		$newids[$id] = 1;
	}
	$newids[$simpleId] = 1;
	echo "Updating configurable product " . $configurableSku;
	echo "<br>";
	Mage::getResourceModel('catalog/product_type_configurable')->saveProducts($configurableProduct, array_keys($newids));
}
fclose($file_handle);
?>

The first three lines are self-evident enough if you’ve done any programming in Magento. The following lines are where the fun begins :

$file_handle = fopen("skus.csv", "r");
while (!feof($file_handle) ) {
	$line_of_text = fgetcsv($file_handle, 1024);

Let’s open the skus.csv file (or whatever you’ve called it) and then loop through its lines.

	$simpleSku = $line_of_text[0];
	$configurableSku = $line_of_text[1];
	$simpleProduct = Mage::getModel('catalog/product')->loadByAttribute('sku',$simpleSku);
	$configurableProduct = Mage::getModel('catalog/product')->loadByAttribute('sku',$configurableSku);

Let’s transfer the simple product SKU from the CSV into a variable, and likewise the configurable product SKU. Then let’s use that SKU to load both products into $simpleProduct and $configurableProduct respectively.

	$simpleId = $simpleProduct->getId();

For the simple product, all we need to get is its ID, so let’s squirrel that away in $simpleId.

For the configurable product, it’s a little more tricky. We need to ensure that we add the simple product to its associated products, not replace any existing ones. Since we’re going to be adding an array of simple products (even if that array only has one product in it) the way we do this is to get the existing simple products which are associated with the configurable product, and put them in an array, then add in the new simple product to that array.

	$ids = $configurableProduct->getTypeInstance()->getUsedProductIds();
	$newids = array();
	foreach ( $ids as $id ) {
		$newids[$id] = 1;
	}
	$newids[$simpleId] = 1;

So here we do just that. We load any existing associated products into $ids, then loop through that and transfer them into the $newids array, then add the $simpleId to the end of the $newids array.

Finally let’s have a little output so we know what’s going on :

	echo "Updating configurable product " . $configurableSku;
	echo "<br>";

And then do the actual key part – save the configurable product with the products in the $newids array as its associated simple products.

	Mage::getResourceModel('catalog/product_type_configurable')->saveProducts($configurableProduct, array_keys($newids));

And that’s pretty much all there is to it. It should run relatively quickly, but if in doubt then run it from the command line to avoid any timeouts. You can download the complete file below, and do shout if you’ve got any questions or suggestions for improvements.

29 comments

  • Kiruba

    I’m getting the below error after ran the script

    Fatal error: Call to a member function getId() on a non-object in /home/late/public_html/simple-configurable.php on line 12

    Please help me to solve the same.

    Thanks in advance.

    • Giles (author)

      Either the Simple or the Configurable product that you’re trying to load from the spreadsheet doesn’t exist.

      Kind regards,

      Giles

  • Dhawal

    Hi Giles,

    Thank for posting this I really needed something that does exactly this.

    Keep up to good work and thank you again.

    Kind Regards
    D

    • Giles (author)

      Dhawal,

      You’re very welcome.

      Kind regards,

      Giles

  • Frank

    Hi,
    Got an error
    Updating configurable product GO_SKU1
    Updating configurable product GO_SKU1
    Updating configurable product GO_SKU1

    Fatal error: Call to a member function getId() on a non-object in /home/thinkmob/public_html/simple-configurable.php on line 12

    • Giles (author)

      Check the spreadsheet that you’ve uploaded – find the product which comes after the three instances of GO_SKU1, as it looks like it’s trying to update a configurable or simple product which doesn’t exist.

  • Frank

    First, thanks for posting this code! So I’m trying to use this code on CE 1.9 and it does run, however it doesn’t actually do the associating that it should. Have any suggestions for me? I have a large catalog of products to update, and if I had this tool working, it would save us a ton of time.
    Thanks again!

    • Giles (author)

      Frank,

      There’s no reason why it shouldn’t run on 1.9. First thing to check – are your CSVs saved as Windows CSVs?

      Kind regards,

      Giles

      • Frank

        Giles,

        Not that I’m aware of… especially since I’m on a Mac. Should they be windows CSVs?

        Thanks!

        • Frank

          Giles,

          Here’s an update for you, turns out the CVS is fine. Here’s how I know…

          I decided to spit out the vars in the script and it seems that the following lines aren’t getting the values they need:
          $simpleProduct = Mage::getModel(‘catalog/product’)->loadByAttribute(‘sku’,$simpleSku);
          $configurableProduct = Mage::getModel(‘catalog/product’)->loadByAttribute(‘sku’,$configurableSku);

          Here’s the resulting vars list (where K00087-P is the “parent sku” and K00087-5000 is the “child’):
          simpleSku: K00087-5000
          configurableSku: K00087-P
          configurableProduct:
          simpleProduct:
          simpleId: 824
          Updating configurable product K00087-P

          Thus
          Mage::getResourceModel(‘catalog/product_type_configurable’)->saveProducts($configurableProduct, array_keys($newids));
          will fail since it doesn’t have the values it needs.

          Any ideas?

          Thanks!

          • Giles (author)

            Is there actually a configurable product with that SKU in the catalogue? Mage::getModel(‘catalog/product’)->loadByAttribute(‘sku’,$configurableSku); is pretty standard functionality loading the product by SKU.

            Giles

  • William

    Hello!

    Love the article. Am I able to use this for Grouped Products?

    • Giles (author)

      William,

      The principle is probably similar, but the script would need some tweaking.

      Giles

      • William

        Hi Giles,

        Would you happen to know what I would need to change? Not the best coder I’m afraid!

        Thanks.

        • Giles (author)

          William,

          Ping us an email and we’ll see what we can do to help out.

          Kind regards,

          Giles

  • Jiten Patel

    Hi Giles,

    Find this very helpful, thanks a lot for providing this Solution.

    I have a configurable product, which has around 70,000 associated simple products, I tried to implement your solution all in one go, but I think there might be timout issue.

    Can you suggest any solution?

    Regards,
    Jiten Patel.

    • Giles (author)

      Jiten,

      I would suggest breaking it into smaller chunks, running it over the command line, and setting the PHP max_execution_time as high as possible.

      Kind regards,

      Giles

  • Sumatra

    This is the BEST explaination I’ve never ever seen in all my life !

    • Giles (author)

      Sumatra,

      We’re pleased you like it!

      Giles

  • Richard

    Hi there. where do I put your (php) file, in the root of the site with the .csv file created and then run the .php from from the site URL ?

    Sorry if I sound a little new at this …

    Regards
    Richard

    • Giles (author)

      Richard,

      That’s fine, don’t worry! Yes, you’re right – you need to put both files (the CSV and the script file) in the web root of your site. In order to link into your Magento installation, the script file looks for a Magento file called Mage.php which is in the app folder. The first line of the code links to this file :

      require_once 'app/Mage.php';
      

      but it does so presuming that the script file itself is in the web root of the site. So if you upload it anywhere else, it won’t be able to find the Mage.php file, and will fall over gibbering.

      Good luck!

      Kind regards,

      Giles

  • pradeep

    Thanks. Its very useful code.
    But when associate products with configurable product, i want the option’s (size, color etc) price to be inserted. Can you suggest what should I do?
    Thanks in advance.

    • Giles (author)

      Pradeep,

      I’m not sure I follow – the simple products (in this instance) already existed, as did the configurable products, so all the attributes about them had been already set, it was just that they weren’t associated to each other. From what you’re saying, it sounds like you’re looking to create the simple products from scratch first?

      Kind regards,

      Giles

  • Jess

    Thank you so much for this. This product allowed me to add existing products that had stopped being associated with a configurable product, and having to delete the product and start from scratch.

    Thank you so much, every other solution I found suggested using a function but did not say how to run the function or where to add it to the code, thank you for making the instructions so simple.

    • Giles (author)

      Jess,

      You’re welcome – thanks for your comment.

      Giles

  • Daniel

    Hi Giles,

    I tried to associated a couple of simple products by using your example above.

    But when I load the list of associated products of the config product afterwards. They aren’t on the list.

    Any suggestions ?

    Regards
    Daniel

    • Giles (author)

      Daniel,

      Thanks for posting – have you reindexed and cleared the caches after running the script?

      Giles

  • aybou_ice

    Thanks, It help a lot.
    But how about the price of the associated products ? the script associate simple product with its configurable products without assigning its prices.
    Thank you.

    • Giles (author)

      The products will take their prices from a combination of the price of the configurable product, plus the price of the relevant associated product.

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to talk to us about your project?