5
5
*/
6
6
namespace Magento \Quote \Observer \Frontend \Quote \Address ;
7
7
8
+ use Magento \Customer \Api \AddressRepositoryInterface ;
9
+ use Magento \Customer \Api \Data \CustomerInterfaceFactory ;
10
+ use Magento \Customer \Api \GroupManagementInterface ;
11
+ use Magento \Customer \Helper \Address ;
12
+ use Magento \Customer \Model \Session ;
13
+ use Magento \Customer \Model \Vat ;
14
+ use Magento \Framework \Event \Observer ;
8
15
use Magento \Framework \Event \ObserverInterface ;
16
+ use Magento \Quote \Api \Data \ShippingAssignmentInterface ;
17
+ use Magento \Quote \Model \Quote ;
9
18
10
19
/**
11
20
* Handle customer VAT number on collect_totals_before event of quote address.
15
24
class CollectTotalsObserver implements ObserverInterface
16
25
{
17
26
/**
18
- * @var \Magento\Customer\Api\ AddressRepositoryInterface
27
+ * @var AddressRepositoryInterface
19
28
*/
20
29
private $ addressRepository ;
21
30
22
31
/**
23
- * @var \Magento\Customer\Model\ Session
32
+ * @var Session
24
33
*/
25
34
private $ customerSession ;
26
35
27
36
/**
28
- * @var \Magento\Customer\Helper\ Address
37
+ * @var Address
29
38
*/
30
39
protected $ customerAddressHelper ;
31
40
32
41
/**
33
- * @var \Magento\Customer\Model\ Vat
42
+ * @var Vat
34
43
*/
35
44
protected $ customerVat ;
36
45
@@ -40,36 +49,36 @@ class CollectTotalsObserver implements ObserverInterface
40
49
protected $ vatValidator ;
41
50
42
51
/**
43
- * @var \Magento\Customer\Api\Data\ CustomerInterfaceFactory
52
+ * @var CustomerInterfaceFactory
44
53
*/
45
54
protected $ customerDataFactory ;
46
55
47
56
/**
48
57
* Group Management
49
58
*
50
- * @var \Magento\Customer\Api\ GroupManagementInterface
59
+ * @var GroupManagementInterface
51
60
*/
52
61
protected $ groupManagement ;
53
62
54
63
/**
55
64
* Initialize dependencies.
56
65
*
57
- * @param \Magento\Customer\Helper\ Address $customerAddressHelper
58
- * @param \Magento\Customer\Model\ Vat $customerVat
66
+ * @param Address $customerAddressHelper
67
+ * @param Vat $customerVat
59
68
* @param VatValidator $vatValidator
60
- * @param \Magento\Customer\Api\Data\ CustomerInterfaceFactory $customerDataFactory
61
- * @param \Magento\Customer\Api\ GroupManagementInterface $groupManagement
62
- * @param \Magento\Customer\Api\ AddressRepositoryInterface $addressRepository
63
- * @param \Magento\Customer\Model\ Session $customerSession
69
+ * @param CustomerInterfaceFactory $customerDataFactory
70
+ * @param GroupManagementInterface $groupManagement
71
+ * @param AddressRepositoryInterface $addressRepository
72
+ * @param Session $customerSession
64
73
*/
65
74
public function __construct (
66
- \ Magento \ Customer \ Helper \ Address $ customerAddressHelper ,
67
- \ Magento \ Customer \ Model \ Vat $ customerVat ,
75
+ Address $ customerAddressHelper ,
76
+ Vat $ customerVat ,
68
77
VatValidator $ vatValidator ,
69
- \ Magento \ Customer \ Api \ Data \ CustomerInterfaceFactory $ customerDataFactory ,
70
- \ Magento \ Customer \ Api \ GroupManagementInterface $ groupManagement ,
71
- \ Magento \ Customer \ Api \ AddressRepositoryInterface $ addressRepository ,
72
- \ Magento \ Customer \ Model \ Session $ customerSession
78
+ CustomerInterfaceFactory $ customerDataFactory ,
79
+ GroupManagementInterface $ groupManagement ,
80
+ AddressRepositoryInterface $ addressRepository ,
81
+ Session $ customerSession
73
82
) {
74
83
$ this ->customerVat = $ customerVat ;
75
84
$ this ->customerAddressHelper = $ customerAddressHelper ;
@@ -83,25 +92,23 @@ public function __construct(
83
92
/**
84
93
* Handle customer VAT number if needed on collect_totals_before event of quote address
85
94
*
86
- * @param \Magento\Framework\Event\ Observer $observer
95
+ * @param Observer $observer
87
96
* @return void
88
97
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
89
98
*/
90
- public function execute (\ Magento \ Framework \ Event \ Observer $ observer )
99
+ public function execute (Observer $ observer )
91
100
{
92
- /** @var \Magento\Quote\Api\Data\ ShippingAssignmentInterface $shippingAssignment */
101
+ /** @var ShippingAssignmentInterface $shippingAssignment */
93
102
$ shippingAssignment = $ observer ->getShippingAssignment ();
94
- /** @var \Magento\Quote\Model\ Quote $quote */
103
+ /** @var Quote $quote */
95
104
$ quote = $ observer ->getQuote ();
96
- /** @var \Magento\Quote\Model\ Quote\Address $address */
105
+ /** @var Quote\Address $address */
97
106
$ address = $ shippingAssignment ->getShipping ()->getAddress ();
98
107
99
108
$ customer = $ quote ->getCustomer ();
100
109
$ storeId = $ customer ->getStoreId ();
101
110
102
- if ($ customer ->getDisableAutoGroupChange ()
103
- || false == $ this ->vatValidator ->isEnabled ($ address , $ storeId )
104
- ) {
111
+ if ($ customer ->getDisableAutoGroupChange () || !$ this ->vatValidator ->isEnabled ($ address , $ storeId )) {
105
112
return ;
106
113
}
107
114
$ customerCountryCode = $ address ->getCountryId ();
@@ -135,6 +142,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
135
142
$ quote ->setCustomerGroupId ($ groupId );
136
143
$ this ->customerSession ->setCustomerGroupId ($ groupId );
137
144
$ customer ->setGroupId ($ groupId );
145
+ $ customer ->setEmail ($ customer ->getEmail () ?: $ quote ->getCustomerEmail ());
138
146
$ quote ->setCustomer ($ customer );
139
147
}
140
148
}
0 commit comments