Create CRON Script In Magento Module

Introduction

Setting CRON in magento is easy

(Optional step) Firstly, install AOEscheduller https://github.com/AOEpeople/Aoe_Scheduler

This will help you to see whether your configured CRON works or not.

Add this code to your Magento module etc directory config.xml.

<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>

In next step create observer.php in Model’s directory.

<?php
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;
    }
 
 
}

Conclusion

Do not forget that you need set CRON for cron.php which takes care of reindexing, etc.

 

credit: Inchoo


Lukas
Lukas Stranovsky
I am the guy who made this. Sorry for typos.