我的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" } }
答案 0 :(得分:0)
我猜您的CSV文件中有错误,请使用您喜欢的电子表格编辑器打开它,然后选择分隔符和附件。如果表看起来不错那么只需打开ist
Varien_File_Csv
班级
protected function _parseCSV($file)
{
$csv = new Varien_File_Csv();
$csv->setDelimiter(',');
$csv->setEnclosure('"');
$data = $csv->getData($file);
//$this->_headLine = array_shift($data);
return $data;
}
你用$ csv [] = array_combine($ header,$ row)做什么是将一个新数组推送到$ csv,这可能也会带来一些麻烦。我建议你为每次迭代使用Mage :: log()并检查数据的外观。这是一个简单的问题,但你有代码......
答案 1 :(得分:0)
大多数情况下你的逻辑是正确的,除了这部分
//this should be assigned to a separate variable first
$csv[] = array_combine($header, $row);
//because in second iteration, the data you require is in $csv[1]
//this will essentially fetch only the first productcode always for all rows.
$productcode = $csv[0]['Product Code'];
以下是经过更正的片段
foreach ($rows as $row) {
// assign it to a var
$csv_data = array_combine($header, $row);
$productcode = $csv_data['Product Code'];
$newprice = $csv_data['New Price'];
$specialprice = $csv_data['Special Price'];
$status = $csv_data['Status'];
// stuff it into an array later
$csv[] = $csv_data;
var_dump($productcode);