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:

0 1 * * *
birthday/observer::sendBirthayEmail

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.

You made it all the way down here so you must have enjoyed this post! You may also like:

Magento 2 logging Matej Maricic
, | 11

Magento 2 logging

Automatically cancel pending orders older than 90 minutes Domagoj Potkoc
Domagoj Potkoc, | 45

Automatically cancel pending orders older than 90 minutes

27 comments

  1. My cron job for changing customer attribute not working.
    .Same code works when I copy that in a controller action and hit by url.
    .On AOE_Scheduler extension, it shows processing and never change from that.
    .I also put this code in try catch but no exception generated.

    $user = Mage::getModel(‘customer/customer’)->load($customer->getEntityId());
    $firstname = ‘Saeed’; $lastname = ‘Khan’;
    $user->setFirstname($firstname);
    $user->setLastname ($lastname);
    $user->save();

    Any suggestion will be appreciated.

  2. I have CentOS on my server. I have written your code properly but still my cron job is not running.

  3. hi,
    I have same problem as KOB.
    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?
    Please do help

  4. I used your tutorial and my cron job is working fine.
    But i have one query related to cron job. Can i create a cron job for controller method if yes how?

  5. Hello sir ,

    How can i execute custom URL of magento files by cron job through cron job setting with magento admin panel .

  6. There’s a typo in model code –
    … public function sendBirthayEmail() …
    missed “d” in “Birthay” that’s maybe a reason of exceptions

  7. Dear Magento Developers,

    I’ve created a module with cron job working and I give it free to those that ask me through email from my blog (when the new blog will be online you can download it from there).

    Now you’ve the chance to test it with no issues.

    Thanks
    Regards

  8. Dear Domagoj Potkoc,

    Thanks for the tip but your post is not complete.
    By saying “I hope that you know how to create magento module” you miss very important information.

    For example, my module doesn’t work because probably i miss to declare the

    <events>

    in the config.xml.

    Is the purpose of this post to contact you for premium services only?

    You help is appreciated.
    Thanks
    Regards

  9. 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?

  10. @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!

  11. 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. 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?

  13. 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 :))))

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

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

  16. 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?

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

  18. Hi, I posted a comment on Toni Anicic’s blog about cron.
    https://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 Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.