Cron jobs
A cron job is code that runs on a schedule rather than on a visitor's request: updating currency rates, clearing old records, sending a report. OpenCart 4 has its own list of such jobs. An extension adds a job to the list, and the system scheduler of the server wakes the shop up at regular intervals.
How it works
- The scheduler of the server opens the address
index.php?route=cron/crononce an hour. - The controller
cron/crongoes through the list of jobs and picks the ones whose time has come. - For each of them it calls the controller written in the job.
The list is shown in the admin panel: Extensions → Cron Jobs. Each job has a cycle — hour, day or month.
The task controller
The task is a storefront controller of the extension. Create it by hand:
// extension/acme_hello/catalog/controller/cron/cleanup.php
namespace Opencart\Catalog\Controller\Extension\AcmeHello\Cron;
class Cleanup extends \Opencart\System\Engine\Controller
{
public function index(int $cron_id, string $code, string $cycle, string $date_added, string $date_modified): void
{
$this->db->query(
"DELETE FROM `" . DB_PREFIX . "acme_callback`
WHERE `date_added` < DATE_SUB(NOW(), INTERVAL 30 DAY)"
);
$this->log->write('Acme Hello: removed ' . $this->db->countAffected() . ' old callback requests');
}
}The task deletes callback requests older than 30 days and writes the number of removed rows to the log. The arguments are the fields of the job; the core passes them on every call.
Registration
The job is added in install() of the admin controller and removed in uninstall():
// extension/acme_hello/admin/controller/module/acme_hello.php
public function install(): void
{
$this->load->model('setting/cron');
$this->model_setting_cron->addCron('acme_hello_cleanup', 'Removes old callback requests', 'day', 'extension/acme_hello/cron/cleanup', true);
}
public function uninstall(): void
{
$this->load->model('setting/cron');
$this->model_setting_cron->deleteCronByCode('acme_hello_cleanup');
}| Argument | Meaning |
|---|---|
| First | A unique code of the job |
| Second | Description for the list |
| Third | Cycle: hour, day, month |
| Fourth | Route of the task controller |
| Fifth | Whether the job is enabled |
The way arguments are passed to addCron() differs between releases — check the method in admin/model/setting/cron.php of your copy. Reinstall the module — the job appears in Extensions → Cron Jobs.
The system scheduler
On the server add a line to crontab — the schedule table of Linux:
0 * * * * curl -s "https://shop.example.com/index.php?route=cron/cron" > /dev/nullEvery hour the server opens the address, and the shop runs the jobs that are due.
Checking
Open http://shop.test/index.php?route=cron/cron in the browser by hand, then System → Maintenance → Error Logs — the log contains the line of the task. A daily job runs again only after a day; the time of the last run is shown in the list of jobs.
Common mistake
The job is registered, but nothing happens. The list in the admin panel does not run anything by itself: without the line in the system crontab nobody opens cron/cron. Add the line on the server and check after an hour that the date of the last run in the list has changed.