Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

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

  1. The scheduler of the server opens the address index.php?route=cron/cron once an hour.
  2. The controller cron/cron goes through the list of jobs and picks the ones whose time has come.
  3. 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');
}
ArgumentMeaning
FirstA unique code of the job
SecondDescription for the list
ThirdCycle: hour, day, month
FourthRoute of the task controller
FifthWhether 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/null

Every 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.

Thanks for reading!

Donate to the site — we work hard for you.