The code below generates an unique URL for each configurable child.
sku,url
simple_product_sku,http://www.example.com/configurable.html?color=7&size=6
Having unique URL for each associated simple product allows you to bid (on Google Adwords, Bings Ads, Shopzilla, ....) on simple products as well as configurable products, while having the people land on the configurable page with pre-selected configurable options - contact us if you need this feature.
How to use it?
- Create a file called generateUniqueUrls.php with the following content and upload it to Magento root
- Go to http://www.example.com/generateUniqueUrls.php
- File will be generated in magento_root/var/export/urls.csv
<?php $mageFilename = 'app/Mage.php'; require_once $mageFilename; Mage::app(); $OutFile = Mage::getBaseDir ().DS."var/export/urls" .".csv"; $manufacturer_id = $argv[1]; if ( file_exists( $OutFile ) ){ unlink( $OutFile ); } $handle = fopen($OutFile, 'w+') or die("can't open file ".$OutFile); $heading = array('sku','url'); $header = implode(",", $heading)."\r\n"; fwrite($handle, $header); $products = Mage::getModel('catalog/product')->getCollection(); $products->addAttributeToFilter('status', 1); //enabled $products->addAttributeToFilter('visibility', 4); //catalog, search $products->addAttributeToFilter('type_id', 'configurable'); if(isset($_GET['manufacturer_id']) || $manufacturer_id) { $products->addAttributeToFilter( 'manufacturer', array( 'eq' => $_GET['manufacturer_id'] ) ); } $products->addAttributeToSelect('*'); $prodIds = $products->getAllIds(); $product = Mage::getModel('catalog/product'); $attributes_code = array(); $attributes_values = array(); foreach ($prodIds as $productId) { $product_data = array(); $product = Mage::getModel('catalog/product'); $product->load($productId); $productURL = $product->getProductUrl(); $_attributes = $product->getTypeInstance()->getConfigurableAttributesAsArray(); $associated_prods = $product->getTypeInstance()->getUsedProducts(); foreach ($associated_prods as $assoc_product) { if ($assoc_product->isSaleable()) { $assoc_products[] = $assoc_product; $str = array(); foreach($_attributes as $_attribute){ foreach ($_attribute['values'] as $value){ $childValue = $assoc_product->getData($_attribute['attribute_code']); if ($value['value_index'] == $childValue){ $str [] = $_attribute['attribute_code'].'='.$value['value_index']; } } } $product_data['sku'] = $assoc_product->getSku(); $product_data['url'] = $productURL."?".implode("&", $str); $row = implode(",", $product_data)."\r\n"; fwrite($handle, $row); } } } fclose($handle);