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.
If you want restrict customer to checkout based on your...
Sometime we need to set html data without load or...
If you want get query string params in controller file,...
Create di.xml and add the below code Magemonkey/Redirect/etc/frontend/di.xml [crayon-628774bc39b3b131767020/] Create...
You can try below code to change local date to...