Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AfterAddressSaveObserver.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Customer\Helper\Address as HelperAddress;
24 
30 {
34  const VIV_PROCESSED_FLAG = 'viv_after_address_save_processed';
35 
39  protected $_customerAddress;
40 
44  protected $_coreRegistry;
45 
49  protected $_customerVat;
50 
54  protected $_groupManagement;
55 
59  protected $appState;
60 
64  protected $scopeConfig;
65 
69  protected $messageManager;
70 
74  protected $escaper;
75 
79  private $customerSession;
80 
92  public function __construct(
93  Vat $customerVat,
94  HelperAddress $customerAddress,
95  Registry $coreRegistry,
96  GroupManagementInterface $groupManagement,
101  CustomerSession $customerSession
102  ) {
103  $this->_customerVat = $customerVat;
104  $this->_customerAddress = $customerAddress;
105  $this->_coreRegistry = $coreRegistry;
106  $this->_groupManagement = $groupManagement;
107  $this->scopeConfig = $scopeConfig;
108  $this->messageManager = $messageManager;
109  $this->escaper = $escaper;
110  $this->appState = $appState;
111  $this->customerSession = $customerSession;
112  }
113 
121  public function execute(\Magento\Framework\Event\Observer $observer)
122  {
124  $customerAddress = $observer->getCustomerAddress();
125  $customer = $customerAddress->getCustomer();
126 
127  if (!$this->_customerAddress->isVatValidationEnabled($customer->getStore())
128  || $this->_coreRegistry->registry(self::VIV_PROCESSED_FLAG)
130  ) {
131  return;
132  }
133 
134  try {
135  $this->_coreRegistry->register(self::VIV_PROCESSED_FLAG, true);
136 
137  if ($customerAddress->getVatId() == ''
138  || !$this->_customerVat->isCountryInEU($customerAddress->getCountry())
139  ) {
140  $defaultGroupId = $this->_groupManagement->getDefaultGroup($customer->getStore())->getId();
141  if (!$customer->getDisableAutoGroupChange() && $customer->getGroupId() != $defaultGroupId) {
142  $customer->setGroupId($defaultGroupId);
143  $customer->save();
144  $this->customerSession->setCustomerGroupId($defaultGroupId);
145  }
146  } else {
147  $result = $this->_customerVat->checkVatNumber(
148  $customerAddress->getCountryId(),
149  $customerAddress->getVatId()
150  );
151 
152  $newGroupId = $this->_customerVat->getCustomerGroupIdBasedOnVatNumber(
153  $customerAddress->getCountryId(),
154  $result,
155  $customer->getStore()
156  );
157 
158  if (!$customer->getDisableAutoGroupChange() && $customer->getGroupId() != $newGroupId) {
159  $customer->setGroupId($newGroupId);
160  $customer->save();
161  $this->customerSession->setCustomerGroupId($newGroupId);
162  }
163 
164  $customerAddress->setVatValidationResult($result);
165 
166  if ($this->appState->getAreaCode() == Area::AREA_FRONTEND) {
167  if ($result->getIsValid()) {
169  } elseif ($result->getRequestSuccess()) {
171  } else {
173  }
174  }
175  }
176  } catch (\Exception $e) {
177  $this->_coreRegistry->register(self::VIV_PROCESSED_FLAG, false, true);
178  }
179  }
180 
187  protected function _canProcessAddress($address)
188  {
189  if ($address->getForceProcess()) {
190  return true;
191  }
192 
193  if ($this->_coreRegistry->registry(BeforeAddressSaveObserver::VIV_CURRENTLY_SAVED_ADDRESS) != $address->getId()
194  ) {
195  return false;
196  }
197 
198  $configAddressType = $this->_customerAddress->getTaxCalculationAddressType();
199  if ($configAddressType == AbstractAddress::TYPE_SHIPPING) {
200  return $this->_isDefaultShipping($address);
201  }
202 
203  return $this->_isDefaultBilling($address);
204  }
205 
212  protected function _isDefaultBilling($address)
213  {
214  return $address->getId() && $address->getId() == $address->getCustomer()->getDefaultBilling()
215  || $address->getIsPrimaryBilling()
216  || $address->getIsDefaultBilling();
217  }
218 
225  protected function _isDefaultShipping($address)
226  {
227  return $address->getId() && $address->getId() == $address->getCustomer()->getDefaultShipping()
228  || $address->getIsPrimaryShipping()
229  || $address->getIsDefaultShipping();
230  }
231 
239  protected function addValidMessage($customerAddress, $validationResult)
240  {
241  $message = [
242  (string)__('Your VAT ID was successfully validated.'),
243  ];
244 
245  $customer = $customerAddress->getCustomer();
246  if (!$this->scopeConfig->isSetFlag(HelperAddress::XML_PATH_VIV_DISABLE_AUTO_ASSIGN_DEFAULT)
247  && !$customer->getDisableAutoGroupChange()
248  ) {
249  $customerVatClass = $this->_customerVat->getCustomerVatClass(
250  $customerAddress->getCountryId(),
251  $validationResult
252  );
253  $message[] = $customerVatClass == Vat::VAT_CLASS_DOMESTIC
254  ? (string)__('You will be charged tax.')
255  : (string)__('You will not be charged tax.');
256  }
257 
258  $this->messageManager->addSuccess(implode(' ', $message));
259 
260  return $this;
261  }
262 
270  {
271  $vatId = $this->escaper->escapeHtml($customerAddress->getVatId());
272  $message = [
273  (string)__('The VAT ID entered (%1) is not a valid VAT ID.', $vatId),
274  ];
275 
276  $customer = $customerAddress->getCustomer();
277  if (!$this->scopeConfig->isSetFlag(HelperAddress::XML_PATH_VIV_DISABLE_AUTO_ASSIGN_DEFAULT)
278  && !$customer->getDisableAutoGroupChange()
279  ) {
280  $message[] = (string)__('You will be charged tax.');
281  }
282 
283  $this->messageManager->addError(implode(' ', $message));
284 
285  return $this;
286  }
287 
294  protected function addErrorMessage($customerAddress)
295  {
296  $message = [
297  (string)__('Your Tax ID cannot be validated.'),
298  ];
299 
300  $customer = $customerAddress->getCustomer();
301  if (!$this->scopeConfig->isSetFlag(HelperAddress::XML_PATH_VIV_DISABLE_AUTO_ASSIGN_DEFAULT)
302  && !$customer->getDisableAutoGroupChange()
303  ) {
304  $message[] = (string)__('You will be charged tax.');
305  }
306 
307  $email = $this->scopeConfig->getValue('trans_email/ident_support/email', ScopeInterface::SCOPE_STORE);
308  $message[] = (string)__('If you believe this is an error, please contact us at %1', $email);
309 
310  $this->messageManager->addError(implode(' ', $message));
311 
312  return $this;
313  }
314 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$customer
Definition: customers.php:11
$email
Definition: details.phtml:13
__construct(Vat $customerVat, HelperAddress $customerAddress, Registry $coreRegistry, GroupManagementInterface $groupManagement, ScopeConfigInterface $scopeConfig, ManagerInterface $messageManager, Escaper $escaper, AppState $appState, CustomerSession $customerSession)
__()
Definition: __.php:13
$message
$address
Definition: customer.php:38
$customerAddress