From 77eed7036ecc5d1981512b8670ffba2f9aebf001 Mon Sep 17 00:00:00 2001 From: Maksym Novik Date: Mon, 1 Apr 2019 23:10:54 +0300 Subject: [PATCH] Add "Ready for Pickup" button on the Order page in Admin Panel which notifies Customer that Order could be picked up #2034. Implemented synchronous email sending with custom email template for storepickup. Added even more TODOs --- .../Model/NotifyOrderIsReadyAndShip.php | 38 ++++++-- .../Container/ReadyForPickupIdentity.php | 91 +++++++++++++++++++ .../Order/Email/ReadyForPickupNotifier.php | 51 +++++++++++ .../Order/Email/ReadyForPickupSender.php | 88 ++++++++++++++++++ InventoryInStorePickup/composer.json | 3 +- InventoryInStorePickup/etc/config.xml | 26 ++++++ InventoryInStorePickup/etc/di.xml | 6 +- .../etc/email_templates.xml | 12 +++ InventoryInStorePickup/etc/module.xml | 1 + .../email/order_ready_for_pickup.html | 53 +++++++++++ .../NotifyOrderIsReadyAndShipInterface.php | 4 +- 11 files changed, 363 insertions(+), 10 deletions(-) create mode 100644 InventoryInStorePickup/Model/Order/Email/Container/ReadyForPickupIdentity.php create mode 100644 InventoryInStorePickup/Model/Order/Email/ReadyForPickupNotifier.php create mode 100644 InventoryInStorePickup/Model/Order/Email/ReadyForPickupSender.php create mode 100644 InventoryInStorePickup/etc/config.xml create mode 100644 InventoryInStorePickup/etc/email_templates.xml create mode 100644 InventoryInStorePickup/view/frontend/email/order_ready_for_pickup.html diff --git a/InventoryInStorePickup/Model/NotifyOrderIsReadyAndShip.php b/InventoryInStorePickup/Model/NotifyOrderIsReadyAndShip.php index c065ee5ba523..276d2ef5b7dc 100644 --- a/InventoryInStorePickup/Model/NotifyOrderIsReadyAndShip.php +++ b/InventoryInStorePickup/Model/NotifyOrderIsReadyAndShip.php @@ -23,32 +23,58 @@ class NotifyOrderIsReadyAndShip implements NotifyOrderIsReadyAndShipInterface */ private $shipOrder; + /** + * @var \Magento\InventoryInStorePickup\Model\Order\Email\ReadyForPickupNotifier + */ + private $emailNotifier; + + /** + * @var \Magento\Sales\Api\OrderRepositoryInterface + */ + private $orderRepository; + /** * NotifyOrderIsReadyAndShip constructor. * - * @param \Magento\InventoryInStorePickupApi\Api\IsOrderReadyForPickupInterface $isOrderReadyForPickup - * @param \Magento\Sales\Api\ShipOrderInterface $shipOrder + * @param \Magento\InventoryInStorePickupApi\Api\IsOrderReadyForPickupInterface $isOrderReadyForPickup + * @param \Magento\Sales\Api\ShipOrderInterface $shipOrder + * @param \Magento\InventoryInStorePickup\Model\Order\Email\ReadyForPickupNotifier $emailNotifier + * @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository */ public function __construct( IsOrderReadyForPickupInterface $isOrderReadyForPickup, - \Magento\Sales\Api\ShipOrderInterface $shipOrder + \Magento\Sales\Api\ShipOrderInterface $shipOrder, + Order\Email\ReadyForPickupNotifier $emailNotifier, + \Magento\Sales\Api\OrderRepositoryInterface $orderRepository ) { $this->isOrderReadyForPickup = $isOrderReadyForPickup; $this->shipOrder = $shipOrder; + $this->emailNotifier = $emailNotifier; + $this->orderRepository = $orderRepository; } /** * {@inheritdoc} */ - public function execute(int $orderId):int + public function execute(int $orderId):?int { if (!$this->isOrderReadyForPickup->execute($orderId)) { throw new OrderIsNotReadyForPickupException(); } - /* TODO: send email */ + $this->emailNotifier->notify($this->getOrder($orderId)); /* TODO: add order comment? */ - return $this->shipOrder->execute($orderId); + return (int)$this->shipOrder->execute($orderId); + } + + /** + * @param int $orderId + * + * @return \Magento\Sales\Api\Data\OrderInterface|\Magento\Sales\Model\Order + */ + private function getOrder(int $orderId) + { + return $this->orderRepository->get($orderId); } } diff --git a/InventoryInStorePickup/Model/Order/Email/Container/ReadyForPickupIdentity.php b/InventoryInStorePickup/Model/Order/Email/Container/ReadyForPickupIdentity.php new file mode 100644 index 000000000000..88719b363600 --- /dev/null +++ b/InventoryInStorePickup/Model/Order/Email/Container/ReadyForPickupIdentity.php @@ -0,0 +1,91 @@ +scopeConfig->isSetFlag( + self::XML_PATH_EMAIL_ENABLED, + \Magento\Store\Model\ScopeInterface::SCOPE_STORE, + $this->getStore()->getStoreId() + ); + } + + /** + * Return email copy_to list + * + * @return array|bool + */ + public function getEmailCopyTo() + { + $data = $this->getConfigValue(self::XML_PATH_EMAIL_COPY_TO, $this->getStore()->getStoreId()); + if (!empty($data)) { + return explode(',', $data); + } + + return false; + } + + /** + * Return copy method + * + * @return mixed + */ + public function getCopyMethod() + { + return $this->getConfigValue(self::XML_PATH_EMAIL_COPY_METHOD, $this->getStore()->getStoreId()); + } + + /** + * Return guest template id + * + * @return mixed + */ + public function getGuestTemplateId() + { + return $this->getConfigValue(self::XML_PATH_EMAIL_GUEST_TEMPLATE, $this->getStore()->getStoreId()); + } + + /** + * Return template id + * + * @return mixed + */ + public function getTemplateId() + { + return $this->getConfigValue(self::XML_PATH_EMAIL_TEMPLATE, $this->getStore()->getStoreId()); + } + + /** + * Return email identity + * + * @return mixed + */ + public function getEmailIdentity() + { + return $this->getConfigValue(self::XML_PATH_EMAIL_IDENTITY, $this->getStore()->getStoreId()); + } +} diff --git a/InventoryInStorePickup/Model/Order/Email/ReadyForPickupNotifier.php b/InventoryInStorePickup/Model/Order/Email/ReadyForPickupNotifier.php new file mode 100644 index 000000000000..786362f81399 --- /dev/null +++ b/InventoryInStorePickup/Model/Order/Email/ReadyForPickupNotifier.php @@ -0,0 +1,51 @@ +historyCollectionFactory = $historyCollectionFactory; + $this->logger = $logger; + $this->sender = $sender; + } +} diff --git a/InventoryInStorePickup/Model/Order/Email/ReadyForPickupSender.php b/InventoryInStorePickup/Model/Order/Email/ReadyForPickupSender.php new file mode 100644 index 000000000000..45a9985b4a44 --- /dev/null +++ b/InventoryInStorePickup/Model/Order/Email/ReadyForPickupSender.php @@ -0,0 +1,88 @@ +eventManager = $eventManager; + } + + /** + * @param \Magento\Sales\Model\Order $order + * + * @return bool + */ + public function send(\Magento\Sales\Model\Order $order):bool + { + return $this->checkAndSend($order); + } + + /** + * Prepare email template with variables + * + * @param \Magento\Sales\Model\ $order + * + * @return void + */ + protected function prepareTemplate(\Magento\Sales\Model\Order $order) + { + $transport = [ + 'order' => $order, + 'store' => $order->getStore(), + 'formattedShippingAddress' => $this->getFormattedShippingAddress($order), + ]; + $transportObject = new DataObject($transport); + + /** + * Event argument `transport` is @deprecated. Use `transportObject` instead. + */ + $this->eventManager->dispatch( + 'email_ready_for_pickup_set_template_vars_before', + ['sender' => $this, 'transport' => $transportObject, 'transportObject' => $transportObject] + ); + + $this->templateContainer->setTemplateVars($transportObject->getData()); + + parent::prepareTemplate($order); + } + +} diff --git a/InventoryInStorePickup/composer.json b/InventoryInStorePickup/composer.json index caa00eafee06..8d438f55a232 100644 --- a/InventoryInStorePickup/composer.json +++ b/InventoryInStorePickup/composer.json @@ -6,7 +6,8 @@ "magento/framework": "*", "magento/module-inventory-in-store-pickup-api": "*", "magento/module-inventory-api": "*", - "magento/module-sales": "*" + "magento/module-sales": "*", + "magento/module-store": "*" }, "type": "magento2-module", "license": [ diff --git a/InventoryInStorePickup/etc/config.xml b/InventoryInStorePickup/etc/config.xml new file mode 100644 index 000000000000..33c4380f25c2 --- /dev/null +++ b/InventoryInStorePickup/etc/config.xml @@ -0,0 +1,26 @@ + + + + + + + 1 + + inventory_instorepickup_order_ready_for_pickup_template + storepickup + bcc + + + + + sales@example.com + Store pickup + + + + diff --git a/InventoryInStorePickup/etc/di.xml b/InventoryInStorePickup/etc/di.xml index f76750e559df..3c4229917676 100644 --- a/InventoryInStorePickup/etc/di.xml +++ b/InventoryInStorePickup/etc/di.xml @@ -30,5 +30,9 @@ type="\Magento\InventoryInStorePickup\Model\NotifyOrderIsReadyAndShip"/> - + + + \Magento\InventoryInStorePickup\Model\Order\Email\Container\ReadyForPickupIdentity + + diff --git a/InventoryInStorePickup/etc/email_templates.xml b/InventoryInStorePickup/etc/email_templates.xml new file mode 100644 index 000000000000..58fb410e0239 --- /dev/null +++ b/InventoryInStorePickup/etc/email_templates.xml @@ -0,0 +1,12 @@ + + + +