Magento – Save all products through php/shell script

magento-1.9PHPshellsimple-productssh

I'm in the process of cleaning up my existing magento install by moving the DB and using a new install so I can do away with all the unnecessary extensions taking up space.

Everything seems to have been setup fine, however some product attributes are not showing up in the admin, but doing a save brings the values back. However I have over 7000 products and cant save them individually. All the products are simple products.

So I've used this script from this article.

<?php
require 'app/Mage.php';
Mage::app();
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
foreach ($products as $product) {
    $product->load();
    echo 'Product: ' . $product->getId() . ' - ' . $product->getName() . "\xA";

    $product->setShortDescription('N/A');
    $product->save();
}
?>

But I need to run this script via the browser and if the internet cuts out, I have to start again going over the ones that were already saved.

  1. Instead, how can I convert this to a shell script and run if via SSH?

  2. Also I have one store that doesn't have the same values as the default (all store views) how/where do I specify that the script run for only that store?

Thanks in advance.

Best Answer

Working shell script here: run it from shell directory php productupdate.php --stores <id,...>

shell/productupdate.php

<?php
require_once 'abstract.php';

class Your_Shell_Productupdate extends Mage_Shell_Abstract
{
    protected $_stores = array();

    public function __construct() {
        parent::__construct();
        set_time_limit(0);

        if ($this->getArg('stores')) {
            $this->_stores = array_merge(
                $this->_stores,
                array_map(
                    'trim',
                    explode(',', $this->getArg('stores'))
                )
            );
        } else {
            $this->_stores = array(Mage_Core_Model_App::ADMIN_STORE_ID);
        }
    }

    public function run()
    {
        try {
            printf(
                'Selected stores: %s'."\n",
                '"'.implode('", "', $this->_stores).'"'
            );

            foreach ($this->_stores as $storeId) {
                $products = Mage::getModel('catalog/product')->getCollection();
                Mage::getSingleton('core/resource_iterator')->walk(
                    $products->getSelect(),
                    array(array($this, '_saveShortDescription')),
                    array('store_id' => $storeId)
                );

                printf(
                    'Done processing.'."\n"
                        .'Total processed products count for store %d: %d'."\n",
                    $storeId, $products->getSize()
                );
            }
        } catch (Exception $e) {
            echo $e->getMessage().'@'.time();
        }
    }

    public function _saveShortDescription($args)
    {
        Mage::getSingleton('catalog/product_action')->updateAttributes(
            array($args['row']['entity_id']),
            array('short_description' => 'N/A'),
            $args['store_id']
        );
    }

    public function usageHelp()
    {
        return <<<USAGE
Usage:  php -f productupdate.php

  --stores <ids>       Process only these stores (comma-separated)

  help                 This help

USAGE;
    }
}

$shell = new Your_Shell_Productupdate();
$shell->run();
Related Topic