Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ToOrderConverter.php
Go to the documentation of this file.
1 <?php
7 
9 use Magento\Quote\Model\Quote\Address\ToOrder as QuoteAddressToOrder;
11 
13 {
17  protected $quoteAddress;
18 
23 
27  public function __construct(
28  \Magento\Sales\Api\Data\OrderExtensionFactory $orderExtensionFactory
29  ) {
30  $this->orderExtensionFactory = $orderExtensionFactory;
31  }
32 
40  public function beforeConvert(QuoteAddressToOrder $subject, QuoteAddress $address, $additional = [])
41  {
42  $this->quoteAddress = $address;
43  return [$address, $additional];
44  }
45 
52  public function afterConvert(QuoteAddressToOrder $subject, OrderInterface $order)
53  {
55  $taxes = $this->quoteAddress->getAppliedTaxes();
56  $extensionAttributes = $order->getExtensionAttributes();
57  if ($extensionAttributes == null) {
58  $extensionAttributes = $this->orderExtensionFactory->create();
59  }
60  if (!empty($taxes)) {
61  foreach ($taxes as $key => $tax) {
62  $tax['extension_attributes']['rates'] = $tax['rates'];
63  unset($tax['rates']);
64  $taxes[$key] = $tax;
65  }
66  $extensionAttributes->setAppliedTaxes($taxes);
67  $extensionAttributes->setConvertingFromQuote(true);
68  }
69 
70  $itemAppliedTaxes = $this->quoteAddress->getItemsAppliedTaxes();
71  $itemAppliedTaxesModified = [];
72  if (!empty($itemAppliedTaxes)) {
73  foreach ($itemAppliedTaxes as $key => $itemAppliedTaxItem) {
74  if (is_array($itemAppliedTaxItem) && !empty($itemAppliedTaxItem)) {
75  foreach ($itemAppliedTaxItem as $itemAppliedTax) {
76  $itemAppliedTaxesModified[$key]['type'] = $itemAppliedTax['item_type'];
77  $itemAppliedTaxesModified[$key]['item_id'] = $itemAppliedTax['item_id'];
78  $itemAppliedTaxesModified[$key]['associated_item_id'] = $itemAppliedTax['associated_item_id'];
79  $itemAppliedTax['extension_attributes']['rates'] = $itemAppliedTax['rates'];
80  unset($itemAppliedTax['rates']);
81  $itemAppliedTaxesModified[$key]['applied_taxes'][] = $itemAppliedTax;
82  }
83  }
84  }
85  $extensionAttributes->setItemAppliedTaxes($itemAppliedTaxesModified);
86  }
87  $order->setExtensionAttributes($extensionAttributes);
88  return $order;
89  }
90 }
__construct(\Magento\Sales\Api\Data\OrderExtensionFactory $orderExtensionFactory)
$order
Definition: order.php:55
$address
Definition: customer.php:38
$extensionAttributes
Definition: payment.php:22
beforeConvert(QuoteAddressToOrder $subject, QuoteAddress $address, $additional=[])