12/11/2012

How to create custom cron job in magento

Magento default have a cron.php file located in the root directory. To set a cron in magento you have to create your own module first, and in the config.xml you have to declare the method which will fired and the time. Below is a dump of a etc/config.xml file of a custom cron module

<?xml version="1.0"?>
<config>
<modules>
<Wl_Cronset>
<version>0.1.0</version>
</Wl_Cronset>
</modules>
<crontab>
<jobs>
<Wl_Cronset>
<schedule>
<cron_expr>01 00 * * *</cron_expr>
</schedule>
<run>
<model>cronset/expired::productExpired</model>
</run>
</Wl_Cronset>
</jobs>
</crontab>
<global>
<models>
<cronset>
<class>Wl_Cronset_Model</class>
</cronset>
</models>
</global>
</config>

As per mentioned in this xml your cron will be fired every day @ night 00:01 min

Now you have to write your own functionality in your module Model file, Here my model file name is Expired and the function which will trigger is productExpired().

<?php
class Wl_Cronset_Model_Expired extends Mage_Core_Model_Abstract
{
public function productExpired()
{
// Your code goes here
}
}
?>

Now login to your server to call the magento root cron.php and set to be fired in every minute.

1 comment: