Relation.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Customer address entity resource model
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Customer\Model\ResourceModel\Address;
  9. use Magento\Customer\Model\Address;
  10. use Magento\Customer\Model\Customer;
  11. use Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationInterface;
  12. /**
  13. * Class represents save operations for customer address relations
  14. */
  15. class Relation implements RelationInterface
  16. {
  17. /**
  18. * @var \Magento\Customer\Model\CustomerFactory
  19. */
  20. protected $customerFactory;
  21. /**
  22. * @param \Magento\Customer\Model\CustomerFactory $customerFactory
  23. */
  24. public function __construct(\Magento\Customer\Model\CustomerFactory $customerFactory)
  25. {
  26. $this->customerFactory = $customerFactory;
  27. }
  28. /**
  29. * Process object relations
  30. *
  31. * @param \Magento\Framework\Model\AbstractModel $object
  32. * @return void
  33. */
  34. public function processRelation(\Magento\Framework\Model\AbstractModel $object)
  35. {
  36. /**
  37. * @var $object Address
  38. */
  39. if (!$object->getIsCustomerSaveTransaction() && $object->getId()) {
  40. $customer = $this->customerFactory->create()->load($object->getCustomerId());
  41. $changedAddresses = [];
  42. $changedAddresses = $this->getDefaultBillingChangedAddress($object, $customer, $changedAddresses);
  43. $changedAddresses = $this->getDefaultShippingChangedAddress($object, $customer, $changedAddresses);
  44. if ($changedAddresses) {
  45. $customer->getResource()->getConnection()->update(
  46. $customer->getResource()->getTable('customer_entity'),
  47. $changedAddresses,
  48. $customer->getResource()->getConnection()->quoteInto('entity_id = ?', $customer->getId())
  49. );
  50. }
  51. }
  52. }
  53. /**
  54. * Get default billing changed address
  55. *
  56. * @param Address $object
  57. * @param Customer $customer
  58. * @param array $changedAddresses
  59. * @return array
  60. */
  61. private function getDefaultBillingChangedAddress(
  62. Address $object,
  63. Customer $customer,
  64. array $changedAddresses
  65. ): array {
  66. if ($object->getIsDefaultBilling()) {
  67. $changedAddresses['default_billing'] = $object->getId();
  68. } elseif ($customer->getDefaultBillingAddress()
  69. && $object->getIsDefaultBilling() === false
  70. && (int)$customer->getDefaultBillingAddress()->getId() === (int)$object->getId()
  71. ) {
  72. $changedAddresses['default_billing'] = null;
  73. }
  74. return $changedAddresses;
  75. }
  76. /**
  77. * Get default shipping changed address
  78. *
  79. * @param Address $object
  80. * @param Customer $customer
  81. * @param array $changedAddresses
  82. * @return array
  83. */
  84. private function getDefaultShippingChangedAddress(
  85. Address $object,
  86. Customer $customer,
  87. array $changedAddresses
  88. ): array {
  89. if ($object->getIsDefaultShipping()) {
  90. $changedAddresses['default_shipping'] = $object->getId();
  91. } elseif ($customer->getDefaultShippingAddress()
  92. && $object->getIsDefaultShipping() === false
  93. && (int)$customer->getDefaultShippingAddress()->getId() === (int)$object->getId()
  94. ) {
  95. $changedAddresses['default_shipping'] = null;
  96. }
  97. return $changedAddresses;
  98. }
  99. /**
  100. * Checks if address has chosen as default and has had an id
  101. *
  102. * @deprecated 102.0.1 Is not used anymore due to changes in logic of save of address.
  103. * If address was default and becomes not default than default address id for customer must be
  104. * set to null
  105. * @param \Magento\Framework\Model\AbstractModel $object
  106. * @return bool
  107. */
  108. protected function isAddressDefault(\Magento\Framework\Model\AbstractModel $object)
  109. {
  110. return $object->getId() && ($object->getIsDefaultBilling() || $object->getIsDefaultShipping());
  111. }
  112. }