diff --git a/src/Repository/OrderRepository.php b/src/Repository/OrderRepository.php index 82d43d1f4..bca48c021 100644 --- a/src/Repository/OrderRepository.php +++ b/src/Repository/OrderRepository.php @@ -36,4 +36,16 @@ public function findOneByCartId($id_cart) { return $this->findOneBy(['id_cart' => (int) $id_cart]); } + + /** + * @param int $id_cart + * + * @return \PrestaShopCollection + * + * @throws \PrestaShopException + */ + public function findAllByCartId($id_cart) + { + return $this->findAllBy(['id_cart' => (int) $id_cart]); + } } diff --git a/src/Service/OrderStatusService.php b/src/Service/OrderStatusService.php index bfa1dab0a..47fe0d11c 100644 --- a/src/Service/OrderStatusService.php +++ b/src/Service/OrderStatusService.php @@ -16,6 +16,7 @@ use Mollie\Api\Types\OrderStatus; use Mollie\Api\Types\PaymentStatus; use Mollie\Config\Config; +use Mollie\Repository\OrderRepository; use Mollie\Utility\OrderStatusUtility; use Order; use OrderDetail; @@ -36,9 +37,12 @@ class OrderStatusService */ private $mailService; - public function __construct(MailService $mailService) + private $orderRepository; + + public function __construct(MailService $mailService, OrderRepository $orderRepository) { $this->mailService = $mailService; + $this->orderRepository = $orderRepository; } /** @@ -102,20 +106,41 @@ public function setOrderStatus($orderId, $statusId, $useExistingPayment = null, $useExistingPayment = !$order->hasInvoice(); } - $history = new OrderHistory(); - $history->id_order = $order->id; - $history->changeIdOrderState($statusId, $orderId, $useExistingPayment); + $orders = $this->orderRepository->findAllByCartId($order->id_cart); + if (count($orders) > 1) { + foreach ($orders as $subOrder) { + $history = new OrderHistory(); + $history->id_order = $subOrder->id; + $history->changeIdOrderState($statusId, $subOrder->id, $useExistingPayment); - $status = OrderStatusUtility::transformPaymentStatusToPaid($status, Config::STATUS_PAID_ON_BACKORDER); + $status = OrderStatusUtility::transformPaymentStatusToPaid($status, Config::STATUS_PAID_ON_BACKORDER); - if ($this->checkIfOrderConfNeedsToBeSend($statusId)) { - $this->mailService->sendOrderConfMail($order, $statusId); - } + if ($this->checkIfOrderConfNeedsToBeSend($statusId)) { + $this->mailService->sendOrderConfMail($subOrder, $statusId); + } - if ('0' === Configuration::get('MOLLIE_MAIL_WHEN_' . Tools::strtoupper($status))) { - $history->add(); + if ('0' === Configuration::get('MOLLIE_MAIL_WHEN_' . Tools::strtoupper($status))) { + $history->add(); + } else { + $history->addWithemail(true, $templateVars); + } + } } else { - $history->addWithemail(true, $templateVars); + $history = new OrderHistory(); + $history->id_order = $order->id; + $history->changeIdOrderState($statusId, $orderId, $useExistingPayment); + + $status = OrderStatusUtility::transformPaymentStatusToPaid($status, Config::STATUS_PAID_ON_BACKORDER); + + if ($this->checkIfOrderConfNeedsToBeSend($statusId)) { + $this->mailService->sendOrderConfMail($order, $statusId); + } + + if ('0' === Configuration::get('MOLLIE_MAIL_WHEN_' . Tools::strtoupper($status))) { + $history->add(); + } else { + $history->addWithemail(true, $templateVars); + } } }