I have a Magento multi store setup and have 2 csv files that will be uploaded on a daily bases to update price, qty, and other attributes.
I would like to create two of the following files that run on cron and each file will do the same thing for each store individually.
Currently the code below takes takes store1.csv and applies it to both stores also if i run this code with store3.csv it will apply all price and updates to both stores.
Would like for store1.csv to only update store 1 and store3.csv only update attributes on store 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);
}
?>