Creating cron script in Magento

2 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
  • FriendFeed
  • NewsVine
  • Sphinn
  • Tumblr

There are 2 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?

Leave a Comment

Magento Design and Development | Magento SEO | iPhone Application Development Web Application Development with ZEND | WordPress Ecommerce | WordPress development
Sitemap

Inchoo - webappsolutions | 2009