Created
March 20, 2013 05:39
-
-
Save oliyoung/5202566 to your computer and use it in GitHub Desktop.
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 | |
function o($str) { echo "[".date('H:i.s'). "]\t".$str."\n"; } | |
error_reporting(E_STRICT); | |
$client = new SoapClient('http://magento.dev/api/soap/?wsdl'); | |
// o("Client: {$client}"); | |
$session = $client->login('ezpublish', 'ezpublish'); | |
o("Session: {$session}"); | |
$erp_products = array(); | |
// Cycle through ERP retrieved products | |
foreach (file($argv[1]) as $line_num => $line) { | |
if ($line_num > 1) { | |
$data = preg_split("/\s{3,}/", $line); # split line on multiple spaces | |
$erp_products[] = array( | |
'type_id' => 'simple', | |
'attribute_set_id' => 4, | |
'sku' => $data[0], | |
'meta_keyword' => $data[2], | |
'name' => $data[3], | |
'price' => trim($data[4]), | |
'description' => $data[3], | |
'short_description' => $data[3], | |
'weight' => 0, | |
'status' => 1, | |
'visibility' => 1, | |
'tax_class_id' => 2, | |
'group' => $data[1], | |
); | |
} | |
} | |
$groups = array(); | |
$result = $client->call($session, 'catalog_category.tree'); | |
foreach($result['children'][0]['children'] as $group) { | |
$groups[$group['name']] = $group['category_id']; | |
} | |
// Find existing products | |
$magento_products = array(); | |
$results = $client->call($session, 'catalog_product.list'); | |
foreach($results as $result) { | |
$magento_products[$result['sku']] = $result['product_id']; | |
o("Found {$result['sku']} locally with Product ID {$result['product_id']}"); | |
} | |
$attributeSets = $client->call($session, 'product_attribute_set.list'); | |
$attributeSet = current($attributeSets); | |
// Cycle through retrieved products | |
foreach($erp_products as $erp_product) { | |
if(in_array($erp_product['sku'], array_keys($magento_products))) { | |
o("Updating {$erp_product['sku']}…"); | |
$result = $client->call($session, 'catalog_product.update', array($erp_product['sku'], $erp_product) ); | |
if($result) { o("… done"); } | |
} else { | |
o("Adding {$erp_product['sku']}…"); | |
$result = $client->call($session, 'catalog_product.create', array('simple', $attributeSet['set_id'], $erp_product['sku'], $erp_product)); | |
if($result) { o("… done"); } | |
} | |
// $result = $client->call($session, 'catalog_category.updateProduct', array('categoryId' => $groups[$erp_product['group']], 'product' => $erp_product['sku'], 'position' => '1', 'productIdentifierType' => 'sku')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment