/** * Handle customer VAT number if needed on collect_totals_before event of quote address * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function dispatch(\Magento\Framework\Event\Observer $observer) { /** @var \Magento\Sales\Model\Quote\Address $quoteAddress */ $quoteAddress = $observer->getQuoteAddress(); /** @var \Magento\Sales\Model\Quote $quote */ $quote = $quoteAddress->getQuote(); $customerData = $quote->getCustomerData(); $storeId = $customerData->getStoreId(); if ($customerData->getCustomAttribute('disable_auto_group_change') && $customerData->getCustomAttribute('disable_auto_group_change')->getValue() || false == $this->vatValidator->isEnabled($quoteAddress, $storeId)) { return; } $customerCountryCode = $quoteAddress->getCountryId(); $customerVatNumber = $quoteAddress->getVatId(); $groupId = null; if (empty($customerVatNumber) || false == $this->customerHelper->isCountryInEU($customerCountryCode)) { $groupId = $customerData->getId() ? $this->customerHelper->getDefaultCustomerGroupId($storeId) : \Magento\Customer\Service\V1\CustomerGroupServiceInterface::NOT_LOGGED_IN_ID; } else { // Magento always has to emulate group even if customer uses default billing/shipping address $groupId = $this->customerHelper->getCustomerGroupIdBasedOnVatNumber($customerCountryCode, $this->vatValidator->validate($quoteAddress, $storeId), $storeId); } if ($groupId) { $quoteAddress->setPrevQuoteCustomerGroupId($quote->getCustomerGroupId()); $quote->setCustomerGroupId($groupId); $customerData = $this->customerBuilder->mergeDataObjectWithArray($customerData, array('group_id' => $groupId)); $quote->setCustomerData($customerData); } }
/** * Address after save event handler * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function afterAddressSave($observer) { /** @var $customerAddress Address */ $customerAddress = $observer->getCustomerAddress(); $customer = $customerAddress->getCustomer(); if (!$this->_customerAddress->isVatValidationEnabled($customer->getStore()) || $this->_coreRegistry->registry(self::VIV_PROCESSED_FLAG) || !$this->_canProcessAddress($customerAddress)) { return; } try { $this->_coreRegistry->register(self::VIV_PROCESSED_FLAG, true); /** @var $customerHelper \Magento\Customer\Helper\Data */ $customerHelper = $this->_customerData; if ($customerAddress->getVatId() == '' || !$this->_customerData->isCountryInEU($customerAddress->getCountry())) { $defaultGroupId = $customerHelper->getDefaultCustomerGroupId($customer->getStore()); if (!$customer->getDisableAutoGroupChange() && $customer->getGroupId() != $defaultGroupId) { $customer->setGroupId($defaultGroupId); $customer->save(); } } else { $result = $customerHelper->checkVatNumber($customerAddress->getCountryId(), $customerAddress->getVatId()); $newGroupId = $customerHelper->getCustomerGroupIdBasedOnVatNumber($customerAddress->getCountryId(), $result, $customer->getStore()); if (!$customer->getDisableAutoGroupChange() && $customer->getGroupId() != $newGroupId) { $customer->setGroupId($newGroupId); $customer->save(); } $customerAddress->setVatValidationResult($result); } } catch (\Exception $e) { $this->_coreRegistry->register(self::VIV_PROCESSED_FLAG, false, true); } }