Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Relation.php
Go to the documentation of this file.
1 <?php
8 
13 {
21  public function processRelation(\Magento\Framework\Model\AbstractModel $customer)
22  {
23  $defaultBillingId = $customer->getData('default_billing');
24  $defaultShippingId = $customer->getData('default_shipping');
25 
27  foreach ($customer->getAddresses() as $address) {
28  if ($address->getData('_deleted')) {
29  if ($address->getId() == $defaultBillingId) {
30  $customer->setData('default_billing', null);
31  }
32 
33  if ($address->getId() == $defaultShippingId) {
34  $customer->setData('default_shipping', null);
35  }
36 
37  $removedAddressId = $address->getId();
38  $address->delete();
39 
40  // Remove deleted address from customer address collection
41  $customer->getAddressesCollection()->removeItemByKey($removedAddressId);
42  } else {
43  $address->setParentId(
44  $customer->getId()
45  )->setStoreId(
46  $customer->getStoreId()
47  )->setIsCustomerSaveTransaction(
48  true
49  )->save();
50 
51  if (($address->getIsPrimaryBilling() ||
52  $address->getIsDefaultBilling()) && $address->getId() != $defaultBillingId
53  ) {
54  $customer->setData('default_billing', $address->getId());
55  }
56 
57  if (($address->getIsPrimaryShipping() ||
58  $address->getIsDefaultShipping()) && $address->getId() != $defaultShippingId
59  ) {
60  $customer->setData('default_shipping', $address->getId());
61  }
62  }
63  }
64 
65  $changedAddresses = [];
66 
67  $changedAddresses['default_billing'] = $customer->getData('default_billing');
68  $changedAddresses['default_shipping'] = $customer->getData('default_shipping');
69 
70  $customer->getResource()->getConnection()->update(
71  $customer->getResource()->getTable('customer_entity'),
72  $changedAddresses,
73  $customer->getResource()->getConnection()->quoteInto('entity_id = ?', $customer->getId())
74  );
75  }
76 }
$customer
Definition: customers.php:11
processRelation(\Magento\Framework\Model\AbstractModel $object)
$address
Definition: customer.php:38