Created
April 2, 2013 06:51
-
-
Save nhp/5290385 to your computer and use it in GitHub Desktop.
Script for usage of avs_fastsimpleimport and configurable products
This file contains 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 | |
/** | |
* Created by JetBrains PhpStorm. | |
* User: npreuss | |
* Date: 3/25/13 | |
* Time: 12:53 PM | |
* To change this template use File | Settings | File Templates. | |
*/ | |
require_once 'app/Mage.php'; | |
umask( 0 ); | |
ini_set('memory_limit', '256M'); | |
$app = Mage::app(); | |
error_reporting(E_ALL | E_STRICT); | |
//$files = glob('/home/npreuss/projects/fo/htdocs/xml/import/artikeldaten/20130324-200402.517000-ShopDatenExport.json'); | |
$files = glob('/home/npreuss/projects/fo/htdocs/xml/import/artikeldaten/*.json'); | |
$time = microtime(); | |
$time = explode(' ', $time); | |
$time = $time[1] + $time[0]; | |
$begintime = $time; | |
$i = 0; | |
/* | |
$import = Mage::getModel('fastsimpleimport/import'); | |
$json = json_decode(file_get_contents('/home/npreuss/projects/fo/htdocs/xml/import/kategorien/20130324-200235.985000-Kategorien.json'), true); | |
$data = array(); | |
//Zend_Debug::dump($json); | |
foreach ($json as $category) { | |
// Zend_Debug::dump(str_replace(' > ', '/', $category['category_path'])); | |
$data[str_replace(' > ', '/', $category['category_path'])] = array( | |
'_root' => 'Default Category', | |
'_category' => trim(str_replace(' > ', '/', str_replace('/', '&' ,$category['category_path']))), | |
// 'description' => $category['description'], | |
'description' => $category['code'], | |
'name' => $category['name'], | |
'available_sort_by' => 'position', | |
'default_sort_by' => 'position', | |
'include_in_menu' => 'yes', | |
'is_active' => 'yes', | |
'position' => $category['position'], | |
); | |
} | |
$data = array_values($data); | |
try { | |
// Zend_Debug::dump($data); | |
$import->processCategoryImport($data); | |
} catch (Exception $e) { | |
print_r($import->getErrorMessages()); | |
} | |
unset($data); | |
unset($import); | |
unset($json); | |
*/ | |
$data = array(); | |
foreach ($files as $file) { | |
++$i; | |
if ($i>100) { | |
} | |
$json = json_decode(file_get_contents($file), true); | |
// $data = array(); | |
foreach ($json as $product) { | |
switch ($product['type']) { | |
case 5: | |
case 6: | |
case 7: | |
$type = 'configurable'; | |
break; | |
case 1: | |
case 2: | |
case 3: | |
case 4: | |
default: | |
$type = 'simple'; | |
} | |
if (!isset($product['description']) || empty($product['description'])) { | |
if ($type == 'simple') { | |
Mage::log($product['sku'], null, 'fsi.log'); | |
} | |
$product['description'] = 'n.a.'; | |
} | |
$data[$product['sku']] = array( | |
'sku' => $product['sku'], | |
'_type' => $type, | |
'_attribute_set' => 'Default', | |
'_product_websites' => 'base', | |
'name' => $product['name'], | |
'price' => $product['VkPreis'], | |
'description' => $product['description'], | |
'short_description' => $product['short_desc'], | |
'weight' => $product['gewicht'], | |
'status' => $product['active'] ? 1 : 2, | |
'visibility' => 4, | |
'tax_class_id' => 2, | |
'qty' => 23, | |
'color' => $product['attribute1'], | |
'size' => $product['attribute2'], | |
'ean' => $product['ean'], | |
); | |
if ($type == 'configurable' && isset($product['parent_sku']) && !empty($product['parent_sku'])) { | |
$i = 0; | |
foreach ($product['parent_sku'] as $linked) { | |
if ($i == 0) { | |
$data[$i.$linked] = $data[$product['sku']]; | |
} | |
$data[$i.$linked]['_super_products_sku'] = $product['parent_sku'][$i]; | |
if (count(array_unique($product['linked_attributes'])) > $i) { | |
$data[$i.$linked]['_super_attribute_code'] = $product['linked_attributes'][$i]; | |
} | |
/* | |
if (isset($product['linked_attributes'][$i])) { | |
$data[$i.$linked]['_super_attribute_code'] = $product['linked_attributes'][$i]; | |
} | |
*/ | |
++$i; | |
} | |
unset($data[$product['sku']]); | |
/* | |
$data[$product['sku']]['_super_products_sku'] = $product['parent_sku'][0]; | |
*/ | |
} | |
/* | |
if ($type == 'simple' && isset($product['parent_sku']) && !empty($product['parent_sku'])) { | |
if (isset($product['attribute1']) && !empty($product['attribute1'])) { | |
$data[$product['parent_sku']]['_super_attribute_code'] = 'color'; | |
} | |
if (isset($product['attribute2']) && !empty($product['attribute2'])) { | |
$data[$product['parent_sku']]['_super_attribute_code'] = 'size'; | |
} | |
} | |
*/ | |
} | |
// Zend_Debug::dump($data); | |
// Mage::log($file.'::'.count($data), null, 'fsi.log'); | |
// Mage::log(memory_get_peak_usage(), null, 'fsi.log'); | |
/* | |
unset($data); | |
unset($product); | |
unset($json); | |
unset($importer); | |
*/ | |
} | |
//Zend_Debug::dump($data); | |
$importData = array(); | |
//Zend_Debug::dump($data); | |
foreach ($data as $_item) { | |
if ($_item['_type'] == 'simple') { | |
array_unshift($importData, $_item); | |
} else { | |
array_push($importData, $_item); | |
} | |
} | |
$importer = Mage::getModel('fastsimpleimport/import') | |
->setContinueAfterErrors(true) | |
->setDropdownAttributes(array('color', 'size')); | |
// ->setPartialIndexing(true); | |
$importer->processProductImport($importData); | |
try { | |
// Zend_Debug::dump($data); | |
} catch (Exception $e) { | |
print_r($importer->getErrorMessages()); | |
} | |
echo count($data)."\n"; | |
$time = microtime(); | |
$time = explode(" ", $time); | |
$time = $time[1] + $time[0]; | |
$endtime = $time; | |
$totaltime = ($endtime - $begintime); | |
echo $totaltime; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment