Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AddressUpdate.php
Go to the documentation of this file.
1 <?php
8 
10 {
14  private $gridPool;
15 
19  private $attribute;
20 
26  public function __construct(
27  \Magento\Sales\Model\ResourceModel\GridPool $gridPool,
28  \Magento\Sales\Model\ResourceModel\Attribute $attribute
29  ) {
30  $this->gridPool = $gridPool;
31  $this->attribute = $attribute;
32  }
33 
41  public function afterProcess(
42  \Magento\Sales\Model\ResourceModel\Order\Handler\Address $subject,
43  \Magento\Sales\Model\ResourceModel\Order\Handler\Address $result,
44  \Magento\Sales\Model\Order $order
45  ) {
46  if ($order->hasInvoices()) {
47  $billingAddress = $order->getBillingAddress();
48  $shippingAddress = $order->getShippingAddress();
49 
50  $orderInvoiceHasChanges = false;
52  foreach ($order->getInvoiceCollection()->getItems() as $invoice) {
53  $invoiceAttributesForSave = [];
54 
55  if (!$invoice->getBillingAddressId() && $billingAddress) {
56  $invoice->setBillingAddressId($billingAddress->getId());
57  $invoiceAttributesForSave[] = 'billing_address_id';
58  $orderInvoiceHasChanges = true;
59  }
60 
61  if (!$invoice->getShippingAddressId() && $shippingAddress) {
62  $invoice->setShippingAddressId($shippingAddress->getId());
63  $invoiceAttributesForSave[] = 'shipping_address_id';
64  $orderInvoiceHasChanges = true;
65  }
66 
67  if (!empty($invoiceAttributesForSave)) {
68  $this->attribute->saveAttribute($invoice, $invoiceAttributesForSave);
69  }
70  }
71 
72  if ($orderInvoiceHasChanges) {
73  $this->gridPool->refreshByOrderId($order->getId());
74  }
75  }
76  }
77 }
$billingAddress
Definition: order.php:25
$shippingAddress
Definition: order.php:40
$order
Definition: order.php:55
$invoice
__construct(\Magento\Sales\Model\ResourceModel\GridPool $gridPool, \Magento\Sales\Model\ResourceModel\Attribute $attribute)