Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ShippingAssignmentProcessor.php
Go to the documentation of this file.
1 <?php
7 
12 use Magento\Quote\Model\ShippingAssignmentFactory;
17 
19 {
23  private $shippingAssignmentFactory;
24 
28  protected $shippingProcessor;
29 
33  protected $cartItemPersister;
34 
38  private $addressRepository;
39 
46  public function __construct(
47  ShippingAssignmentFactory $shippingAssignmentFactory,
50  AddressRepositoryInterface $addressRepository = null
51  ) {
52  $this->shippingAssignmentFactory = $shippingAssignmentFactory;
53  $this->shippingProcessor = $shippingProcessor;
54  $this->cartItemPersister = $cartItemPersister;
55  $this->addressRepository = $addressRepository
56  ?: ObjectManager::getInstance()->get(AddressRepositoryInterface::class);
57  }
58 
65  public function create(CartInterface $quote)
66  {
68  $shippingAddress = $quote->getShippingAddress();
69 
71  $shippingAssignment = $this->shippingAssignmentFactory->create();
72  $shippingAssignment->setItems($quote->getItems());
73  $shippingAssignment->setShipping($this->shippingProcessor->create($shippingAddress));
74 
75  return $shippingAssignment;
76  }
77 
86  public function save(CartInterface $quote, ShippingAssignmentInterface $shippingAssignment)
87  {
89  foreach ($shippingAssignment->getItems() as $item) {
91  if (!$quote->getItemById($item->getItemId()) && !$item->isDeleted()) {
92  $this->cartItemPersister->save($quote, $item);
93  }
94  }
95 
96  $shippingAddress = $shippingAssignment->getShipping()->getAddress();
97 
98  if ($shippingAddress->getCustomerAddressId()) {
99  try {
100  $this->addressRepository->getById($shippingAddress->getCustomerAddressId());
101  } catch (NoSuchEntityException $e) {
102  $shippingAddress->setCustomerAddressId(null);
103  }
104  }
105 
106  $this->shippingProcessor->save($shippingAssignment->getShipping(), $quote);
107  }
108 }
$quote
$shippingAddress
Definition: order.php:40
__construct(ShippingAssignmentFactory $shippingAssignmentFactory, ShippingProcessor $shippingProcessor, CartItemPersister $cartItemPersister, AddressRepositoryInterface $addressRepository=null)