Skip to content

Commit 493956a

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-58987' into 2.2-develop-pr11
2 parents c5e5d24 + d7f73f8 commit 493956a

File tree

13 files changed

+592
-333
lines changed

13 files changed

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

app/code/Magento/Tax/Observer/AfterAddressSaveObserver.php

Lines changed: 17 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,15 @@
66
namespace Magento\Tax\Observer;
77

88
use Magento\Customer\Model\Address;
9-
use Magento\Customer\Model\Session;
109
use Magento\Framework\Event\Observer;
1110
use Magento\Framework\Event\ObserverInterface;
1211
use Magento\Framework\Module\Manager;
1312
use Magento\PageCache\Model\Config;
13+
use Magento\Tax\Api\TaxAddressManagerInterface;
1414
use Magento\Tax\Helper\Data;
1515

1616
class AfterAddressSaveObserver implements ObserverInterface
1717
{
18-
/**
19-
* @var Session
20-
*/
21-
protected $customerSession;
22-
2318
/**
2419
* @var Data
2520
*/
@@ -40,21 +35,28 @@ class AfterAddressSaveObserver implements ObserverInterface
4035
private $cacheConfig;
4136

4237
/**
43-
* @param Session $customerSession
38+
* Manager to save data in customer session.
39+
*
40+
* @var TaxAddressManagerInterface
41+
*/
42+
private $addressManager;
43+
44+
/**
4445
* @param Data $taxHelper
4546
* @param Manager $moduleManager
4647
* @param Config $cacheConfig
48+
* @param TaxAddressManagerInterface $addressManager
4749
*/
4850
public function __construct(
49-
Session $customerSession,
5051
Data $taxHelper,
5152
Manager $moduleManager,
52-
Config $cacheConfig
53+
Config $cacheConfig,
54+
TaxAddressManagerInterface $addressManager
5355
) {
54-
$this->customerSession = $customerSession;
5556
$this->taxHelper = $taxHelper;
5657
$this->moduleManager = $moduleManager;
5758
$this->cacheConfig = $cacheConfig;
59+
$this->addressManager = $addressManager;
5860
}
5961

6062
/**
@@ -64,57 +66,13 @@ public function __construct(
6466
*/
6567
public function execute(Observer $observer)
6668
{
67-
if ($this->moduleManager->isEnabled('Magento_PageCache') && $this->cacheConfig->isEnabled() &&
68-
$this->taxHelper->isCatalogPriceDisplayAffectedByTax()) {
69+
if ($this->moduleManager->isEnabled('Magento_PageCache')
70+
&& $this->cacheConfig->isEnabled()
71+
&& $this->taxHelper->isCatalogPriceDisplayAffectedByTax()
72+
) {
6973
/** @var $customerAddress Address */
7074
$address = $observer->getCustomerAddress();
71-
72-
// Check if the address is either the default billing, shipping, or both
73-
if ($this->isDefaultBilling($address)) {
74-
$this->customerSession->setDefaultTaxBillingAddress(
75-
[
76-
'country_id' => $address->getCountryId(),
77-
'region_id' => $address->getRegion() ? $address->getRegionId() : null,
78-
'postcode' => $address->getPostcode(),
79-
]
80-
);
81-
}
82-
83-
if ($this->isDefaultShipping($address)) {
84-
$this->customerSession->setDefaultTaxShippingAddress(
85-
[
86-
'country_id' => $address->getCountryId(),
87-
'region_id' => $address->getRegion() ? $address->getRegionId() : null,
88-
'postcode' => $address->getPostcode(),
89-
]
90-
);
91-
}
75+
$this->addressManager->setDefaultAddressAfterSave($address);
9276
}
9377
}
94-
95-
/**
96-
* Check whether specified billing address is default for its customer
97-
*
98-
* @param Address $address
99-
* @return bool
100-
*/
101-
protected function isDefaultBilling($address)
102-
{
103-
return $address->getId() && $address->getId() == $address->getCustomer()->getDefaultBilling()
104-
|| $address->getIsPrimaryBilling()
105-
|| $address->getIsDefaultBilling();
106-
}
107-
108-
/**
109-
* Check whether specified shipping address is default for its customer
110-
*
111-
* @param Address $address
112-
* @return bool
113-
*/
114-
protected function isDefaultShipping($address)
115-
{
116-
return $address->getId() && $address->getId() == $address->getCustomer()->getDefaultShipping()
117-
|| $address->getIsPrimaryShipping()
118-
|| $address->getIsDefaultShipping();
119-
}
12078
}

app/code/Magento/Tax/Observer/CustomerLoggedInObserver.php

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\Event\ObserverInterface;
1212
use Magento\Framework\Module\Manager;
1313
use Magento\PageCache\Model\Config;
14+
use Magento\Tax\Api\TaxAddressManagerInterface;
1415
use Magento\Tax\Helper\Data;
1516

1617
class CustomerLoggedInObserver implements ObserverInterface
@@ -44,25 +45,35 @@ class CustomerLoggedInObserver implements ObserverInterface
4445
*/
4546
private $groupRepository;
4647

48+
/**
49+
* Manager to save data in customer session.
50+
*
51+
* @var TaxAddressManagerInterface
52+
*/
53+
private $addressManager;
54+
4755
/**
4856
* @param GroupRepositoryInterface $groupRepository
4957
* @param Session $customerSession
5058
* @param Data $taxHelper
5159
* @param Manager $moduleManager
5260
* @param Config $cacheConfig
61+
* @param TaxAddressManagerInterface $addressManager
5362
*/
5463
public function __construct(
5564
GroupRepositoryInterface $groupRepository,
5665
Session $customerSession,
5766
Data $taxHelper,
5867
Manager $moduleManager,
59-
Config $cacheConfig
68+
Config $cacheConfig,
69+
TaxAddressManagerInterface $addressManager
6070
) {
6171
$this->groupRepository = $groupRepository;
6272
$this->customerSession = $customerSession;
6373
$this->taxHelper = $taxHelper;
6474
$this->moduleManager = $moduleManager;
6575
$this->cacheConfig = $cacheConfig;
76+
$this->addressManager = $addressManager;
6677
}
6778

6879
/**
@@ -72,45 +83,20 @@ public function __construct(
7283
*/
7384
public function execute(Observer $observer)
7485
{
75-
if ($this->moduleManager->isEnabled('Magento_PageCache') && $this->cacheConfig->isEnabled() &&
76-
$this->taxHelper->isCatalogPriceDisplayAffectedByTax()) {
86+
if ($this->moduleManager->isEnabled('Magento_PageCache')
87+
&& $this->cacheConfig->isEnabled()
88+
&& $this->taxHelper->isCatalogPriceDisplayAffectedByTax()
89+
) {
7790
/** @var \Magento\Customer\Model\Data\Customer $customer */
7891
$customer = $observer->getData('customer');
7992
$customerGroupId = $customer->getGroupId();
8093
$customerGroup = $this->groupRepository->getById($customerGroupId);
8194
$customerTaxClassId = $customerGroup->getTaxClassId();
8295
$this->customerSession->setCustomerTaxClassId($customerTaxClassId);
8396

84-
/** @var \Magento\Customer\Api\Data\AddressInterface[] $addresses */
8597
$addresses = $customer->getAddresses();
8698
if (isset($addresses)) {
87-
$defaultShippingFound = false;
88-
$defaultBillingFound = false;
89-
foreach ($addresses as $address) {
90-
if ($address->isDefaultBilling()) {
91-
$defaultBillingFound = true;
92-
$this->customerSession->setDefaultTaxBillingAddress(
93-
[
94-
'country_id' => $address->getCountryId(),
95-
'region_id' => $address->getRegion() ? $address->getRegion()->getRegionId() : null,
96-
'postcode' => $address->getPostcode(),
97-
]
98-
);
99-
}
100-
if ($address->isDefaultShipping()) {
101-
$defaultShippingFound = true;
102-
$this->customerSession->setDefaultTaxShippingAddress(
103-
[
104-
'country_id' => $address->getCountryId(),
105-
'region_id' => $address->getRegion() ? $address->getRegion()->getRegionId() : null,
106-
'postcode' => $address->getPostcode(),
107-
]
108-
);
109-
}
110-
if ($defaultShippingFound && $defaultBillingFound) {
111-
break;
112-
}
113-
}
99+
$this->addressManager->setDefaultAddressAfterLogIn($addresses);
114100
}
115101
}
116102
}

0 commit comments

Comments
 (0)