TaxAddressManager.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Model;
  7. use Magento\Customer\Model\Address;
  8. use Magento\Customer\Model\Session;
  9. use Magento\Tax\Api\TaxAddressManagerInterface;
  10. /**
  11. * Class to save address in customer session.
  12. */
  13. class TaxAddressManager implements TaxAddressManagerInterface
  14. {
  15. /**
  16. * Customer session model.
  17. *
  18. * @var Session
  19. */
  20. private $customerSession;
  21. /**
  22. * @param Session $customerSession
  23. */
  24. public function __construct(Session $customerSession)
  25. {
  26. $this->customerSession = $customerSession;
  27. }
  28. /**
  29. * Set default Tax Billing and Shipping address into customer session after address save.
  30. *
  31. * @param Address $address
  32. * @return void
  33. */
  34. public function setDefaultAddressAfterSave(Address $address)
  35. {
  36. if ($this->isDefaultBilling($address)) {
  37. $this->customerSession->setDefaultTaxBillingAddress(
  38. [
  39. 'country_id' => $address->getCountryId(),
  40. 'region_id' => $address->getRegion() ? $address->getRegionId() : null,
  41. 'postcode' => $address->getPostcode(),
  42. ]
  43. );
  44. }
  45. if ($this->isDefaultShipping($address)) {
  46. $this->customerSession->setDefaultTaxShippingAddress(
  47. [
  48. 'country_id' => $address->getCountryId(),
  49. 'region_id' => $address->getRegion() ? $address->getRegionId() : null,
  50. 'postcode' => $address->getPostcode(),
  51. ]
  52. );
  53. }
  54. }
  55. /**
  56. * Set default Tax Shipping and Billing addresses into customer session after login.
  57. *
  58. * @param \Magento\Customer\Api\Data\AddressInterface[] $addresses
  59. * @return void
  60. */
  61. public function setDefaultAddressAfterLogIn(array $addresses)
  62. {
  63. $defaultShippingFound = false;
  64. $defaultBillingFound = false;
  65. foreach ($addresses as $address) {
  66. if ($address->isDefaultBilling()) {
  67. $defaultBillingFound = true;
  68. $this->customerSession->setDefaultTaxBillingAddress(
  69. [
  70. 'country_id' => $address->getCountryId(),
  71. 'region_id' => $address->getRegion() ? $address->getRegionId() : null,
  72. 'postcode' => $address->getPostcode(),
  73. ]
  74. );
  75. }
  76. if ($address->isDefaultShipping()) {
  77. $defaultShippingFound = true;
  78. $this->customerSession->setDefaultTaxShippingAddress(
  79. [
  80. 'country_id' => $address->getCountryId(),
  81. 'region_id' => $address->getRegion() ? $address->getRegionId() : null,
  82. 'postcode' => $address->getPostcode(),
  83. ]
  84. );
  85. }
  86. if ($defaultShippingFound && $defaultBillingFound) {
  87. break;
  88. }
  89. }
  90. }
  91. /**
  92. * Check whether specified billing address is default for customer from address.
  93. *
  94. * @param Address $address
  95. * @return bool
  96. */
  97. private function isDefaultBilling(Address $address)
  98. {
  99. return $address->getId() && $address->getId() == $address->getCustomer()->getDefaultBilling()
  100. || $address->getIsPrimaryBilling()
  101. || $address->getIsDefaultBilling();
  102. }
  103. /**
  104. * Check whether specified shipping address is default for customer from address.
  105. *
  106. * @param Address $address
  107. * @return bool
  108. */
  109. private function isDefaultShipping(Address $address)
  110. {
  111. return $address->getId() && $address->getId() == $address->getCustomer()->getDefaultShipping()
  112. || $address->getIsPrimaryShipping()
  113. || $address->getIsDefaultShipping();
  114. }
  115. }