21 use Magento\Sales\Api\Data\OrderInterfaceFactory as OrderFactory;
136 private $quoteIdMaskFactory;
141 private $addressRepository;
146 private $addressesToSync = [];
194 \
Magento\
Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory =
null,
228 $storeId = $this->storeManager->getStore()->getStoreId();
231 $quote->setBillingAddress($this->quoteAddressFactory->create());
232 $quote->setShippingAddress($this->quoteAddressFactory->create());
235 $quote->getShippingAddress()->setCollectShippingRates(
true);
236 $this->quoteRepository->save(
$quote);
248 $storeId = $this->storeManager->getStore()->getStoreId();
252 $this->quoteRepository->save(
$quote);
256 return (
int)
$quote->getId();
266 $customerModel = $this->customerModelFactory->create();
270 __(
"The customer can't be assigned to the cart. The cart belongs to a different store.")
273 if (
$quote->getCustomerId()) {
274 throw new StateException(
275 __(
"The customer can't be assigned to the cart because the cart isn't anonymous.")
279 $this->quoteRepository->getForCustomer(
$customerId);
280 throw new StateException(
281 __(
"The customer can't be assigned to the cart because the customer already has an active cart.")
283 }
catch (\
Magento\Framework\Exception\NoSuchEntityException $e) {
287 $quote->setCustomerIsGuest(0);
293 $this->quoteRepository->save(
$quote);
303 protected function createAnonymousCart(
$storeId)
306 $quote = $this->quoteFactory->create();
323 }
catch (\
Magento\Framework\Exception\NoSuchEntityException $e) {
326 $quote = $this->quoteFactory->create();
329 $quote->setCustomerIsGuest(0);
340 if ($paymentMethod) {
341 $paymentMethod->setChecks([
343 \
Magento\
Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_COUNTRY,
344 \
Magento\
Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_CURRENCY,
345 \
Magento\
Payment\Model\Method\AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX,
350 $data = $paymentMethod->getData();
355 $quote->setCustomerId(
null);
356 $quote->setCustomerEmail(
$quote->getBillingAddress()->getEmail());
357 $quote->setCustomerIsGuest(
true);
361 $this->eventManager->dispatch(
'checkout_submit_before', [
'quote' =>
$quote]);
367 __(
'A server error stopped your order from being placed. Please try to place your order again.')
371 $this->checkoutSession->setLastQuoteId(
$quote->getId());
372 $this->checkoutSession->setLastSuccessQuoteId(
$quote->getId());
373 $this->checkoutSession->setLastOrderId(
$order->getId());
374 $this->checkoutSession->setLastRealOrderId(
$order->getIncrementId());
375 $this->checkoutSession->setLastOrderStatus(
$order->getStatus());
377 $this->eventManager->dispatch(
'checkout_submit_all_after', [
'order' =>
$order,
'quote' =>
$quote]);
386 return $this->quoteRepository->getActiveForCustomer(
$customerId);
400 if (!
$quote->getAllVisibleItems()) {
401 $quote->setIsActive(
false);
422 $parentItemId =
$quoteItem->getParentItemId();
424 if ($parentItemId && !isset(
$orderItems[$parentItemId])) {
425 $orderItems[$parentItemId] = $this->quoteItemToOrderItem->convert(
427 [
'parent_item' =>
null]
431 $orderItems[$itemId] = $this->quoteItemToOrderItem->convert(
$quoteItem, [
'parent_item' => $parentItem]);
447 $order = $this->orderFactory->create();
448 $this->quoteValidator->validateBeforeSubmit(
$quote);
449 if (!
$quote->getCustomerIsGuest()) {
450 if (
$quote->getCustomerId()) {
451 $this->_prepareCustomerQuote(
$quote);
452 $this->customerManagement->validateAddresses(
$quote);
454 $this->customerManagement->populateCustomerInfo(
$quote);
458 if (
$quote->isVirtual()) {
459 $this->dataObjectHelper->mergeDataObjects(
460 \
Magento\Sales\Api\Data\OrderInterface::class,
462 $this->quoteAddressToOrder->convert(
$quote->getBillingAddress(), $orderData)
465 $this->dataObjectHelper->mergeDataObjects(
466 \
Magento\Sales\Api\Data\OrderInterface::class,
468 $this->quoteAddressToOrder->convert(
$quote->getShippingAddress(), $orderData)
471 $quote->getShippingAddress(),
473 'address_type' =>
'shipping',
474 'email' =>
$quote->getCustomerEmail()
480 $order->setShippingMethod(
$quote->getShippingAddress()->getShippingMethod());
483 $quote->getBillingAddress(),
485 'address_type' =>
'billing',
486 'email' =>
$quote->getCustomerEmail()
493 $order->setPayment($this->quotePaymentToOrderPayment->convert(
$quote->getPayment()));
495 if (
$quote->getCustomer()) {
500 $order->setCustomerFirstname(
$quote->getCustomerFirstname());
501 $order->setCustomerMiddlename(
$quote->getCustomerMiddlename());
502 $order->setCustomerLastname(
$quote->getCustomerLastname());
504 $this->eventManager->dispatch(
505 'sales_model_service_quote_submit_before',
513 $quote->setIsActive(
false);
514 $this->eventManager->dispatch(
515 'sales_model_service_quote_submit_success',
521 $this->quoteRepository->save(
$quote);
523 if (!empty($this->addressesToSync)) {
524 foreach ($this->addressesToSync as $addressId) {
525 $this->addressRepository->deleteById($addressId);
528 $this->eventManager->dispatch(
529 'sales_model_service_quote_submit_failure',
549 protected function _prepareCustomerQuote(
$quote)
552 $billing =
$quote->getBillingAddress();
553 $shipping =
$quote->isVirtual() ? null :
$quote->getShippingAddress();
555 $customer = $this->customerRepository->getById(
$quote->getCustomerId());
556 $hasDefaultBilling = (bool)
$customer->getDefaultBilling();
557 $hasDefaultShipping = (bool)
$customer->getDefaultShipping();
559 if ($shipping && !$shipping->getSameAsBilling()
560 && (!$shipping->getCustomerId() || $shipping->getSaveInAddressBook())
563 if (!$hasDefaultShipping) {
566 $hasDefaultShipping =
true;
567 if (!$hasDefaultBilling && !$billing->getSaveInAddressBook()) {
569 $hasDefaultBilling =
true;
581 if (!$billing->getCustomerId() || $billing->getSaveInAddressBook()) {
583 if (!$hasDefaultBilling) {
585 if (!$hasDefaultShipping) {
598 if ($shipping && !$shipping->getCustomerId() && !$hasDefaultBilling) {
599 $shipping->setIsDefaultBilling(
true);
$quotePaymentToOrderPayment
submit(QuoteEntity $quote, $orderData=[])
assignCustomer($cartId, $customerId, $storeId)
placeOrder($cartId, PaymentInterface $paymentMethod=null)
__construct(EventManager $eventManager, QuoteValidator $quoteValidator, OrderFactory $orderFactory, OrderManagement $orderManagement, CustomerManagement $customerManagement, ToOrderConverter $quoteAddressToOrder, ToOrderAddressConverter $quoteAddressToOrderAddress, ToOrderItemConverter $quoteItemToOrderItem, ToOrderPaymentConverter $quotePaymentToOrderPayment, UserContextInterface $userContext, \Magento\Quote\Api\CartRepositoryInterface $quoteRepository, \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository, \Magento\Customer\Model\CustomerFactory $customerModelFactory, \Magento\Quote\Model\Quote\AddressFactory $quoteAddressFactory, \Magento\Framework\Api\DataObjectHelper $dataObjectHelper, StoreManagerInterface $storeManager, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Customer\Model\Session $customerSession, \Magento\Customer\Api\AccountManagementInterface $accountManagement, \Magento\Quote\Model\QuoteFactory $quoteFactory, \Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory=null, \Magento\Customer\Api\AddressRepositoryInterface $addressRepository=null)
$quoteAddressToOrderAddress
submitQuote(QuoteEntity $quote, $orderData=[])
createEmptyCartForCustomer($customerId)
getCartForCustomer($customerId)