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
9 
11 
15 class Relation implements RelationInterface
16 {
20  protected $customerFactory;
21 
25  public function __construct(\Magento\Customer\Model\CustomerFactory $customerFactory)
26  {
27  $this->customerFactory = $customerFactory;
28  }
29 
36  public function processRelation(\Magento\Framework\Model\AbstractModel $object)
37  {
41  if (!$object->getIsCustomerSaveTransaction() && $this->isAddressDefault($object)) {
42  $customer = $this->customerFactory->create()->load($object->getCustomerId());
43  $changedAddresses = [];
44 
45  if ($object->getIsDefaultBilling()) {
46  $changedAddresses['default_billing'] = $object->getId();
47  }
48 
49  if ($object->getIsDefaultShipping()) {
50  $changedAddresses['default_shipping'] = $object->getId();
51  }
52 
53  if ($changedAddresses) {
54  $customer->getResource()->getConnection()->update(
55  $customer->getResource()->getTable('customer_entity'),
56  $changedAddresses,
57  $customer->getResource()->getConnection()->quoteInto('entity_id = ?', $customer->getId())
58  );
59  }
60  }
61  }
62 
69  protected function isAddressDefault(\Magento\Framework\Model\AbstractModel $object)
70  {
71  return $object->getId() && ($object->getIsDefaultBilling() || $object->getIsDefaultShipping());
72  }
73 }
$customer
Definition: customers.php:11
processRelation(\Magento\Framework\Model\AbstractModel $object)
isAddressDefault(\Magento\Framework\Model\AbstractModel $object)
Definition: Relation.php:69
__construct(\Magento\Customer\Model\CustomerFactory $customerFactory)
Definition: Relation.php:25