1

i want to create configuration from admin for this cron tab how we can do that?

<?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="abc\abc\Cron\OrderReport" method="execute">
            <schedule>* * * * *</schedule>
        </job>
    </group>
</config>
imt
  • 161
  • 6

3 Answers3

1

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-&gt;runModelPath = $runModelPath;
    $this-&gt;configValueFactory = $configValueFactory;
    parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
}

/**
 * @inheritdoc
 *
 * @return $this
 * @throws LocalizedException
 */
public function afterSave()
{
    $time = $this-&gt;getData('groups/general/fields/time/value') ?:
        explode(
            ',',
            $this-&gt;_config-&gt;getValue(
                'imt/general/time',
                $this-&gt;getScope(),
                $this-&gt;getScopeId()
            ) ?: '0,0,0'
        );
    $frequency = $this-&gt;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-&gt;configValueFactory-&gt;create()-&gt;load(
            self::CRON_STRING_PATH,
            'path'
        )-&gt;setValue(
            $cronExprString
        )-&gt;setPath(
            self::CRON_STRING_PATH
        )-&gt;save();
        $this-&gt;configValueFactory-&gt;create()-&gt;load(
            self::CRON_MODEL_PATH,
            'path'
        )-&gt;setValue(
            $this-&gt;runModelPath
        )-&gt;setPath(
            self::CRON_MODEL_PATH
        )-&gt;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

Tu Van
  • 6,868
  • 2
  • 11
  • 22
0

In crontab.xml file you need to mention reference path of your store config field

<?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="default">
        <job name="order_product_report" instance="abc\abc\Cron\OrderReport" method="execute">
            <config_path>sectionName/groupname/fieldname</config_path>
        </job>
    </group>
</config>
Prashant Valanda
  • 12,659
  • 5
  • 42
  • 69
  • <config_path>sectionName/groupname/fieldname</config_path> how its come i mean system can you provide more code – imt Oct 17 '22 at 12:20
0

In your abc/abc/etc/system.xml you should add a field for the expression configuration:

<field id="cron_exp" translate="label comment" sortOrder="0" type="text" showInDefault="1" showInWebsite="1" showInStore="1">
    <label>Cron Expression</label>
</field>

Above should go inside a section and a group (assuming you already have those for other configurations). In your crontab.xml you should point to that new configuration path:

    <job name="order_product_report" instance="abc\abc\Cron\OrderReport" method="execute">
        <config_path>sectionName/groupname/cron_exp</config_path>
    </job>