1

无法反转输出,尝试使用 array_reverse 和 usort。

我正在尝试使用 magmi 数据泵将产品从 XML 导入 Magento,工作正常,但我需要以相反的顺序输出,以便 Magento 将简单产品与可配置产品链接起来,

有任何想法吗?

$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

我的输出是:

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] => )

但我需要这个:

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 ) 
4

1 回答 1

0

不确定Magmi Datapump如何根据您的示例链接简单和可配置的产品,但假设您的问题所需要的只是在简单产品之后导入可配置产品,您可以执行以下操作:

  1. 在将它们从 XML 中提取出来后,通过更改$newProductData = array(...)为创建一个中间产品记录数组$newProductData[] = array(...)

  2. 现在使用这样的东西按产品类型对中间数组进行排序:

    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. 最后,遍历排序后的数组并完成导入:

    foreach ($newProductData as $data) {
        $dp->ingest($data);
    }
    
于 2015-04-03T22:55:52.063 回答