我有一个脚本,它将根据匹配SKU更新Magento中的库存数量。只要CSV中的标题与他们正在更新的Magento中的字段相匹配,这就可以正常工作。
SKU的数量
ABC123,20
ABC124,20
然而,我的源CSV标题被命名为“产品ID”和“数量”,例如
产品ID,数量
ABC123,20
ABC124,20
如何重新映射数组键名,以便导入有效?
require_once '../app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$count = 0;
$file = fopen('/home/public_html/mysite/csvs/my.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) {
if ($count == 0) {
foreach ($line as $key=>$value) {
$cols[$value] = $key;
}
}
$count++;
if ($count == 1) continue;
#Convert the lines to cols
if ($count > 0) {
foreach($cols as $col=>$value) {
unset(${$col});
${$col} = $line[$value];
echo ${$col};
}
}
//print_r($cols); die();
// Check if SKU exists
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
if ( $product ) {
$productId = $product->getId();
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
$stockItemId = $stockItem->getId();
$stock = array();
if (!$stockItemId) {
$stockItem->setData('product_id', $product->getId());
$stockItem->setData('stock_id', 1);
} else {
$stock = $stockItem->getData();
}
foreach($cols as $col=>$value) {
$stock[$col] = $line[$value];
}
foreach($stock as $field => $value) {
$stockItem->setData($field, $value?$value:0);
}
$stockItem->save();
unset($stockItem);
unset($product);
}
echo "<br />Stock updated $sku";
}
fclose($file);
答案 0 :(得分:0)
您可以尝试下面有效的文章:
http://www.blog.magepsycho.com/updating-product-qty-in-magento-in-an-easier-faster-way/
它简单地基于原始的sql查询,甚至对数百万个sku的数量更新都非常快。
由于