ShippingAssignmentProcessor.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Model\Quote\ShippingAssignment;
  7. use Magento\Framework\Exception\LocalizedException;
  8. use Magento\Quote\Api\Data\CartInterface;
  9. use Magento\Quote\Api\Data\ShippingAssignmentInterface;
  10. use Magento\Framework\Exception\InputException;
  11. use Magento\Quote\Model\ShippingAssignmentFactory;
  12. use Magento\Quote\Model\Quote\Item\CartItemPersister;
  13. use Magento\Customer\Api\AddressRepositoryInterface;
  14. use Magento\Framework\App\ObjectManager;
  15. use Magento\Framework\Exception\NoSuchEntityException;
  16. class ShippingAssignmentProcessor
  17. {
  18. /**
  19. * @var ShippingAssignmentFactory
  20. */
  21. private $shippingAssignmentFactory;
  22. /**
  23. * @var ShippingProcessor
  24. */
  25. protected $shippingProcessor;
  26. /**
  27. * @var CartItemPersister
  28. */
  29. protected $cartItemPersister;
  30. /**
  31. * @var AddressRepositoryInterface
  32. */
  33. private $addressRepository;
  34. /**
  35. * @param ShippingAssignmentFactory $shippingAssignmentFactory
  36. * @param ShippingProcessor $shippingProcessor
  37. * @param CartItemPersister $cartItemPersister
  38. * @param AddressRepositoryInterface $addressRepository
  39. */
  40. public function __construct(
  41. ShippingAssignmentFactory $shippingAssignmentFactory,
  42. ShippingProcessor $shippingProcessor,
  43. CartItemPersister $cartItemPersister,
  44. AddressRepositoryInterface $addressRepository = null
  45. ) {
  46. $this->shippingAssignmentFactory = $shippingAssignmentFactory;
  47. $this->shippingProcessor = $shippingProcessor;
  48. $this->cartItemPersister = $cartItemPersister;
  49. $this->addressRepository = $addressRepository
  50. ?: ObjectManager::getInstance()->get(AddressRepositoryInterface::class);
  51. }
  52. /**
  53. * Create shipping assignment
  54. *
  55. * @param CartInterface $quote
  56. * @return \Magento\Quote\Api\Data\ShippingAssignmentInterface
  57. */
  58. public function create(CartInterface $quote)
  59. {
  60. /** @var \Magento\Quote\Model\Quote $quote */
  61. $shippingAddress = $quote->getShippingAddress();
  62. /** @var \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment */
  63. $shippingAssignment = $this->shippingAssignmentFactory->create();
  64. $shippingAssignment->setItems($quote->getItems());
  65. $shippingAssignment->setShipping($this->shippingProcessor->create($shippingAddress));
  66. return $shippingAssignment;
  67. }
  68. /**
  69. * Save shipping assignment
  70. *
  71. * @param ShippingAssignmentInterface $shippingAssignment
  72. * @param CartInterface $quote
  73. * @return void
  74. * @throws InputException|LocalizedException
  75. */
  76. public function save(CartInterface $quote, ShippingAssignmentInterface $shippingAssignment)
  77. {
  78. /** @var \Magento\Quote\Model\Quote $quote */
  79. foreach ($shippingAssignment->getItems() as $item) {
  80. /** @var \Magento\Quote\Model\Quote\Item $item */
  81. if (!$quote->getItemById($item->getItemId()) && !$item->isDeleted()) {
  82. $this->cartItemPersister->save($quote, $item);
  83. }
  84. }
  85. $shippingAddress = $shippingAssignment->getShipping()->getAddress();
  86. if ($shippingAddress->getCustomerAddressId()) {
  87. try {
  88. $this->addressRepository->getById($shippingAddress->getCustomerAddressId());
  89. } catch (NoSuchEntityException $e) {
  90. $shippingAddress->setCustomerAddressId(null);
  91. }
  92. }
  93. $this->shippingProcessor->save($shippingAssignment->getShipping(), $quote);
  94. }
  95. }