Creating cron script 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.

14
Top

Enjoyed this post?

Subscribe to our RSS Feed, Follow us on Twitter and spread it to your friends!

Author

Domagoj Potkoc

Senior ZEND Developer

Domagoj Potkoc is part of "boiler room" together with Ivan Weiler, Hrvoje and Vanja. His special expertise are payment gateways.

Other posts from this author

Discussion 14 Comments

Add Comment
  1. can you provide me complete module for this

  2. Next

    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?

  3. “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….

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

  5. codep

    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?

  6. tom

    I really want the functioin ! That ten day after I send the products to customer , then system send a email to customer !? any idea?

  7. key
  8. Pristaldus

    Don’t forget to add the model in the config.xml file’s global section!

    With using the package ang module name of the example:

    <config>
      <!-- ... -->
      <models>
        <birthday>
          <class>Inchoo_Birthday_Model</class>
        </birthday>
      </models>
    </config>
    

    It will eliminate the ‘invalid callback’ error mentioned above by Codep and cron will work.

  9. Pristaldus

    Sorry, my first comment was errorous…

    <config>
      <!-- ... --->
      <global>
        <models>
          <birthday>
             <class>Inchoo_Birthday_Model</class>
          </birthday>
        </models>
      </global>
      <!-- ... -->
    </config>
    

    It is OK. The global section is important :) )))

  10. Ivn

    Hi there,

    I need to create a cron job in Magento which should send emails to customers whose order status is complete. I need to schedule this cron job such that it checks for order with status = “complete” and send them emails. Also the customers which are already sent an email should be marked so that they are not sent email twice.

    I have created a custom module and in its config.xml I have added my template email and cron job scheduled.

    Also in the Module>>Model>>Observer.php I have declared my sendEmail() trying to send a test email to my email id.

    I try to run this cron task by running http://yourdomain.com/cron.php. But I fail to get any mail.

    Any suggestions please?

  11. Gz Chauhan

    Hi..
    i create cron module and it work fine but problem is that when i set

    0 */12 * * *

    in config.xml it not send mail every 12 hours.. can u say this is right expresion or not.. can u give me right expresion for every 12 hours cron set

  12. Ivn

    @Gz Chauhan

    I think if you need to run your cron every 12 hours, may be this expression should work.

    * 0,12 * * *

    Try it out!

  13. kob

    HI.
    I try to send an email template via cron.
    I know the cron function works b’coz I can send emails hard coded like : mail(“mymail@example.com”,’bla’,'bla’);
    and I know that the code I use for sending the email template works b’coz when I put the code in a .phtml file and run it through the browser it sends the email.
    It is only when I try to use the template inside the cron function that it is not working.
    any ideas what can I do?

  14. Muzammil

    is there any way we can run cron by direct run url in browser.

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top