/** * Send email to customer * * @param Order $order * @param bool $notify * @param string $comment * @return bool */ public function send(Order $order, $notify = true, $comment = '') { if ($order->getShippingAddress()) { $formattedShippingAddress = $this->addressRenderer->format($order->getShippingAddress(), 'html'); } else { $formattedShippingAddress = ''; } $formattedBillingAddress = $this->addressRenderer->format($order->getBillingAddress(), 'html'); $transport = new \Magento\Framework\Object(['template_vars' => ['order' => $order, 'comment' => $comment, 'billing' => $order->getBillingAddress(), 'store' => $order->getStore(), 'formattedShippingAddress' => $formattedShippingAddress, 'formattedBillingAddress' => $formattedBillingAddress]]); $this->eventManager->dispatch('email_order_comment_set_template_vars_before', ['sender' => $this, 'transport' => $transport]); $this->templateContainer->setTemplateVars($transport->getTemplateVars()); return $this->checkAndSend($order, $notify); }
/** * Prepare email template with variables * * @param Order $order * @return void */ protected function prepareTemplate(Order $order) { $transport = new \Magento\Framework\Object(['template_vars' => ['order' => $order, 'billing' => $order->getBillingAddress(), 'payment_html' => $this->getPaymentHtml($order), 'store' => $order->getStore(), 'formattedShippingAddress' => $this->addressRenderer->format($order->getShippingAddress(), 'html'), 'formattedBillingAddress' => $this->addressRenderer->format($order->getBillingAddress(), 'html')]]); $this->eventManager->dispatch('email_order_set_template_vars_before', ['sender' => $this, 'transport' => $transport]); $this->templateContainer->setTemplateVars($transport->getTemplateVars()); parent::prepareTemplate($order); }
/** * Getter for billing address of order by format * * @param \Magento\Sales\Model\Order $order * @return string */ public function getBillingAddressFormattedHtml($order) { $billingAddress = $order->getBillingAddress(); if (!$billingAddress instanceof \Magento\Sales\Model\Order\Address) { return ''; } return $this->addressRenderer->format($billingAddress, 'html'); }
/** * Sends order creditmemo email to the customer. * * Email will be sent immediately in two cases: * * - if asynchronous email sending is disabled in global settings * - if $forceSyncMode parameter is set to TRUE * * Otherwise, email will be sent later during running of * corresponding cron job. * * @param Creditmemo $creditmemo * @param bool $forceSyncMode * @return bool */ public function send(Creditmemo $creditmemo, $forceSyncMode = false) { $creditmemo->setSendEmail(true); if (!$this->globalConfig->getValue('sales_email/general/async_sending') || $forceSyncMode) { $order = $creditmemo->getOrder(); if ($order->getShippingAddress()) { $formattedShippingAddress = $this->addressRenderer->format($order->getShippingAddress(), 'html'); } else { $formattedShippingAddress = ''; } $formattedBillingAddress = $this->addressRenderer->format($order->getBillingAddress(), 'html'); $transport = new \Magento\Framework\Object(['template_vars' => ['order' => $order, 'creditmemo' => $creditmemo, 'comment' => $creditmemo->getCustomerNoteNotify() ? $creditmemo->getCustomerNote() : '', 'billing' => $order->getBillingAddress(), 'payment_html' => $this->getPaymentHtml($order), 'store' => $order->getStore(), 'formattedShippingAddress' => $formattedShippingAddress, 'formattedBillingAddress' => $formattedBillingAddress]]); $this->eventManager->dispatch('email_creditmemo_set_template_vars_before', ['sender' => $this, 'transport' => $transport]); $this->templateContainer->setTemplateVars($transport->getTemplateVars()); if ($this->checkAndSend($order)) { $creditmemo->setEmailSent(true); $this->creditmemoResource->saveAttribute($creditmemo, ['send_email', 'email_sent']); return true; } } $this->creditmemoResource->saveAttribute($creditmemo, 'send_email'); return false; }
/** * Returns string with formatted address * * @param Address $address * @return null|string */ public function getFormattedAddress(Address $address) { return $this->addressRenderer->format($address, 'html'); }
/** * Insert order to pdf page * * @param \Zend_Pdf_Page &$page * @param \Magento\Sales\Model\Order $obj * @param bool $putOrderId * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ protected function insertOrder(&$page, $obj, $putOrderId = true) { if ($obj instanceof \Magento\Sales\Model\Order) { $shipment = null; $order = $obj; } elseif ($obj instanceof \Magento\Sales\Model\Order\Shipment) { $shipment = $obj; $order = $shipment->getOrder(); } $this->y = $this->y ? $this->y : 815; $top = $this->y; $page->setFillColor(new \Zend_Pdf_Color_GrayScale(0.45)); $page->setLineColor(new \Zend_Pdf_Color_GrayScale(0.45)); $page->drawRectangle(25, $top, 570, $top - 55); $page->setFillColor(new \Zend_Pdf_Color_GrayScale(1)); $this->setDocHeaderCoordinates([25, $top, 570, $top - 55]); $this->_setFontRegular($page, 10); if ($putOrderId) { $page->drawText(__('Order # ') . $order->getRealOrderId(), 35, $top -= 30, 'UTF-8'); } $page->drawText(__('Order Date: ') . $this->_localeDate->formatDate($this->_localeDate->scopeDate($order->getStore(), $order->getCreatedAt(), true), \IntlDateFormatter::MEDIUM, false), 35, $top -= 15, 'UTF-8'); $top -= 10; $page->setFillColor(new \Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92)); $page->setLineColor(new \Zend_Pdf_Color_GrayScale(0.5)); $page->setLineWidth(0.5); $page->drawRectangle(25, $top, 275, $top - 25); $page->drawRectangle(275, $top, 570, $top - 25); /* Calculate blocks info */ /* Billing Address */ $billingAddress = $this->_formatAddress($this->addressRenderer->format($order->getBillingAddress(), 'pdf')); /* Payment */ $paymentInfo = $this->_paymentData->getInfoBlock($order->getPayment())->setIsSecureMode(true)->toPdf(); $paymentInfo = htmlspecialchars_decode($paymentInfo, ENT_QUOTES); $payment = explode('{{pdf_row_separator}}', $paymentInfo); foreach ($payment as $key => $value) { if (strip_tags(trim($value)) == '') { unset($payment[$key]); } } reset($payment); /* Shipping Address and Method */ if (!$order->getIsVirtual()) { /* Shipping Address */ $shippingAddress = $this->_formatAddress($this->addressRenderer->format($order->getShippingAddress(), 'pdf')); $shippingMethod = $order->getShippingDescription(); } $page->setFillColor(new \Zend_Pdf_Color_GrayScale(0)); $this->_setFontBold($page, 12); $page->drawText(__('Sold to:'), 35, $top - 15, 'UTF-8'); if (!$order->getIsVirtual()) { $page->drawText(__('Ship to:'), 285, $top - 15, 'UTF-8'); } else { $page->drawText(__('Payment Method:'), 285, $top - 15, 'UTF-8'); } $addressesHeight = $this->_calcAddressHeight($billingAddress); if (isset($shippingAddress)) { $addressesHeight = max($addressesHeight, $this->_calcAddressHeight($shippingAddress)); } $page->setFillColor(new \Zend_Pdf_Color_GrayScale(1)); $page->drawRectangle(25, $top - 25, 570, $top - 33 - $addressesHeight); $page->setFillColor(new \Zend_Pdf_Color_GrayScale(0)); $this->_setFontRegular($page, 10); $this->y = $top - 40; $addressesStartY = $this->y; foreach ($billingAddress as $value) { if ($value !== '') { $text = []; foreach ($this->string->split($value, 45, true, true) as $_value) { $text[] = $_value; } foreach ($text as $part) { $page->drawText(strip_tags(ltrim($part)), 35, $this->y, 'UTF-8'); $this->y -= 15; } } } $addressesEndY = $this->y; if (!$order->getIsVirtual()) { $this->y = $addressesStartY; foreach ($shippingAddress as $value) { if ($value !== '') { $text = []; foreach ($this->string->split($value, 45, true, true) as $_value) { $text[] = $_value; } foreach ($text as $part) { $page->drawText(strip_tags(ltrim($part)), 285, $this->y, 'UTF-8'); $this->y -= 15; } } } $addressesEndY = min($addressesEndY, $this->y); $this->y = $addressesEndY; $page->setFillColor(new \Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92)); $page->setLineWidth(0.5); $page->drawRectangle(25, $this->y, 275, $this->y - 25); $page->drawRectangle(275, $this->y, 570, $this->y - 25); $this->y -= 15; $this->_setFontBold($page, 12); $page->setFillColor(new \Zend_Pdf_Color_GrayScale(0)); $page->drawText(__('Payment Method'), 35, $this->y, 'UTF-8'); $page->drawText(__('Shipping Method:'), 285, $this->y, 'UTF-8'); $this->y -= 10; $page->setFillColor(new \Zend_Pdf_Color_GrayScale(1)); $this->_setFontRegular($page, 10); $page->setFillColor(new \Zend_Pdf_Color_GrayScale(0)); $paymentLeft = 35; $yPayments = $this->y - 15; } else { $yPayments = $addressesStartY; $paymentLeft = 285; } foreach ($payment as $value) { if (trim($value) != '') { //Printing "Payment Method" lines $value = preg_replace('/<br[^>]*>/i', "\n", $value); foreach ($this->string->split($value, 45, true, true) as $_value) { $page->drawText(strip_tags(trim($_value)), $paymentLeft, $yPayments, 'UTF-8'); $yPayments -= 15; } } } if ($order->getIsVirtual()) { // replacement of Shipments-Payments rectangle block $yPayments = min($addressesEndY, $yPayments); $page->drawLine(25, $top - 25, 25, $yPayments); $page->drawLine(570, $top - 25, 570, $yPayments); $page->drawLine(25, $yPayments, 570, $yPayments); $this->y = $yPayments - 15; } else { $topMargin = 15; $methodStartY = $this->y; $this->y -= 15; foreach ($this->string->split($shippingMethod, 45, true, true) as $_value) { $page->drawText(strip_tags(trim($_value)), 285, $this->y, 'UTF-8'); $this->y -= 15; } $yShipments = $this->y; $totalShippingChargesText = "(" . __('Total Shipping Charges') . " " . $order->formatPriceTxt($order->getShippingAmount()) . ")"; $page->drawText($totalShippingChargesText, 285, $yShipments - $topMargin, 'UTF-8'); $yShipments -= $topMargin + 10; $tracks = []; if ($shipment) { $tracks = $shipment->getAllTracks(); } if (count($tracks)) { $page->setFillColor(new \Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92)); $page->setLineWidth(0.5); $page->drawRectangle(285, $yShipments, 510, $yShipments - 10); $page->drawLine(400, $yShipments, 400, $yShipments - 10); //$page->drawLine(510, $yShipments, 510, $yShipments - 10); $this->_setFontRegular($page, 9); $page->setFillColor(new \Zend_Pdf_Color_GrayScale(0)); //$page->drawText(__('Carrier'), 290, $yShipments - 7 , 'UTF-8'); $page->drawText(__('Title'), 290, $yShipments - 7, 'UTF-8'); $page->drawText(__('Number'), 410, $yShipments - 7, 'UTF-8'); $yShipments -= 20; $this->_setFontRegular($page, 8); foreach ($tracks as $track) { $maxTitleLen = 45; $endOfTitle = strlen($track->getTitle()) > $maxTitleLen ? '...' : ''; $truncatedTitle = substr($track->getTitle(), 0, $maxTitleLen) . $endOfTitle; $page->drawText($truncatedTitle, 292, $yShipments, 'UTF-8'); $page->drawText($track->getNumber(), 410, $yShipments, 'UTF-8'); $yShipments -= $topMargin - 5; } } else { $yShipments -= $topMargin - 5; } $currentY = min($yPayments, $yShipments); // replacement of Shipments-Payments rectangle block $page->drawLine(25, $methodStartY, 25, $currentY); //left $page->drawLine(25, $currentY, 570, $currentY); //bottom $page->drawLine(570, $currentY, 570, $methodStartY); //right $this->y = $currentY; $this->y -= 15; } }
/** * Returns string with formatted address * * @param Address $address * @return null|string */ public function getFormattedAddress(\Magento\Sales\Model\Order\Address $address) { return $this->addressRenderer->format($address, 'html'); }
/** * Formats order address to html, pdf and etc. formats * * @param \Magento\Sales\Model\Order\Address $address * @param string $format * @return null|string */ public function formatAddress(\Magento\Sales\Model\Order\Address $address, $format) { return $this->addressRenderer->format($address, $format); }
/** * @param Order $order * @return string|null */ protected function getFormattedBillingAddress($order) { return $this->addressRenderer->format($order->getBillingAddress(), 'html'); }