1

こんにちは、magento のマルチ ストア セットアップがあり、価格、数量、およびその他の属性を更新するために毎日アップロードされる 2 つの csv ファイルがあります。

cron で実行される次の 2 つのファイルを作成したいと思います。各ファイルは、各ストアに対して個別に同じことを行います。

現在、以下のコードは 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);
    }
?>
4

1 に答える 1