Magento 2 : Create cron with config schedule

Posted . Visible to the public.

Step 1:

Create system.xml file with configuration field.

<group id="cron" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1"
                   showInStore="1">
                <label>MSD Cron</label>
                <field id="schedule" translate="Schedule" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
                    <label>Schedule</label>
                </field>
            </group>

Step 2 :

Create crontab.xml content

<group id="default">
        <job name="msd_invoice_cron" instance="Vasan\BusIntegration\Cron\Conversion" method="execute">
            <config_path>integration/cron/schedule</config_path>
        </job>
    </group>

Step 3:

Create config file

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <integration>
            <cron>
                <schedule>* * * * *</schedule>
            </cron>
        </integration>
    </default>
</config>

Step 4:

Create Cron class

<?php
/**
 * MsdOrderConversion
 *
 * @copyright Copyright © 2023 Vasan. All rights reserved.
 * @author    psureshvasan@vasan.com
 */

namespace Vasan\BusIntegration\Cron;

use Vasan\Bidding\Model\Milestones;
use Vasan\BusIntegration\Model\LoggerService;
use Vasan\BusIntegration\Model\Service;
use Vasan\Bidding\Model\ResourceModel\Milestones\CollectionFactory as MilestoneCollectionFactory;

class Conversion
{
    /**
     * @var LoggerService
     */
    protected $logger;

    /**
     * @var Service
     */
    protected $service;

    /**
     * @var MilestoneCollectionFactory
     */
    protected $milestoneCollectionFactory;

    /**
     * @param Service $service
     * @param LoggerService $logger
     * @param MilestoneCollectionFactory $milestoneCollectionFactory
     */
    public function __construct(
        Service $service,
        LoggerService $logger,
        MilestoneCollectionFactory $milestoneCollectionFactory
    ) {
        $this->service = $service;
        $this->logger = $logger;
        $this->milestoneCollectionFactory = $milestoneCollectionFactory;
    }

    /**
     * Execute cron action.
     *
     * @return void
     */
    public function execute()
    {
        $collection = $this->milestoneCollectionFactory->create();
        $collection->addFieldToFilter(
            ['msd_order_number', 'msd_order_number'],
            [
                ['neq' => ""],
                ['notnull' => true]
            ]
        );
        $collection->addFieldToFilter(
            ['msd_invoice_number', 'msd_invoice_number'],
            [
                ['eq' => ""],
                ['null' => true]
            ]
        );
        $collection->addFieldToFilter('payment_status', ['eq' => Milestones::PAYMENT_STATUS_READY_TO_PAY]);
        $collection->load();
        foreach ($collection as $milestone) {
            $this->logger->log('The cron is executed and id is ' . $milestone->getId());
        }
        $this->logger->log('The cron is executed');
    }
}

vasan
Last edit
vasan
Keywords
Magento, 2, Conditional, Collection, Cron
Posted by vasan to vasan's deck (2023-05-25 03:00)