8 August, 2022

How to Create Job Scheduler Using Cron in Magento?

One day a client told me, “My e-commerce website has gained excellent popularity and hits a decent visitor count from the targeted audience.” But still he was not so happy with his website and continued, “My website loses many visitors in the checkout page and I would like to trace them and send a reminder mail about their shopping, which may generate more sales.” I have come across many clients who experienced same kind of problem with their e-commerce websites. Clients’ expectations from a developer point of view would be to send a reminder mail to the users who were skipped away from the website during product checkouts. But is it possible? If so, what is the solution? The solution is CRON. In this article I would like to explain the uses of “cron” and how to use it in your e-commerce project. Let’s jump to action!

A cron is a job scheduler that allows you to specify actions which should run at specific intervals. Magento allows you to schedule custom tasks in an XML configuration, in a similar manner to the UNIX crontab style. Here is an example from app/code/core/local/modulename/etc/config.xml:

<config>
...
  <crontab>
   <jobs>
    <modulename_observer>
     <schedule>
       <cron_expr>*/5 * * * *</cron_expr>
     </schedule>
     <run>
      <model>modulename/observer::SendMailtocustomer</model>
     </run>
    </modulename_observer>
   </jobs>
...
  </crontab>
...
</config>

The above job will be run for every 5 minutes.

Read: How to Create Structural Block in Magento

We can create multiple jobs between cron tabs. Next step is to create a model file observer.php. Here we can write the method which contains the functionality. Here is the example: app/code/local/modulename/Model/Observer.php.

class Namespace_Modulename_Model_Observer {

public function SendMailtocustomer() { 

      <!-- Here is the place where you have to add remainder functionality --> 

          /* @var $mailTemplate Mage_Core_Model_Email_Template *
          $mailTemplate = Mage::getModel('core/email_template');
          /* Send Transactional Email */
          $mailTemplate->sendTransactional(
                $templateId,
                $sender,
                $emailto,
                $name,
                $data
          );
}
}

This method will be called whenever a cron runs.

Note: You have to set cron execution on the server (www.yourstore.com/cron.php), without this it will not work. Follow the below steps to set up a cron job.

1. Go to cpanel and find cron job.

2. Configure the interval at which you wish to run the cron job. You can use Common settings or you can set individual time settings (minute, hour, day, month, weekdays)

3. In the Command field, type the command you wish the system to run. If you do not wish to receive an email notifying you of a specific cron job, add the following line to the command:>/dev/null 2>&1

4. Click Add New Cron Job.

Here you can create multiple cron jobs with various time intervals.

I hope this article was informative. You can apply this technique for various circumstance like jobportal, checkout process websites etc.

One Comment

Leave a Reply

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