55 */
66namespace Magento \Quote \Observer \Frontend \Quote \Address ;
77
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 ;
815use Magento \Framework \Event \ObserverInterface ;
16+ use Magento \Quote \Api \Data \ShippingAssignmentInterface ;
17+ use Magento \Quote \Model \Quote ;
918
1019/**
1120 * Handle customer VAT number on collect_totals_before event of quote address.
1524class CollectTotalsObserver implements ObserverInterface
1625{
1726 /**
18- * @var \Magento\Customer\Api\ AddressRepositoryInterface
27+ * @var AddressRepositoryInterface
1928 */
2029 private $ addressRepository ;
2130
2231 /**
23- * @var \Magento\Customer\Model\ Session
32+ * @var Session
2433 */
2534 private $ customerSession ;
2635
2736 /**
28- * @var \Magento\Customer\Helper\ Address
37+ * @var Address
2938 */
3039 protected $ customerAddressHelper ;
3140
3241 /**
33- * @var \Magento\Customer\Model\ Vat
42+ * @var Vat
3443 */
3544 protected $ customerVat ;
3645
@@ -40,36 +49,36 @@ class CollectTotalsObserver implements ObserverInterface
4049 protected $ vatValidator ;
4150
4251 /**
43- * @var \Magento\Customer\Api\Data\ CustomerInterfaceFactory
52+ * @var CustomerInterfaceFactory
4453 */
4554 protected $ customerDataFactory ;
4655
4756 /**
4857 * Group Management
4958 *
50- * @var \Magento\Customer\Api\ GroupManagementInterface
59+ * @var GroupManagementInterface
5160 */
5261 protected $ groupManagement ;
5362
5463 /**
5564 * Initialize dependencies.
5665 *
57- * @param \Magento\Customer\Helper\ Address $customerAddressHelper
58- * @param \Magento\Customer\Model\ Vat $customerVat
66+ * @param Address $customerAddressHelper
67+ * @param Vat $customerVat
5968 * @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
6473 */
6574 public function __construct (
66- \ Magento \ Customer \ Helper \ Address $ customerAddressHelper ,
67- \ Magento \ Customer \ Model \ Vat $ customerVat ,
75+ Address $ customerAddressHelper ,
76+ Vat $ customerVat ,
6877 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
7382 ) {
7483 $ this ->customerVat = $ customerVat ;
7584 $ this ->customerAddressHelper = $ customerAddressHelper ;
@@ -83,25 +92,23 @@ public function __construct(
8392 /**
8493 * Handle customer VAT number if needed on collect_totals_before event of quote address
8594 *
86- * @param \Magento\Framework\Event\ Observer $observer
95+ * @param Observer $observer
8796 * @return void
8897 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
8998 */
90- public function execute (\ Magento \ Framework \ Event \ Observer $ observer )
99+ public function execute (Observer $ observer )
91100 {
92- /** @var \Magento\Quote\Api\Data\ ShippingAssignmentInterface $shippingAssignment */
101+ /** @var ShippingAssignmentInterface $shippingAssignment */
93102 $ shippingAssignment = $ observer ->getShippingAssignment ();
94- /** @var \Magento\Quote\Model\ Quote $quote */
103+ /** @var Quote $quote */
95104 $ quote = $ observer ->getQuote ();
96- /** @var \Magento\Quote\Model\ Quote\Address $address */
105+ /** @var Quote\Address $address */
97106 $ address = $ shippingAssignment ->getShipping ()->getAddress ();
98107
99108 $ customer = $ quote ->getCustomer ();
100109 $ storeId = $ customer ->getStoreId ();
101110
102- if ($ customer ->getDisableAutoGroupChange ()
103- || false == $ this ->vatValidator ->isEnabled ($ address , $ storeId )
104- ) {
111+ if ($ customer ->getDisableAutoGroupChange () || !$ this ->vatValidator ->isEnabled ($ address , $ storeId )) {
105112 return ;
106113 }
107114 $ customerCountryCode = $ address ->getCountryId ();
@@ -135,6 +142,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
135142 $ quote ->setCustomerGroupId ($ groupId );
136143 $ this ->customerSession ->setCustomerGroupId ($ groupId );
137144 $ customer ->setGroupId ($ groupId );
145+ $ customer ->setEmail ($ customer ->getEmail () ?: $ quote ->getCustomerEmail ());
138146 $ quote ->setCustomer ($ customer );
139147 }
140148 }
0 commit comments