SaveHandler.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Model\QuoteRepository;
  7. use Magento\Quote\Api\Data\CartInterface;
  8. use Magento\Customer\Api\AddressRepositoryInterface;
  9. use Magento\Framework\App\ObjectManager;
  10. use Magento\Framework\Exception\NoSuchEntityException;
  11. use Magento\Framework\Exception\InputException;
  12. class SaveHandler
  13. {
  14. /**
  15. * @var \Magento\Quote\Model\Quote\Item\CartItemPersister
  16. */
  17. private $cartItemPersister;
  18. /**
  19. * @var \Magento\Quote\Model\Quote\Address\BillingAddressPersister
  20. */
  21. private $billingAddressPersister;
  22. /**
  23. * @var \Magento\Quote\Model\ResourceModel\Quote
  24. */
  25. private $quoteResourceModel;
  26. /**
  27. * @var \Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentPersister
  28. */
  29. private $shippingAssignmentPersister;
  30. /**
  31. * @var AddressRepositoryInterface
  32. */
  33. private $addressRepository;
  34. /**
  35. * @param \Magento\Quote\Model\ResourceModel\Quote $quoteResource
  36. * @param \Magento\Quote\Model\Quote\Item\CartItemPersister $cartItemPersister
  37. * @param \Magento\Quote\Model\Quote\Address\BillingAddressPersister $billingAddressPersister
  38. * @param \Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentPersister $shippingAssignmentPersister
  39. * @param AddressRepositoryInterface $addressRepository
  40. */
  41. public function __construct(
  42. \Magento\Quote\Model\ResourceModel\Quote $quoteResource,
  43. \Magento\Quote\Model\Quote\Item\CartItemPersister $cartItemPersister,
  44. \Magento\Quote\Model\Quote\Address\BillingAddressPersister $billingAddressPersister,
  45. \Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentPersister $shippingAssignmentPersister,
  46. AddressRepositoryInterface $addressRepository = null
  47. ) {
  48. $this->quoteResourceModel = $quoteResource;
  49. $this->cartItemPersister = $cartItemPersister;
  50. $this->billingAddressPersister = $billingAddressPersister;
  51. $this->shippingAssignmentPersister = $shippingAssignmentPersister;
  52. $this->addressRepository = $addressRepository
  53. ?: ObjectManager::getInstance()->get(AddressRepositoryInterface::class);
  54. }
  55. /**
  56. * Process and save quote data
  57. *
  58. * @param CartInterface $quote
  59. * @return CartInterface
  60. * @throws InputException
  61. * @throws \Magento\Framework\Exception\CouldNotSaveException
  62. * @throws \Magento\Framework\Exception\LocalizedException
  63. */
  64. public function save(CartInterface $quote)
  65. {
  66. /** @var \Magento\Quote\Model\Quote $quote */
  67. // Quote Item processing
  68. $items = $quote->getItems();
  69. if ($items) {
  70. foreach ($items as $item) {
  71. /** @var \Magento\Quote\Model\Quote\Item $item */
  72. if (!$item->isDeleted()) {
  73. $quote->setLastAddedItem($this->cartItemPersister->save($quote, $item));
  74. }
  75. }
  76. }
  77. // Billing Address processing
  78. $billingAddress = $quote->getBillingAddress();
  79. if ($billingAddress) {
  80. if ($billingAddress->getCustomerAddressId()) {
  81. try {
  82. $this->addressRepository->getById($billingAddress->getCustomerAddressId());
  83. } catch (NoSuchEntityException $e) {
  84. $billingAddress->setCustomerAddressId(null);
  85. }
  86. }
  87. $this->billingAddressPersister->save($quote, $billingAddress);
  88. }
  89. $this->processShippingAssignment($quote);
  90. $this->quoteResourceModel->save($quote->collectTotals());
  91. return $quote;
  92. }
  93. /**
  94. * Process shipping assignment
  95. *
  96. * @param \Magento\Quote\Model\Quote $quote
  97. * @return void
  98. * @throws InputException
  99. */
  100. private function processShippingAssignment($quote)
  101. {
  102. // Shipping Assignments processing
  103. $extensionAttributes = $quote->getExtensionAttributes();
  104. if (!$quote->isVirtual() && $extensionAttributes && $extensionAttributes->getShippingAssignments()) {
  105. $shippingAssignments = $extensionAttributes->getShippingAssignments();
  106. if (count($shippingAssignments) > 1) {
  107. throw new InputException(__('Only 1 shipping assignment can be set'));
  108. }
  109. $this->shippingAssignmentPersister->save($quote, $shippingAssignments[0]);
  110. }
  111. }
  112. }