Created
April 22, 2016 18:47
-
-
Save nflint/fbfb628fa3c8fe2497f14799924be797 to your computer and use it in GitHub Desktop.
Create CSV with url
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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_id'].'='.$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); | |
//See more at: https://www.ayasoftware.com/preselected-associated-simple-product-based-configurable-options#sthash.t5sOsquW.dpuf | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment