我有一个包含多行的 CSV。我试图让我的代码遍历每一行,但它似乎在第一行之后停止。
该脚本正在更新多个产品的 Magento 价格,但它们在“_”之前具有相同的 SKU 起始编号,您可以看到这是代码。
我认为这是它可能会卡住的地方,因为阵列不平坦或 foreach 卡住了,我不确定。
$rows = array_map('str_getcsv', file('price/price-update.csv'));
$header = array_shift($rows);
//var_dump($rows);
$csv = array();
foreach ($rows as $row) {
$csv[] = array_combine($header, $row);
$productcode = $csv[0]['Product Code'];
$newprice = $csv[0]['New Price'];
$specialprice = $csv[0]['Special Price'];
$status = $csv[0]['Status'];
var_dump($productcode);
for ( $i = 1; $i < 15; $i++ ) {
$magentoSku = $productcode."_0".$i;
var_dump($magentoSku);
// get magento product based on sku above
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$magentoSku);
//var_dump($product);
if ( $product->getSku() == $magentoSku ) {
//sku match, update price
$product->setPrice($newprice);
$product->setSpecialPrice($specialprice);
if ($status == "A") {
$product->setStatus(1);
} else {
$product->setStatus(0);
}
$product->save();
//var_dump($product);
} else {
echo "No Match";
// sku doesnt match, don't update anything, and continue looping
}
}
}
$rows 的输出:
array(2) { [0]=> array(15) { [0]=> string(7) "1234150" [1]=> string(9) "HEADBOARD" [2]=> string(8) "WING 6'0" [3]=> string(0) "" [4]=> string(6) "459.99" [5]=> string(7) "1121115" [6]=> string(6) "279.99" [7]=> string(7) "1130610" [8]=> string(1) "0" [9]=> string(1) "0" [10]=> string(1) "A" [11]=> string(1) "0" [12]=> string(1) "Y" [13]=> string(1) "C" [14]=> string(18) "Available to Order" } [1]=> array(15) { [0]=> string(7) "1234149" [1]=> string(9) "HEADBOARD" [2]=> string(8) "WING 6'0" [3]=> string(0) "" [4]=> string(6) "459.99" [5]=> string(7) "1121115" [6]=> string(6) "279.99" [7]=> string(7) "1130610" [8]=> string(1) "0" [9]=> string(1) "0" [10]=> string(1) "D" [11]=> string(1) "0" [12]=> string(1) "Y" [13]=> string(1) "C" [14]=> string(18) "Available to Order" } }