Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
QuoteManagement Class Reference
Inheritance diagram for QuoteManagement:
CartManagementInterface

Public Member Functions

 __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)
 
 createEmptyCart ()
 
 createEmptyCartForCustomer ($customerId)
 
 placeOrder ($cartId, PaymentInterface $paymentMethod=null)
 
 getCartForCustomer ($customerId)
 
 submit (QuoteEntity $quote, $orderData=[])
 
- Public Member Functions inherited from CartManagementInterface
 assignCustomer ($cartId, $customerId, $storeId)
 

Protected Member Functions

 submitQuote (QuoteEntity $quote, $orderData=[])
 

Protected Attributes

 $eventManager
 
 $quoteValidator
 
 $orderFactory
 
 $orderManagement
 
 $customerManagement
 
 $quoteAddressToOrder
 
 $quoteAddressToOrderAddress
 
 $quoteItemToOrderItem
 
 $quotePaymentToOrderPayment
 
 $userContext
 
 $quoteRepository
 
 $customerRepository
 
 $customerModelFactory
 
 $quoteAddressFactory
 
 $dataObjectHelper
 
 $storeManager
 
 $checkoutSession
 
 $customerSession
 
 $accountManagement
 
 $quoteFactory
 

Additional Inherited Members

- Data Fields inherited from CartManagementInterface
const METHOD_GUEST = 'guest'
 

Detailed Description

Class QuoteManagement

@SuppressWarnings(PHPMD.CouplingBetweenObjects) @SuppressWarnings(PHPMD.TooManyFields)

Definition at line 31 of file QuoteManagement.php.

Constructor & Destructor Documentation

◆ __construct()

__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 
)
Parameters
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

Definition at line 173 of file QuoteManagement.php.

196  {
197  $this->eventManager = $eventManager;
198  $this->quoteValidator = $quoteValidator;
199  $this->orderFactory = $orderFactory;
200  $this->orderManagement = $orderManagement;
201  $this->customerManagement = $customerManagement;
202  $this->quoteAddressToOrder = $quoteAddressToOrder;
203  $this->quoteAddressToOrderAddress = $quoteAddressToOrderAddress;
204  $this->quoteItemToOrderItem = $quoteItemToOrderItem;
205  $this->quotePaymentToOrderPayment = $quotePaymentToOrderPayment;
206  $this->userContext = $userContext;
207  $this->quoteRepository = $quoteRepository;
208  $this->customerRepository = $customerRepository;
209  $this->customerModelFactory = $customerModelFactory;
210  $this->quoteAddressFactory = $quoteAddressFactory;
211  $this->dataObjectHelper = $dataObjectHelper;
212  $this->storeManager = $storeManager;
213  $this->checkoutSession = $checkoutSession;
214  $this->accountManagement = $accountManagement;
215  $this->customerSession = $customerSession;
216  $this->quoteFactory = $quoteFactory;
217  $this->quoteIdMaskFactory = $quoteIdMaskFactory ?: ObjectManager::getInstance()
218  ->get(\Magento\Quote\Model\QuoteIdMaskFactory::class);
219  $this->addressRepository = $addressRepository ?: ObjectManager::getInstance()
220  ->get(\Magento\Customer\Api\AddressRepositoryInterface::class);
221  }
$addressRepository

Member Function Documentation

◆ createEmptyCart()

createEmptyCart ( )

{Creates an empty cart and quote for a guest.

Returns
int Cart ID.
Exceptions
}

Implements CartManagementInterface.

Definition at line 226 of file QuoteManagement.php.

227  {
228  $storeId = $this->storeManager->getStore()->getStoreId();
229  $quote = $this->createAnonymousCart($storeId);
230 
231  $quote->setBillingAddress($this->quoteAddressFactory->create());
232  $quote->setShippingAddress($this->quoteAddressFactory->create());
233 
234  try {
235  $quote->getShippingAddress()->setCollectShippingRates(true);
236  $this->quoteRepository->save($quote);
237  } catch (\Exception $e) {
238  throw new CouldNotSaveException(__("The quote can't be created."));
239  }
240  return $quote->getId();
241  }
$quote
__()
Definition: __.php:13

◆ createEmptyCartForCustomer()

createEmptyCartForCustomer (   $customerId)

{Creates an empty cart and quote for a specified customer if customer does not have a cart yet.

Parameters
int$customerIdThe customer ID.
Returns
int new cart ID if customer did not have a cart or ID of the existing cart otherwise.
Exceptions
}

Implements CartManagementInterface.

Definition at line 246 of file QuoteManagement.php.

247  {
248  $storeId = $this->storeManager->getStore()->getStoreId();
249  $quote = $this->createCustomerCart($customerId, $storeId);
250 
251  try {
252  $this->quoteRepository->save($quote);
253  } catch (\Exception $e) {
254  throw new CouldNotSaveException(__("The quote can't be created."));
255  }
256  return (int)$quote->getId();
257  }
$quote
__()
Definition: __.php:13

◆ getCartForCustomer()

getCartForCustomer (   $customerId)

{Returns information for the cart for a specified customer.

Parameters
int$customerIdThe customer ID.
Returns
\Magento\Quote\Api\Data\CartInterface Cart object.
Exceptions
}

Implements CartManagementInterface.

Definition at line 384 of file QuoteManagement.php.

385  {
386  return $this->quoteRepository->getActiveForCustomer($customerId);
387  }

◆ placeOrder()

placeOrder (   $cartId,
PaymentInterface  $paymentMethod = null 
)

{Places an order for a specified cart.

Parameters
int$cartIdThe cart ID.
PaymentInterface | null$paymentMethod
Exceptions
}

Implements CartManagementInterface.

Definition at line 337 of file QuoteManagement.php.

338  {
339  $quote = $this->quoteRepository->getActive($cartId);
340  if ($paymentMethod) {
341  $paymentMethod->setChecks([
342  \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_CHECKOUT,
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,
346  \Magento\Payment\Model\Method\AbstractMethod::CHECK_ZERO_TOTAL,
347  ]);
348  $quote->getPayment()->setQuote($quote);
349 
350  $data = $paymentMethod->getData();
351  $quote->getPayment()->importData($data);
352  }
353 
354  if ($quote->getCheckoutMethod() === self::METHOD_GUEST) {
355  $quote->setCustomerId(null);
356  $quote->setCustomerEmail($quote->getBillingAddress()->getEmail());
357  $quote->setCustomerIsGuest(true);
358  $quote->setCustomerGroupId(\Magento\Customer\Api\Data\GroupInterface::NOT_LOGGED_IN_ID);
359  }
360 
361  $this->eventManager->dispatch('checkout_submit_before', ['quote' => $quote]);
362 
363  $order = $this->submit($quote);
364 
365  if (null == $order) {
366  throw new LocalizedException(
367  __('A server error stopped your order from being placed. Please try to place your order again.')
368  );
369  }
370 
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());
376 
377  $this->eventManager->dispatch('checkout_submit_all_after', ['order' => $order, 'quote' => $quote]);
378  return $order->getId();
379  }
$quote
$order
Definition: order.php:55
__()
Definition: __.php:13
submit(QuoteEntity $quote, $orderData=[])
$cartId
Definition: quote.php:22

◆ submit()

submit ( QuoteEntity  $quote,
  $orderData = [] 
)

Submit quote

Parameters
Quote$quote
array$orderData
Returns
\Magento\Framework\Model\AbstractExtensibleModel|\Magento\Sales\Api\Data\OrderInterface|object|null
Exceptions

Definition at line 398 of file QuoteManagement.php.

399  {
400  if (!$quote->getAllVisibleItems()) {
401  $quote->setIsActive(false);
402  return null;
403  }
404 
405  return $this->submitQuote($quote, $orderData);
406  }
$quote
submitQuote(QuoteEntity $quote, $orderData=[])

◆ submitQuote()

submitQuote ( QuoteEntity  $quote,
  $orderData = [] 
)
protected

Submit quote

Parameters
Quote$quote
array$orderData
Returns
\Magento\Framework\Model\AbstractExtensibleModel|\Magento\Sales\Api\Data\OrderInterface|object
Exceptions

Definition at line 445 of file QuoteManagement.php.

446  {
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);
453  }
454  $this->customerManagement->populateCustomerInfo($quote);
455  }
456  $addresses = [];
457  $quote->reserveOrderId();
458  if ($quote->isVirtual()) {
459  $this->dataObjectHelper->mergeDataObjects(
460  \Magento\Sales\Api\Data\OrderInterface::class,
461  $order,
462  $this->quoteAddressToOrder->convert($quote->getBillingAddress(), $orderData)
463  );
464  } else {
465  $this->dataObjectHelper->mergeDataObjects(
466  \Magento\Sales\Api\Data\OrderInterface::class,
467  $order,
468  $this->quoteAddressToOrder->convert($quote->getShippingAddress(), $orderData)
469  );
470  $shippingAddress = $this->quoteAddressToOrderAddress->convert(
471  $quote->getShippingAddress(),
472  [
473  'address_type' => 'shipping',
474  'email' => $quote->getCustomerEmail()
475  ]
476  );
477  $shippingAddress->setData('quote_address_id', $quote->getShippingAddress()->getId());
479  $order->setShippingAddress($shippingAddress);
480  $order->setShippingMethod($quote->getShippingAddress()->getShippingMethod());
481  }
482  $billingAddress = $this->quoteAddressToOrderAddress->convert(
483  $quote->getBillingAddress(),
484  [
485  'address_type' => 'billing',
486  'email' => $quote->getCustomerEmail()
487  ]
488  );
489  $billingAddress->setData('quote_address_id', $quote->getBillingAddress()->getId());
491  $order->setBillingAddress($billingAddress);
492  $order->setAddresses($addresses);
493  $order->setPayment($this->quotePaymentToOrderPayment->convert($quote->getPayment()));
494  $order->setItems($this->resolveItems($quote));
495  if ($quote->getCustomer()) {
496  $order->setCustomerId($quote->getCustomer()->getId());
497  }
498  $order->setQuoteId($quote->getId());
499  $order->setCustomerEmail($quote->getCustomerEmail());
500  $order->setCustomerFirstname($quote->getCustomerFirstname());
501  $order->setCustomerMiddlename($quote->getCustomerMiddlename());
502  $order->setCustomerLastname($quote->getCustomerLastname());
503 
504  $this->eventManager->dispatch(
505  'sales_model_service_quote_submit_before',
506  [
507  'order' => $order,
508  'quote' => $quote
509  ]
510  );
511  try {
512  $order = $this->orderManagement->place($order);
513  $quote->setIsActive(false);
514  $this->eventManager->dispatch(
515  'sales_model_service_quote_submit_success',
516  [
517  'order' => $order,
518  'quote' => $quote
519  ]
520  );
521  $this->quoteRepository->save($quote);
522  } catch (\Exception $e) {
523  if (!empty($this->addressesToSync)) {
524  foreach ($this->addressesToSync as $addressId) {
525  $this->addressRepository->deleteById($addressId);
526  }
527  }
528  $this->eventManager->dispatch(
529  'sales_model_service_quote_submit_failure',
530  [
531  'order' => $order,
532  'quote' => $quote,
533  'exception' => $e
534  ]
535  );
536  throw $e;
537  }
538  return $order;
539  }
$billingAddress
Definition: order.php:25
$addresses
Definition: address_list.php:7
$quote
$shippingAddress
Definition: order.php:40
$order
Definition: order.php:55

Field Documentation

◆ $accountManagement

$accountManagement
protected

Definition at line 126 of file QuoteManagement.php.

◆ $checkoutSession

$checkoutSession
protected

Definition at line 116 of file QuoteManagement.php.

◆ $customerManagement

$customerManagement
protected

Definition at line 56 of file QuoteManagement.php.

◆ $customerModelFactory

$customerModelFactory
protected

Definition at line 96 of file QuoteManagement.php.

◆ $customerRepository

$customerRepository
protected

Definition at line 91 of file QuoteManagement.php.

◆ $customerSession

$customerSession
protected

Definition at line 121 of file QuoteManagement.php.

◆ $dataObjectHelper

$dataObjectHelper
protected

Definition at line 106 of file QuoteManagement.php.

◆ $eventManager

$eventManager
protected

Definition at line 36 of file QuoteManagement.php.

◆ $orderFactory

$orderFactory
protected

Definition at line 46 of file QuoteManagement.php.

◆ $orderManagement

$orderManagement
protected

Definition at line 51 of file QuoteManagement.php.

◆ $quoteAddressFactory

$quoteAddressFactory
protected

Definition at line 101 of file QuoteManagement.php.

◆ $quoteAddressToOrder

$quoteAddressToOrder
protected

Definition at line 61 of file QuoteManagement.php.

◆ $quoteAddressToOrderAddress

$quoteAddressToOrderAddress
protected

Definition at line 66 of file QuoteManagement.php.

◆ $quoteFactory

$quoteFactory
protected

Definition at line 131 of file QuoteManagement.php.

◆ $quoteItemToOrderItem

$quoteItemToOrderItem
protected

Definition at line 71 of file QuoteManagement.php.

◆ $quotePaymentToOrderPayment

$quotePaymentToOrderPayment
protected

Definition at line 76 of file QuoteManagement.php.

◆ $quoteRepository

$quoteRepository
protected

Definition at line 86 of file QuoteManagement.php.

◆ $quoteValidator

$quoteValidator
protected

Definition at line 41 of file QuoteManagement.php.

◆ $storeManager

$storeManager
protected

Definition at line 111 of file QuoteManagement.php.

◆ $userContext

$userContext
protected

Definition at line 81 of file QuoteManagement.php.


The documentation for this class was generated from the following file: