HummingbirdUK main logo


Coding solutions to business problems

About us

We use code to create solutions to business challenges, bottle-necks and headaches.

If you think your business has a problem that can be solved through code, we are happy to chat things through without any obligation.

Get in touch

Automatically link simple/configurable products - HummingbirdUK

Home / Blog / Automatically link simple/configurable products - HummingbirdUK

Written by Giles Bennett

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!


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] <?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); ?> [/php] The first three lines are self-evident enough if you've done any programming in Magento. The following lines are where the fun begins : [php] $file_handle = fopen("skus.csv", "r"); while (!feof($file_handle) ) { $line_of_text = fgetcsv($file_handle, 1024); [/php] Let's open the skus.csv file (or whatever you've called it) and then loop through its lines. [php] $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); [/php] 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. [php] $simpleId = $simpleProduct->getId(); [/php] 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. [php] $ids = $configurableProduct->getTypeInstance()->getUsedProductIds(); $newids = array(); foreach ( $ids as $id ) { $newids[$id] = 1; } $newids[$simpleId] = 1; [/php] 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 : [php] echo "Updating configurable product " . $configurableSku; echo "<br>"; [/php] And then do the actual key part - save the configurable product with the products in the $newids array as its associated simple products. [php] Mage::getResourceModel('catalog/product_type_configurable')->saveProducts($configurableProduct, array_keys($newids)); [/php] 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.
Author : Giles Bennett

About the author

Giles Bennett built his first website in 1996, and is old enough to miss Netscape Navigator. Initially a lawyer, he jumped ship to IT in 2008, and after 5 years as a freelancer, he founded HummingbirdUK in 2013. He can be reached by email at