Creating cron script in Magento

5 Comments 13th JAN 2010 | Posted by Domagoj Potkoc in Magento

Creating cron script in Magento

Creating Magento cron script is very simple thing. First of all we have to create the module, (I hope that you know how to create magento module) and add in config.xml file next code:

<crontab>
        <jobs>
            <inchoo_birthday_send>
                <schedule><cron_expr>0 1 * * *</cron_expr></schedule>
                <run><model>birthday/observer::sendBirthayEmail</model></run>
            </inchoo_birthday_send>
        </jobs>
</crontab>

Next step, we have to create model file observer.php (in folder model) with method sendBirthayEmail

class Inchoo_Birthday_Model_Observer
{

    public function sendBirthayEmail()
    {
        //this collection get all users which have birthday on today
     	$customer = Mage::getModel("customer/customer")->getCollection();
    	$customer->addFieldToFilter('dob', array('like' => '%'.date("m").'-'.date("d").' 00:00:00'));
    	$customer->addNameToSelect();
    	$items = $customer->getItems();

    	foreach($items as $item)
    	{
        // send email or do something
    	}

        return $this;
    }

}

This cron will be executed every day at 01:00 AM. The important thing: You have to set cron execution on the server (www.yourstore.com/cron.php), without it will not work.

If you like what you read, please share it.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Yahoo! Bookmarks
  • Reddit
  • Technorati
  • Twitter
  • StumbleUpon
  • LinkedIn
  • Netvibes
  • NewsVine
  • Sphinn
  • Tumblr
  • Posterous

To post code in comments, place your code inside [code] and [/code] tags.

There are 5 comments (Add Yours +)

  • can you provide me complete module for this

  • Hi, I posted a comment on Toni Anicic’s blog about cron.
    http://inchoo.net/ecommerce/magento/magento-newsletter/

    He’s been very kind and tried to help, but maybe you are the right person for this topic.
    We are experiencing strange behavior with cron-based.
    After the two comments in Toni’s blog, we re-queued the newsletter, launched cron via wget and are observing the mail server’s behaviour.
    Current batch size ($countOfSubscritions) is 20.
    Right after the cron run, the mail server received 105 e-mails for delivery.
    How’s it that 20 becomes 105 with a single cron run?

  • “This cron will be executed every day at 01:00 AM. The important thing: You have to set cron execution on the server (www.yourstore.com/cron.php), without it will not work.”

    What is role of crontab command on linux(CentOS 5.3)
    and What is role of “cron.php” ?
    and What is role of magento’s cron script ?

    Have any relevant above three kind of task ?? please….

  • Great tip, just another question? How allow the store’s administrator to choose when execute the script?

  • I did EXACTLY what you’ve suggested but it doesn’t work – in cron_schedule table in magento’s base I’m getting following error: ‘exception ‘Mage_Core_Exception’ with message ‘Invalid callback: birthday/observer::sendBirthayEmail does not exist’….’. do you have any idea why?

Leave a Comment

Please wrap all source codes with [code][/code] tags.
Magento Design and Development | Magento SEO | iPhone Application Development Web Application Development with ZEND | WordPress Ecommerce | WordPress development
Sitemap

Inchoo - webappsolutions | 2009