I assume your vendor name is Imt, and your module name is OrderProductReport.
Step 1: Create system.xml to add config in the system configuration:
- File path: app/code/Imt/OrderProductReport/etc/admin/system.xml
- Content:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="imt" translate="label" sortOrder="900">
<label>IMT</label>
</tab>
<section id="imt" translate="label" sortOrder="10" showInDefault="1" showInWebsite="1"
showInStore="1">
<class>separator-top</class>
<label>Order Product Report</label>
<tab>imt</tab>
<resource>Imt_OrderProductReport::config</resource>
<group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1"
showInStore="1">
<label>General</label>
<field id="frequency" translate="label" type="select" sortOrder="1" showInDefault="1">
<label>Frequency</label>
<source_model>Magento\Cron\Model\Config\Source\Frequency</source_model>
<backend_model>Imt\OrderProductReport\Model\Config\Backend\OrderProductReportCron</backend_model>
</field>
<field id="time" translate="label" sortOrder="2" type="time" showInDefault="1">
<label>Start Time</label>
</field>
</group>
</section>
</system>
</config>
Step 2: Create backend model class for Cron config.
- File path: app/code/Imt/OrderProductReport/Model/Config/Backend/OrderProductReportCron.php
- File content:
<?php
namespace Imt\OrderProductReport\Model\Config\Backend;
use Magento\Cron\Model\Config\Backend\Product\Alert;
use Magento\Framework\Exception\LocalizedException;
class OrderProductReportCron extends \Magento\Framework\App\Config\Value
{
const CRON_STRING_PATH = 'crontab/default/jobs/order_product_report/schedule/cron_expr';
const CRON_MODEL_PATH = 'crontab/default/jobs/order_product_report/run/model';
/**
* @var \Magento\Framework\App\Config\ValueFactory
*/
protected $configValueFactory;
/**
* @var string
*/
protected $runModelPath = '';
/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
* @param \Magento\Framework\App\Config\ValueFactory $configValueFactory
* @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param string $runModelPath
* @param array $data
*/
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\App\Config\ScopeConfigInterface $config,
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
\Magento\Framework\App\Config\ValueFactory $configValueFactory,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
$runModelPath = '',
array $data = []
) {
$this->runModelPath = $runModelPath;
$this->configValueFactory = $configValueFactory;
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
}
/**
* @inheritdoc
*
* @return $this
* @throws LocalizedException
*/
public function afterSave()
{
$time = $this->getData('groups/general/fields/time/value') ?:
explode(
',',
$this->_config->getValue(
'imt/general/time',
$this->getScope(),
$this->getScopeId()
) ?: '0,0,0'
);
$frequency = $this->getValue();
$cronExprArray = [
(int)($time[1] ?? 0), //Minute
(int)($time[0] ?? 0), //Hour
$frequency == \Magento\Cron\Model\Config\Source\Frequency::CRON_MONTHLY ? '1' : '*', //Day of the Month
'*', //Month of the Year
$frequency == \Magento\Cron\Model\Config\Source\Frequency::CRON_WEEKLY ? '1' : '*', //Day of the Week
];
$cronExprString = join(' ', $cronExprArray);
try {
$this->configValueFactory->create()->load(
self::CRON_STRING_PATH,
'path'
)->setValue(
$cronExprString
)->setPath(
self::CRON_STRING_PATH
)->save();
$this->configValueFactory->create()->load(
self::CRON_MODEL_PATH,
'path'
)->setValue(
$this->runModelPath
)->setPath(
self::CRON_MODEL_PATH
)->save();
} catch (\Exception $e) {
throw new LocalizedException(__('We can\'t save the cron expression.'));
}
return parent::afterSave();
}
}
Step 3: Set config path for crontab.xml.
- File path: app/code/Imt/OrderProductReport/etc/crontab.xml
- Content:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
<group id="order_product_report">
<job name="order_product_report" instance="Imt\OrderProductReport\Cron\OrderReport" method="execute">
<config_path>crontab/default/jobs/order_product_report/schedule/cron_expr</config_path>
</job>
</group>
</config>
My code follows how Magento core has done in the Magento_ProductAlert module.
You can read more about system.xml reference from Magento document: https://experienceleague.adobe.com/docs/commerce-operations/configuration-guide/files/config-reference-systemxml.html