1

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);
    }
?>
1
  • David, did you solved your problem? if yes can you post the code, I have the same problem now, thanks
    – Alberto
    Commented Jul 19, 2016 at 14:42

1 Answer 1

2

The store ID should be set before loading the product.

$product = Mage::getModel('catalog/product')->setStoreId(3)->loadByAttribute('sku', $sku);

Afterwards it doesn't have any influence on the loaded data.

Also make sure price is set to website level under System > Configuration > Catalog > Price.

As far as stock quantity goes, that can only be set on global level. Per SKU there is only one stock level

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.