docs/developer-guide/cron-jobs.md
This guide provides detailed instructions for developers on setting up and managing cron jobs in OpenCart. Cron jobs are essential for automating recurring tasks such as refreshing currency rates, cache clearing, sitemap generation, sending newsletters, etc.
OpenCart integrates a cron handler that can centralize all reccuring tasks to allow to easily manage all from the admin instead of having to manage them from the host panel, which can be convenient for the end-user.
First of all it's necessary to set up your host to call the OpenCart cron handler, this is necessary only once, then all will be handled directly from OpenCart, follow this procedure to set up the cron, here based on cPanel but the same can be done in all host panels.
Extension > Cron menuExtension > Cron (e.g.: wget "https://mywebsite.tld/index.php?route=cron/cron" --read-timeout=5400)That's all, now all the cron tasks set into Extension > Cron will run automatically.
Step 1: Create the Cron Class
Create a PHP class for your extension. For example, place the following code in extension/test_module/catalog/controller/cron.php:
<?php
namespace Opencart\Catalog\Controller\Extension\TestModule;
class Cron extends \Opencart\System\Engine\Controller {
public function index(): void {
// Process your customizations here
$this->log->write('My cron job has successfully started!');
}
}
Step 2: Register the Startup
Startups are typically registered in the install() method of your extension. For programmatic registration, use the Startup model.
Add the following code to your module’s controller:
<?php
namespace Opencart\Admin\Controller\Extension\TestModule\Module;
class TestModule extends \Opencart\System\Engine\Controller {
public function install(): void {
// Load the cron model
$this->load->model('setting/cron');
// Register the cron job
$this->model_setting_cron->addCron([
'code' => 'my_cron', // Cron code (unique identifier)
'description' => 'My custom cron',
'cycle' => 'day', // cycle (hour, day, week)
'action' => 'extension/test_module/cron', // action
'status' => true
]);
}
public function uninstall(): void {
// Remove the cron job on uninstall
$this->load->model('setting/cron');
$this->model_setting_cron->deleteCronByCode('my_cron');
}
Step 3: Test the Cron Job
Extensions > Installer).Extensions > Extensions).Extension > Cron Jobs, you should see your new entry▶️ Run Cron JobSystem > Maintenance > Error logs) to verify that your custom log message appears.