salespersonRepository = $salespersonRepository; $this->orderAttachment = $orderAttachment; } /** * inject $salesperson * @param SalespersonInterface $salesperson * @return $this */ public function setSalesperson(SalespersonInterface $salesperson): static { $this->salesperson = $salesperson; return $this; } public function getSalesperson(): SalespersonInterface { return $this->salesperson; } /** * inject $salesperson * @param SalespersonInterface $salesperson * @return $this * @throws LocalizedException */ public function setOrderItems(array $orderItems): static { foreach ($orderItems as $item) { if (!$item instanceof \Magento\Sales\Model\Order\Item) { throw new LocalizedException(__('One of item in parameter orderItems array are not instance of \Magento\Sales\Model\Order\Item class')); } } $this->orderItems = $orderItems; return $this; } /** * @return Order\Item[] */ public function getOrderItems(): array { return $this->orderItems; } /** * @param Order $order * @return bool */ protected function checkAndSend(Order $order): bool { $this->identityContainer->setStore($order->getStore()); // if (!$this->identityContainer->isEnabled()) { // return false; // } $this->prepareTemplate($order); /** @var SenderBuilder $sender */ $sender = $this->getSender(); try { $sender->setAttachmentFile($this->orderAttachment->getFile($order, $this)); $sender->send(); } catch (\Exception $e) { $this->logger->error($e->getMessage(), $e->getTrace()); return false; } // if ($this->identityContainer->getCopyMethod() == 'copy') { // try { // $sender->sendCopyTo(); // } catch (\Exception $e) { // $this->logger->error($e->getMessage(), $e->getTrace()); // } // } return true; } public function send(Order $order) { $this->order = $order; return $this->checkAndSend($order); } /** * Prepare email template with variables * * @param Order $order * @return void */ protected function prepareTemplate(Order $order) { try { $transport = [ 'order' => $order, 'orderItems' => $this->orderItems, 'salesperson' => $this->salesperson, 'order_id' => $order->getId(), 'file' => $this->file = $this->orderAttachment->getFile($order, $this), 'order_data' => [ 'customer_name' => $order->getCustomerName(), 'is_not_virtual' => $order->getIsNotVirtual(), 'order_id' => $order->getId(), 'order_item' => $order->getAllVisibleItems(), 'frontend_status_label' => $order->getFrontendStatusLabel() ] ]; $this->identityContainer->setEmailCopyTo($this->getEmailCopyTo($order)); $this->templateContainer->setTemplateVars($transport); parent::prepareTemplate($order); } catch (FileSystemException | \PhpOffice\PhpSpreadsheet\Writer\Exception | Exception $e) { $this->logger->critical($e->getMessage(), $e->getTrace()); } } protected function getEmailCopyTo(Order $order): array { $cc = [$order->getCustomerEmail()]; $fields = ['email', 'rsm_email', 'sisr_email', 'csr_email']; foreach ($fields as $field) { if ($email = $this->salesperson->getData($field)) { $email = strval($email); $email = trim($email, ','); $temp = array_map('trim', explode(',', $email)); $cc = array_merge($cc, $temp); } } return $cc; } }