Magento CE 1.8.1.0 – Auto Reindex Product Flat Data and Refresh Cache

cachece-1.8.1.0cronreindex

When I edit something on a product, Magento always ask, reindex 'Product Flat Data' and refresh cache 'Blocks HTML output'

I manually process those whenever I edit but it's very annoying when I have to edit my product often. I give up to those two task since I have to do it later again

Is there any way automatically reindex 'Product Flat Data' and refresh cache 'Blocks HTML output' every few hours? like using cronjob (then how to setup?)

Best Answer

Magento has a class that specifically handles command line scripts. The Magento bootstrap can be handled via the Shell Abstract script inside of your shell/ folder.

<?php

include_once 'abstract.php';

class JohnG_Shell_Cron extends Mage_Shell_Abstract {

    public function run() {
        $this->_clearindex();
        $this->_clearCache();
    }

    protected function _clearindex() {
        $process = Mage::getModel('index/indexer')->getProcessByCode('catalog_product_flat');
        $process->reindexAll();
    }

    protected function _clearCache() {
        Mage::app()->getCacheInstance()->clean('block_html');
    }
}

Now setup your cron like this:

0 0-23/2 * * * php -f /path/to/your/magento/shell/johngscript.php