系统学习magento二次开发,推荐小册:《Magento中文全栈二次开发 》
本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。
在Magento 2中通过配置设置cron时间的步骤:
步骤1:首先您需要在 以下路径中添加crontab.xml
app\code\Vendor\Extension\etc\crontab.xml
<?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 instance="Vendor\Extension\Cron\Cronfile" method="execute" name="my_cron_job"> <config_path>crontab/default/jobs/my_cron_job/schedule/cron_expr</ </job> </group> </config>
步骤2: 需要在系统上添加新组.xml路径如下
app\code\Vendor\Extension\etc\adminhtml\system.xml
<group id="configurable_cron" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0"> <label>Cron Settings</label> <field id="frequency" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0"> <label>Frequency</label> <source_model>Magento\Cron\Model\Config\Source\Frequency</source_model> <backend_model>Vendor\Extension\Model\Config\Cronconfig</backend_model> </field> <field id="time" translate="label comment" sortOrder="2" type="time" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Start Time</label> </field> </group>
第 3 步:现在在 以下路径中添加 Cronconfig.php
app\code\Vendor\Extension\Model\Config\Cronconfig.php
<?php namespace Vendor\Extension\Model\Config; class Cronconfig extends \Magento\Framework\App\Config\Value { const CRON_STRING_PATH = 'crontab/default/jobs/my_cron_job/schedule/cron_expr'; const CRON_MODEL_PATH = 'crontab/default/jobs/my_cron_job/run/model'; /** * @var \Magento\Framework\App\Config\ValueFactory */ protected $_configValueFactory; /** * @var mixed|string */ protected $_runModelPath = ''; /** * CronConfig1 constructor. * @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|null $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); } /** * @return CronConfig1 * @throws \Exception */ public function afterSave() { $time = $this->getData('groups/configurable_cron/fields/time/value'); $frequency = $this->getData('groups/configurable_cron/fields/frequency/value'); $cronExprArray = [ intval($time[1]), intval($time[0]), $frequency == \Magento\Cron\Model\Config\Source\Frequency::CRON_MONTHLY ? '1' : '*', '*', $frequency == \Magento\Cron\Model\Config\Source\Frequency::CRON_WEEKLY ? '1' : '*', ]; $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 \Exception(__('Some Thing Want Wrong , We can\'t save the cron expression.')); } return parent::afterSave(); } }
步骤4:现在在 以下路径中添加Cronfile.php
app\code\Vendor\Extension\Cron\Cronfile.php
<?php namespace Vendor\Extension\Cron; class Cronfile { public function execute() { //Add your cron code. } }
结论:
因此,现在所有人都可以通过Magento 2中的配置设置Cron Time。