您好,我有一个 magento 多商店设置,并且有 2 个 csv 文件,这些文件将每天上传以更新价格、数量和其他属性。
我想创建以下两个在 cron 上运行的文件,每个文件将为每个商店单独执行相同的操作。
目前,下面的代码采用 store1.csv 并将其应用于两个商店,如果我使用 store3.csv 运行此代码,它将对两个商店应用所有价格和更新。
希望 store1.csv 仅更新存储 1,而 store3.csv 仅更新存储 3 上的属性。
<?php
/*Move to our working directory
$home = getenv("HOME");
if (! $home) {
chdir('../'); // We hope we are somewhere where this works
} else {
chdir($home.'/project/html');
}*/
$csv = "store3.csv";
if (sizeof($argv) > 1) {
$csv = $argv[1];
}
//Turn On Error Reporting
error_reporting(E_ALL | E_STRICT);
//BOOTSTRAP MAGENTO
$mageFilename = '../app/Mage.php';
require_once $mageFilename;
//require_once 'relatedProducts.php';
Mage::app()->setCurrentStore(Mage::getModel('core/store')->setStoreId(3));
Mage::setIsDeveloperMode(true);
umask(0);
//OPEN CSV
if (($handle = fopen($csv, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 2000, "\t")) !== FALSE) {
$num = count($data);
if ($num < 1) continue; // skip blank lines
$sku = trim($data[0]);
if ($num < 2) {
echo "Skipping: ".$sku." not enough fields\n";
continue;
}
$qty = trim($data[1]);
$price = trim($data[2]);
// grab the product based on sku.
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku)->setStoreId(3);
if(!$product) {
print "Error: Invalid SKU, ".$sku."\n";
continue;
}
if ($product->getPrice() != $price) {
$product->setPrice($price);
$product->save();
}
// Grab the inventory(stock) model in order to update quantities.
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId())->setStoreId(3);
if ($stockItem->getData('qty') != $qty) {
$stockItem->setData('qty', $qty);
if ($qty > 0) {
$stockItem->setData('is_in_stock', 1);
}
$stockItem->save();
}
}
fclose($handle);
}
?>