1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Quote\Observer\Frontend\Quote\Address;
use Magento\Framework\Event\ObserverInterface;
class CollectTotalsObserver implements ObserverInterface
{
/**
* @var \Magento\Customer\Api\AddressRepositoryInterface
*/
private $addressRepository;
/**
* @var \Magento\Customer\Model\Session
*/
private $customerSession;
/**
* @var \Magento\Customer\Helper\Address
*/
protected $customerAddressHelper;
/**
* @var \Magento\Customer\Model\Vat
*/
protected $customerVat;
/**
* @var VatValidator
*/
protected $vatValidator;
/**
* @var \Magento\Customer\Api\Data\CustomerInterfaceFactory
*/
protected $customerDataFactory;
/**
* Group Management
*
* @var \Magento\Customer\Api\GroupManagementInterface
*/
protected $groupManagement;
/**
* Initialize dependencies.
*
* @param \Magento\Customer\Helper\Address $customerAddressHelper
* @param \Magento\Customer\Model\Vat $customerVat
* @param VatValidator $vatValidator
* @param \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerDataFactory
* @param \Magento\Customer\Api\GroupManagementInterface $groupManagement
* @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
* @param \Magento\Customer\Model\Session $customerSession
*/
public function __construct(
\Magento\Customer\Helper\Address $customerAddressHelper,
\Magento\Customer\Model\Vat $customerVat,
VatValidator $vatValidator,
\Magento\Customer\Api\Data\CustomerInterfaceFactory $customerDataFactory,
\Magento\Customer\Api\GroupManagementInterface $groupManagement,
\Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
\Magento\Customer\Model\Session $customerSession
) {
$this->customerVat = $customerVat;
$this->customerAddressHelper = $customerAddressHelper;
$this->vatValidator = $vatValidator;
$this->customerDataFactory = $customerDataFactory;
$this->groupManagement = $groupManagement;
$this->addressRepository = $addressRepository;
$this->customerSession = $customerSession;
}
/**
* Handle customer VAT number if needed on collect_totals_before event of quote address
*
* @param \Magento\Framework\Event\Observer $observer
* @return void
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
/** @var \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment */
$shippingAssignment = $observer->getShippingAssignment();
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $observer->getQuote();
/** @var \Magento\Quote\Model\Quote\Address $address */
$address = $shippingAssignment->getShipping()->getAddress();
$customer = $quote->getCustomer();
$storeId = $customer->getStoreId();
if ($customer->getDisableAutoGroupChange()
|| false == $this->vatValidator->isEnabled($address, $storeId)
) {
return;
}
$customerCountryCode = $address->getCountryId();
$customerVatNumber = $address->getVatId();
/** try to get data from customer if quote address needed data is empty */
if (empty($customerCountryCode) && empty($customerVatNumber) && $customer->getDefaultShipping()) {
$customerAddress = $this->addressRepository->getById($customer->getDefaultShipping());
$customerCountryCode = $customerAddress->getCountryId();
$customerVatNumber = $customerAddress->getVatId();
}
$groupId = null;
if (empty($customerVatNumber) || false == $this->customerVat->isCountryInEU($customerCountryCode)) {
$groupId = $customer->getId() ? $this->groupManagement->getDefaultGroup(
$storeId
)->getId() : $this->groupManagement->getNotLoggedInGroup()->getId();
} else {
// Magento always has to emulate group even if customer uses default billing/shipping address
$groupId = $this->customerVat->getCustomerGroupIdBasedOnVatNumber(
$customerCountryCode,
$this->vatValidator->validate($address, $storeId),
$storeId
);
}
if ($groupId) {
$address->setPrevQuoteCustomerGroupId($quote->getCustomerGroupId());
$quote->setCustomerGroupId($groupId);
$this->customerSession->setCustomerGroupId($groupId);
$customer->setGroupId($groupId);
$quote->setCustomer($customer);
}
}
}