cartManagement = $cartManagement; $this->agreementsValidator = $agreementsValidator; $this->customerSession = $customerSession; $this->checkoutHelper = $checkoutHelper; $this->orderCancellationService = $orderCancellationService; } /** * Execute operation * * @param Quote $quote * @param array $agreement * @return void * @throws \Exception */ public function execute(Quote $quote, array $agreement) { if (!$this->agreementsValidator->isValid($agreement)) { throw new LocalizedException(__( "The order wasn't placed. First, agree to the terms and conditions, then try placing your order again." )); } if ($this->getCheckoutMethod($quote) === Onepage::METHOD_GUEST) { $this->prepareGuestQuote($quote); } $this->disabledQuoteAddressValidation($quote); $quote->collectTotals(); try { $this->cartManagement->placeOrder($quote->getId()); } catch (\Exception $e) { $this->orderCancellationService->execute($quote->getReservedOrderId()); throw $e; } } /** * Get checkout method * * @param Quote $quote * @return string */ private function getCheckoutMethod(Quote $quote) { if ($this->customerSession->isLoggedIn()) { return Onepage::METHOD_CUSTOMER; } if (!$quote->getCheckoutMethod()) { if ($this->checkoutHelper->isAllowedGuestCheckout($quote)) { $quote->setCheckoutMethod(Onepage::METHOD_GUEST); } else { $quote->setCheckoutMethod(Onepage::METHOD_REGISTER); } } return $quote->getCheckoutMethod(); } /** * Prepare quote for guest checkout order submit * * @param Quote $quote * @return void */ private function prepareGuestQuote(Quote $quote) { $quote->setCustomerId(null) ->setCustomerEmail($quote->getBillingAddress()->getEmail()) ->setCustomerIsGuest(true) ->setCustomerGroupId(Group::NOT_LOGGED_IN_ID); } }