Source of file OrderEmailNotifier.php
Size: 7,957 Bytes - Last Modified: 2021-12-24T06:35:03+00:00
/var/www/docs.ssmods.com/process/src/src/Checkout/OrderEmailNotifier.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 | <?php namespace SilverShop\Checkout; use SilverShop\Extension\ShopConfigExtension; use SilverShop\Model\Order; use SilverShop\Model\OrderStatusLog; use SilverShop\Page\CheckoutPage; use SilverStripe\Control\Director; use SilverStripe\Control\Email\Email; use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Injector\Injectable; use SilverStripe\Core\Injector\Injector; /** * Handles email notifications to customers and / or admins. * * @package shop */ class OrderEmailNotifier { use Injectable; use Configurable; /** * @var Order $order */ protected $order; /** * @var bool */ protected $debugMode = false; /** * Assign the order to a local variable * * @param Order $order */ public function __construct(Order $order) { $this->order = $order; } /** * @param bool $bool * @return $this */ public function setDebugMode($bool) { $this->debugMode = $bool; return $this; } /** * @param string $template * @param string $subject * * @return Email */ protected function buildEmail($template, $subject) { $from = ShopConfigExtension::config()->email_from ? ShopConfigExtension::config()->email_from : Email::config()->admin_email; $to = $this->order->getLatestEmail(); $checkoutpage = CheckoutPage::get()->first(); $completemessage = $checkoutpage ? $checkoutpage->dbObject('PurchaseComplete') : ''; /** * @var Email $email */ $email = Email::create() ->setHTMLTemplate($template) ->setFrom($from) ->setTo($to) ->setSubject($subject); $email->setData( [ 'PurchaseCompleteMessage' => $completemessage, 'Order' => $this->order, 'BaseURL' => Director::absoluteBaseURL(), ] ); return $email; } /** * Send a mail of the order to the client (and another to the admin). * * @param string $template - the class name of the email you wish to send * @param string $subject - subject of the email * @param bool $copyToAdmin - true by default, whether it should send a copy to the admin * * @return bool|string */ public function sendEmail($template, $subject, $copyToAdmin = true) { $email = $this->buildEmail($template, $subject); if ($copyToAdmin) { $email->setBcc(Email::config()->admin_email); } if ($this->debugMode) { return $this->debug($email); } else { return $email->send(); } } /** * Send customer a confirmation that the order has been received * * @return bool */ public function sendConfirmation() { $subject = _t( 'SilverShop\ShopEmail.ConfirmationSubject', 'Order #{OrderNo} confirmation', '', array('OrderNo' => $this->order->Reference) ); return $this->sendEmail( 'SilverShop/Model/Order_ConfirmationEmail', $subject, self::config()->bcc_confirmation_to_admin ); } /** * Notify store owner about new order. * * @return bool|string */ public function sendAdminNotification() { $subject = _t( 'SilverShop\ShopEmail.AdminNotificationSubject', 'Order #{OrderNo} notification', '', array('OrderNo' => $this->order->Reference) ); $email = $this->buildEmail('SilverShop/Model/Order_AdminNotificationEmail', $subject) ->setTo(Email::config()->admin_email); if ($this->debugMode) { return $this->debug($email); } else { return $email->send(); } } /** * Send customer an order receipt email. * Precondition: The order payment has been successful */ public function sendReceipt() { $subject = _t( 'SilverShop\ShopEmail.ReceiptSubject', 'Order #{OrderNo} receipt', '', array('OrderNo' => $this->order->Reference) ); return $this->sendEmail( 'SilverShop/Model/Order_ReceiptEmail', $subject, self::config()->bcc_receipt_to_admin ); } /** * Sends an email to the admin that an order has been cancelled */ public function sendCancelNotification() { $email = Email::create() ->setSubject(_t( 'SilverShop\ShopEmail.CancelSubject', 'Order #{OrderNo} cancelled by member', '', ['OrderNo' => $this->order->Reference] )) ->setFrom(Email::config()->admin_email) ->setTo(Email::config()->admin_email) ->setBody($this->order->renderWith(Order::class)); if ($this->debugMode) { return $this->debug($email); } else { return $email->send(); } } /** * Send an email to the customer containing the latest note of {@link OrderStatusLog} and the current status. * * @param string $title Subject for email * @param string $note Optional note-content (instead of using the OrderStatusLog) * * @return bool|string */ public function sendStatusChange($title, $note = null) { $latestLog = null; if (!$note) { // Find the latest log message that hasn't been sent to the client yet, but can be (e.g. is visible) $latestLog = OrderStatusLog::get() ->filter("OrderID", $this->order->ID) ->filter("SentToCustomer", 0) ->filter("VisibleToCustomer", 1) ->first(); if ($latestLog) { $note = $latestLog->Note; $title = $latestLog->Title; } } if (Config::inst()->get(OrderProcessor::class, 'receipt_email')) { $adminEmail = Config::inst()->get(OrderProcessor::class, 'receipt_email'); } else { $adminEmail = Email::config()->admin_email; } /** * @var Email $e */ $email = Email::create() ->setFrom($adminEmail) ->setSubject(_t('SilverShop\ShopEmail.StatusChangeSubject', 'SilverShop – {Title}', ['Title' => $title])) ->setTo($this->order->getLatestEmail()) ->setHTMLTemplate('SilverShop/Model/Order_StatusEmail') ->setData( [ 'Order' => $this->order, 'Note' => $note, 'FromEmail' => $adminEmail ] ); if ($this->debugMode) { $result = $this->debug($email); } else { $result = $email->send(); } if ($latestLog) { // If we got the note from an OrderStatusLog object, mark it as having been sent to the customer $latestLog->SentToCustomer = true; $latestLog->write(); } return $result; } /** * The new Email::debug method in SilverStripe dumps the entire message with all message parts, * which makes it unusable to preview an Email. * This method simulates the old way of the message output and renders only the HTML body. * * @param Email $email * @return string */ protected function debug(Email $email) { $email->render(); $template = $email->getHTMLTemplate(); $headers = $email->getSwiftMessage()->getHeaders()->toString(); return "<h2>Email HTML template: $template</h2>\n" . "<pre>$headers</pre>" . $email->getBody(); } } |