Magento is one of the most powerful ecommerce platforms. For example, an
option in the platform allows you to set a time scheduler to run
multiple activities/tasks automatically. Magento does this through Cron
Job, and it also helps in increasing the performance of the store.
Automating tasks in Magento involves caching, indexing, generating
sitemap, auto update currency rates, and much more.
So in this tutorial, I am going to teach you how to setup cron job in
Magento.
- Create and Activate Custom Module
- Setup Cron Job
Create and Activate Custom Module
Go to the Admin Panel of your store and disable the cache by navigating to System → Cache Management.Go to app/code/local and create directories as shown below:
Now create config.xml in app/code/local/Namespace/Modulename/etc and paste the following code in it:
<?xml version="1.0"?>
<config>
<modules>
<Namespace_Modulename>
<version>0.0.1</version>
</Namespace_Modulename>
</modules>
</config>
Now that the configuration of module is done, just activate it. Create Namespace_Modulename in app/etc/modules and paste the following code in it:
<?xml version="1.0"?>
<config>
<modules>
<Namespace_Modulename>
<active>true</active>
<codePool>local</codePool>
</Namespace_Modulename>
</modules>
</config>
Setup Cron Job
Go to app/code/local/Namespace/Modulename/etc and open the config.xml file. Simply update the code. The final code of config.xml will be:<?xml version="1.0"?>
<config>
<modules>
<Namespace_Modulename>
<version>0.0.1</version>
</Namespace_Modulename>
</modules>
<crontab>
<jobs>
<custom_cron_task>
<schedule>
<cron_expr>*/5 * * * *</cron_expr>
</schedule>
<run>
<model>cron/cron::customtask</model>
</run>
</custom_cron_task>
</jobs>
</crontab>
<global>
<models>
<cron>
<class>Namespace_Modulename_Model</class>
</cron>
</models>
</global>
</config>
In the code above, we have scheduled a cron job to run every five minutes. In <run>, I have defined a function customtask.
Now create Cron.php in app/code/local/Namespace/Modulename/Model and paste the following code in it:
<?php
class Namespace_Modulename_Model_Cron
{
public function customisedtask()
{
// send email
$mail = Mage::getModel('core/email')
->setToEmail('username@email.com')
->setBody('Body of the Automated Cron Email Goes Here')
->setSubject('Subject: Cron Task (every 2 minutes) '.date("Y-m-d H:i:s"))
->setFromEmail('test@example.com')
->setFromName('Your Store Name')
->setType('html');
$mail->send();
}
}
No comments:
Post a Comment