Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OrderPlace.php
Go to the documentation of this file.
1 <?php
8 
17 
23 {
27  private $cartManagement;
28 
32  private $agreementsValidator;
33 
37  private $customerSession;
38 
42  private $checkoutHelper;
43 
52  public function __construct(
53  CartManagementInterface $cartManagement,
54  AgreementsValidatorInterface $agreementsValidator,
55  Session $customerSession,
56  Data $checkoutHelper
57  ) {
58  $this->cartManagement = $cartManagement;
59  $this->agreementsValidator = $agreementsValidator;
60  $this->customerSession = $customerSession;
61  $this->checkoutHelper = $checkoutHelper;
62  }
63 
72  public function execute(Quote $quote, array $agreement)
73  {
74  if (!$this->agreementsValidator->isValid($agreement)) {
75  throw new LocalizedException(__(
76  "The order wasn't placed. First, agree to the terms and conditions, then try placing your order again."
77  ));
78  }
79 
80  if ($this->getCheckoutMethod($quote) === Onepage::METHOD_GUEST) {
81  $this->prepareGuestQuote($quote);
82  }
83 
84  $this->disabledQuoteAddressValidation($quote);
85 
86  $quote->collectTotals();
87  $this->cartManagement->placeOrder($quote->getId());
88  }
89 
96  private function getCheckoutMethod(Quote $quote)
97  {
98  if ($this->customerSession->isLoggedIn()) {
100  }
101  if (!$quote->getCheckoutMethod()) {
102  if ($this->checkoutHelper->isAllowedGuestCheckout($quote)) {
103  $quote->setCheckoutMethod(Onepage::METHOD_GUEST);
104  } else {
105  $quote->setCheckoutMethod(Onepage::METHOD_REGISTER);
106  }
107  }
108 
109  return $quote->getCheckoutMethod();
110  }
111 
118  private function prepareGuestQuote(Quote $quote)
119  {
120  $quote->setCustomerId(null)
121  ->setCustomerEmail($quote->getBillingAddress()->getEmail())
122  ->setCustomerIsGuest(true)
123  ->setCustomerGroupId(Group::NOT_LOGGED_IN_ID);
124  }
125 }
$quote
__()
Definition: __.php:13
__construct(CartManagementInterface $cartManagement, AgreementsValidatorInterface $agreementsValidator, Session $customerSession, Data $checkoutHelper)
Definition: OrderPlace.php:52
$cartManagement
Definition: quote.php:16
execute(Quote $quote, array $agreement)
Definition: OrderPlace.php:72