Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SaveHandler.php
Go to the documentation of this file.
1 <?php
7 
13 
15 {
19  private $cartItemPersister;
20 
24  private $billingAddressPersister;
25 
29  private $quoteResourceModel;
30 
34  private $shippingAssignmentPersister;
35 
39  private $addressRepository;
40 
48  public function __construct(
49  \Magento\Quote\Model\ResourceModel\Quote $quoteResource,
50  \Magento\Quote\Model\Quote\Item\CartItemPersister $cartItemPersister,
51  \Magento\Quote\Model\Quote\Address\BillingAddressPersister $billingAddressPersister,
52  \Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentPersister $shippingAssignmentPersister,
53  AddressRepositoryInterface $addressRepository = null
54  ) {
55  $this->quoteResourceModel = $quoteResource;
56  $this->cartItemPersister = $cartItemPersister;
57  $this->billingAddressPersister = $billingAddressPersister;
58  $this->shippingAssignmentPersister = $shippingAssignmentPersister;
59  $this->addressRepository = $addressRepository
60  ?: ObjectManager::getInstance()->get(AddressRepositoryInterface::class);
61  }
62 
72  public function save(CartInterface $quote)
73  {
75  // Quote Item processing
76  $items = $quote->getItems();
77 
78  if ($items) {
79  foreach ($items as $item) {
81  if (!$item->isDeleted()) {
82  $quote->setLastAddedItem($this->cartItemPersister->save($quote, $item));
83  }
84  }
85  }
86 
87  // Billing Address processing
88  $billingAddress = $quote->getBillingAddress();
89 
90  if ($billingAddress) {
91  if ($billingAddress->getCustomerAddressId()) {
92  try {
93  $this->addressRepository->getById($billingAddress->getCustomerAddressId());
94  } catch (NoSuchEntityException $e) {
95  $billingAddress->setCustomerAddressId(null);
96  }
97  }
98 
99  $this->billingAddressPersister->save($quote, $billingAddress);
100  }
101 
102  $this->processShippingAssignment($quote);
103  $this->quoteResourceModel->save($quote->collectTotals());
104 
105  return $quote;
106  }
107 
115  private function processShippingAssignment($quote)
116  {
117  // Shipping Assignments processing
118  $extensionAttributes = $quote->getExtensionAttributes();
119 
120  if (!$quote->isVirtual() && $extensionAttributes && $extensionAttributes->getShippingAssignments()) {
121  $shippingAssignments = $extensionAttributes->getShippingAssignments();
122 
123  if (count($shippingAssignments) > 1) {
124  throw new InputException(__('Only 1 shipping assignment can be set'));
125  }
126 
127  $this->shippingAssignmentPersister->save($quote, $shippingAssignments[0]);
128  }
129  }
130 }
$billingAddress
Definition: order.php:25
$quote
__()
Definition: __.php:13
save(\Magento\Quote\Api\Data\CartInterface $quote)
$extensionAttributes
Definition: payment.php:22
__construct(\Magento\Quote\Model\ResourceModel\Quote $quoteResource, \Magento\Quote\Model\Quote\Item\CartItemPersister $cartItemPersister, \Magento\Quote\Model\Quote\Address\BillingAddressPersister $billingAddressPersister, \Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentPersister $shippingAssignmentPersister, AddressRepositoryInterface $addressRepository=null)
Definition: SaveHandler.php:48
$items