1

Cannot reverse output, tried with array_reverse and usort.

I'm trying to import products from an XML to Magento with magmi datapump, works fine but I need the output in reverse order for Magento to link simple products with configurable products,

Any ideas?

$xml = simplexml_load_file("23.xml") or die("Error: Cannot create object");

foreach($xml->wapiitems->record as $book) {

$item = $book->fields->itemno;
if (strlen($item) <= 6) {
$type = "configurable";
$ca = "color,size";}
else {
$type = "simple";
$ca = "";}

$newProductData = array(
        'sku'          => (string)$book->fields->itemno,        // name
        'type'           => (string)$type,        // sku
        'color' => (string)$book->subtables->descriptions->record->fields->variant1name,    // special price        
        'size'         => (string)$book->subtables->descriptions->record->fields->variant3name,     // price
        'attribute_set' => 'Default',            // attribute_set
        'store'         => 'admin',            
        'name'   => (string)$book->subtables->descriptions->record->fields->description,        // full description
        'configurable_attributes' => (string)$ca    // short description

);

//$dp->ingest($newProductData);
echo "</br>";
print_r ($newProductData);
$newProductData=null;    //clear memory
unset($newProductData); //clear memory
}
unset($xml);

$dp->endImportSession();   // end import

My output is:

Array ( [sku] => 90349 [type] => configurable [color] => [size] => [attribute_set] => Default [store] => admin [name] => [configurable_attributes] => color,size ) 
Array ( [sku] => 903490101004 [type] => simple [color] => Red [size] => 4 [attribute_set] => Default [store] => admin [name] => Q-Irine Cover [configurable_attributes] => ) 
Array ( [sku] => 903490101005 [type] => simple [color] => Black [size] => 5 [attribute_set] => Default [store] => admin [name] => Q-Irine Cover [configurable_attributes] => ) 
Array ( [sku] => 903490101006 [type] => simple [color] => Black [size] => 6 [attribute_set] => Default [store] => admin [name] => Q-Irine Cover [configurable_attributes] => )

But I need this:

Array ( [sku] => 903490101006 [type] => simple [color] => Black [size] => 6 [attribute_set] => Default [store] => admin [name] => Q-Irine Cover [configurable_attributes] => )
Array ( [sku] => 903490101005 [type] => simple [color] => Black [size] => 5 [attribute_set] => Default [store] => admin [name] => Q-Irine Cover [configurable_attributes] => ) 
Array ( [sku] => 903490101004 [type] => simple [color] => Red [size] => 4 [attribute_set] => Default [store] => admin [name] => Q-Irine Cover [configurable_attributes] => ) 
Array ( [sku] => 90349 [type] => configurable [color] => [size] => [attribute_set] => Default [store] => admin [name] => [configurable_attributes] => color,size ) 
frednoah
  • 13
  • 3

1 Answers1

0

Not sure how Magmi Datapump is linking the simple and configurable products based on your example, but assuming that all that is required for your problem is that configurable products get imported after the simple products, you could do something like this:

  1. Create an intermediate array of product records after pulling them out of XML by changing $newProductData = array(...) to $newProductData[] = array(...)

  2. Now use something like this to sort your intermediate array by product type:

    usort($newProductData, function($a, $b)
    {
        if ($a['type'] == 'configurable' && $b['type'] == 'simple') {
            return 1;
        } else if ($a['type'] == 'simple' && $b['type'] == 'configurable') {
            return -1;
        } else {
            return strnatcmp($a['sku'], $b['sku']);
        }
    });
    
  3. Finally, iterate over the sorted array and complete the import:

    foreach ($newProductData as $data) {
        $dp->ingest($data);
    }
    
fantasticrice
  • 1,631
  • 1
  • 21
  • 31