Magento 2 - How can I send new mail to customer, if any order has been placed by banktransfer and more that 7 days since order placed.
I need to send mail using Crons.
This is my code:
<?php
namespace Arnavcancleorder\Cancleorder\Cron;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\Search\FilterGroup;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Sales\Api\OrderManagementInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order;
use Magento\Framework\App\RequestInterface;
class Sendemail extends \Magento\Framework\App\Action\Action
{
private $orderRepository;
/**
* @var SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;
private $filterBuilder;
private $filterGroup;
private $orderManagement;
protected $_request;
protected $_transportBuilder;
protected $_storeManager;
public function __construct(
OrderRepositoryInterface $orderRepository,
SearchCriteriaBuilder $searchCriteriaBuilder,
FilterBuilder $filterBuilder,
FilterGroup $filterGroup,
OrderManagementInterface $orderManagement,
\Magento\Framework\App\Action\Context $context
, \Magento\Framework\App\Request\Http $request
, \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
, \Magento\Store\Model\StoreManagerInterface $storeManager
) {
$this->orderRepository = $orderRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->filterBuilder = $filterBuilder;
$this->filterGroup = $filterGroup;
$this->orderManagement = $orderManagement;
$this->_request = $request;
$this->_transportBuilder = $transportBuilder;
$this->_storeManager = $storeManager;
parent::__construct($context);
}
public function execute()
{
$today = date("Y-m-d h:i:s");
$to = strtotime('-2 min', strtotime($today));
$to = date('Y-m-d h:i:s', $to);
$filterGroupDate = $this->filterGroup;
$filterGroupStatus = clone($filterGroupDate);
$filterGroupEmail = clone($this->filterGroup);
$filterDate = $this->filterBuilder
->setField('updated_at')
->setConditionType('to')
->setValue($to)
->create();
$filterStatus = $this->filterBuilder
->setField('status')
->setConditionType('eq')
->setValue('ced_credit_limit')
->create();
$filterEmail = $this->filterBuilder
->setField('email_msg')
->setConditionType('eq')
->setValue('2')
->create();
$filterGroupDate->setFilters([$filterDate]);
$filterGroupStatus->setFilters([$filterStatus]);
$filterGroupEmail->setFilters([$filterEmail]);
$searchCriteria = $this->searchCriteriaBuilder->setFilterGroups(
[$filterGroupDate, $filterGroupStatus,$filterGroupEmail]
);
$searchResults = $this->orderRepository->getList($searchCriteria->create());
foreach ($searchResults->getItems() as $order) {
$names=$order->getCustomerEmail();
$store = $this->_storeManager->getStore()->getId();
$transport = $this->_transportBuilder->setTemplateIdentifier('40')
->setTemplateOptions(['area' => 'frontend', 'store' => $store])
->setTemplateVars(
[
'store' => $this->_storeManager->getStore(),
]
)
->setFrom('general')
// you can config general email address in Store -> Configuration -> General -> Store Email Addresses
->addTo($names)
->getTransport();
$transport->sendMessage();
return $this;
}
}
}