We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

Let’s say I have this requirement to send the shipment email every 5 minutes or 10 minutes. To perform the task, I chose to create a custom cronjob and its file to achieve the functionality. Follow the below steps for more details.

Step 1: Create a 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 name="sendshipmentemail" instance="MagemonkeysShipmentemailsendCronSend" method="execute">
            <schedule>0,15,30,45 * * * *</schedule>
        </job>
    </group>
</config>

Step 2: Create a Send.php file. Please follow the below code

<?php

namespace MagemonkeysShipmentemailsendCron;

use MagentoFrameworkApiSearchFilterGroupBuilder;
use MagentoFrameworkApiSearchCriteriaBuilder;
use MagentoFrameworkAppObjectManager;
use MagentoSalesModelOrderShipmentRepository;
use MagentoSalesModelOrderRepository;
use MagentoFrameworkApiFilterBuilder;
use PsrLogLoggerInterface;

class Send
{

    private $orderRepository;
    private $searchCriteriaBuilder;

    public function __construct(

        OrderRepository $orderRepository,
        SearchCriteriaBuilder $searchCriteriaBuilder,
        ShipmentRepository $shipmentRepository,
        FilterBuilder $filterBuilder,
        FilterGroupBuilder $filterGroupBuilder,
        LoggerInterface $logger
    )
    {
        $this->orderRepository = $orderRepository;
        $this->shipmentRepository = $shipmentRepository;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->filterBuilder = $filterBuilder;
        $this->filterGroupBuilder = $filterGroupBuilder;
        $this->objectManager = ObjectManager::getInstance();
        $this->logger = $logger;
    }


    public function execute()
    {

        $filtergroup_date = $this->filterGroupBuilder
            ->addFilter(
                $this->filterBuilder
                    ->setField('created_at')
                    ->setConditionType('gt')
                    ->setValue('2020-01-01')
                    ->create()
            )
            ->create();


        $filtergroup_email_sent = $this->filterGroupBuilder
            ->addFilter(
                $this->filterBuilder
                    ->setField('email_sent')
                    ->setConditionType('null')
                    ->create()
            )
            ->create();

        $filtergroup_send_email = $this->filterGroupBuilder
            ->addFilter(
                $this->filterBuilder
                    ->setField('send_email')
                    ->setConditionType('null')
                    ->create()
            )
            ->create();

        $searchCriteria = $this->searchCriteriaBuilder
            ->setFilterGroups([
                $filtergroup_date,
                $filtergroup_email_sent,
                $filtergroup_send_email
            ])
            ->create();;

        $shipmentList = $this->shipmentRepository->getList($searchCriteria)->getItems();

        foreach ($shipmentList as $shipment) {
            if (count($shipment->getTracks()) > 0) {
                $this->objectManager->create('MagentoShippingModelShipmentNotifier')->notify($shipment);
                $shipment->setEmailSent("1");
                $shipment->save();
            } else {
                $this->logger->warning("Unable to send shipment email for order");
            }
        }
    }
}

Now, when the cron runs it will execute our code and if any shipment mail needs to send then it will be sent.

field_5bfb909c5ccae

    Recent Articles
    Get a Free Quote