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
1 2 3 4 5 6 7 8 |
<?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="Magemonkeys\Shipmentemailsend\Cron\Send" method="execute"> <schedule>0,15,30,45 * * * *</schedule> </job> </group> </config> |
Step 2: Create a Send.php file. Please follow the below code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
<?php namespace Magemonkeys\Shipmentemailsend\Cron; use Magento\Framework\Api\Search\FilterGroupBuilder; use Magento\Framework\Api\SearchCriteriaBuilder; use Magento\Framework\App\ObjectManager; use Magento\Sales\Model\Order\ShipmentRepository; use Magento\Sales\Model\OrderRepository; use Magento\Framework\Api\FilterBuilder; use Psr\Log\LoggerInterface; 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('Magento\Shipping\Model\ShipmentNotifier')->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.
[crayon-63d3e326b5e30723259456/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.