Magento Update Script

To Update massive amounts of data in magento without going directly into the database I found an interesting piece of code under http://www.mydons.com/writing-custom-magento-shell-script/:

<?php
require_once 'abstract.php';

/**
 * Mydons Inventoryupdate Shell Script
 *
 * @author     Mydons
 */
class Mydons_Shell_Inventory extends Mage_Shell_Abstract {

    /**
     * Run script
     *
     */
    public function run() {

        Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
        $file_handle = fopen("Inventory.csv", "r");

        while (!feof($file_handle)) {
            $line_of_text = fgetcsv($file_handle, 1024);
            $productSku = $line_of_text[0];
            $qty = $line_of_text[1]; 
            $product = Mage::getModel('catalog/product')
                 ->loadByAttribute('sku', $productSku);
            if ($Product) {
                $stockItem = Mage::getModel('cataloginventory/stock_item')
       ->loadByProduct($product);
                // Set Product Stock Data Here
                $stockItem->setData('is_in_stock', 1);
                $stockItem->setData('qty', $qty);
                $stockItem->save();
                echo "Updated Inventory for product " . $productSku . "\n";
            } else {
                echo "Product " . $productSku . " Doesnt Exist \n";
            }
        }
        fclose($file_handle);
    }

    /**
     * Retrieve Usage Help Message
     *
     */
    public function usageHelp() {
        return <<<USAGE
Usage:  php -f inventory.php -- [options]
        php -f inventory.php 


USAGE;
    }

}

$shell = new Mydons_Shell_Inventory();
$shell->run();

I adopted this file to simply print the sku of all products in the shop and count them:

<?php
require_once 'abstract.php';

/**
 * SkuPrinter
 *
 * @author manuel
 */
class Update extends Mage_Shell_Abstract {

    /**
     * Run script
     */
    public function run() {
        Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
        $collection = Mage::getResourceModel('catalog/product_collection');
        $i=0;
        foreach ($collection as $product) {
                echo $product->getSku() . "\n";
                $i++;
        }
        print "#products: $i\n";
    }

    /**
     * Retrieve Usage Help Message
     */
    public function usageHelp() {
        return <<<USAGE
Usage:  php -f update.php -- [options]
        php -f update.php
USAGE;
    }

}

$shell = new Update();
$shell->run();

These two examples show how to iterate over all products and how to get a specific product by sku. They have to be placed in ${magento_root}/shell

To speed up updates a bit magento provides the ability to update single fields instead of the whole product as in code snippet one:

$product->setAttributeCode($newValue)
$product->getResource()->saveAttribute($product, 'attribute_Code');

Of course the fastest way is to write SQL queries. Therefor please read into the magento EAV model.

Leave a Comment