/** * @param Observer $observer * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function execute(Observer $observer) { if ($this->moduleManager->isEnabled('Magento_PageCache') && $this->cacheConfig->isEnabled() && $this->taxHelper->isCatalogPriceDisplayAffectedByTax()) { /** @var $customerAddress Address */ $address = $observer->getCustomerAddress(); // Check if the address is either the default billing, shipping, or both if ($this->isDefaultBilling($address)) { $this->customerSession->setDefaultTaxBillingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegionId() : null, 'postcode' => $address->getPostcode()]); } if ($this->isDefaultShipping($address)) { $this->customerSession->setDefaultTaxShippingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegionId() : null, 'postcode' => $address->getPostcode()]); } } }
/** * Address before save event handler * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function execute(\Magento\Framework\Event\Observer $observer) { if ($this->_coreRegistry->registry(self::VIV_CURRENTLY_SAVED_ADDRESS)) { $this->_coreRegistry->unregister(self::VIV_CURRENTLY_SAVED_ADDRESS); } /** @var $customerAddress Address */ $customerAddress = $observer->getCustomerAddress(); if ($customerAddress->getId()) { $this->_coreRegistry->register(self::VIV_CURRENTLY_SAVED_ADDRESS, $customerAddress->getId()); } else { $configAddressType = $this->_customerAddress->getTaxCalculationAddressType(); $forceProcess = $configAddressType == AbstractAddress::TYPE_SHIPPING ? $customerAddress->getIsDefaultShipping() : $customerAddress->getIsDefaultBilling(); if ($forceProcess) { $customerAddress->setForceProcess(true); } else { $this->_coreRegistry->register(self::VIV_CURRENTLY_SAVED_ADDRESS, 'new_address'); } } }
/** * Address after save event handler * * @param \Magento\Framework\Event\Observer $observer * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function execute(\Magento\Framework\Event\Observer $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); if ($customerAddress->getVatId() == '' || !$this->_customerVat->isCountryInEU($customerAddress->getCountry())) { $defaultGroupId = $this->_groupManagement->getDefaultGroup($customer->getStore())->getId(); if (!$customer->getDisableAutoGroupChange() && $customer->getGroupId() != $defaultGroupId) { $customer->setGroupId($defaultGroupId); $customer->save(); } } else { $result = $this->_customerVat->checkVatNumber($customerAddress->getCountryId(), $customerAddress->getVatId()); $newGroupId = $this->_customerVat->getCustomerGroupIdBasedOnVatNumber($customerAddress->getCountryId(), $result, $customer->getStore()); if (!$customer->getDisableAutoGroupChange() && $customer->getGroupId() != $newGroupId) { $customer->setGroupId($newGroupId); $customer->save(); } $customerAddress->setVatValidationResult($result); if ($this->appState->getAreaCode() == Area::AREA_FRONTEND) { if ($result->getIsValid()) { $this->addValidMessage($customerAddress, $result); } elseif ($result->getRequestSuccess()) { $this->addInvalidMessage($customerAddress); } else { $this->addErrorMessage($customerAddress); } } } } catch (\Exception $e) { $this->_coreRegistry->register(self::VIV_PROCESSED_FLAG, false, true); } }
/** * Address after save event handler * * @param \Magento\Framework\Event\Observer $observer * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ 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); if ($customerAddress->getVatId() == '' || !$this->_customerVat->isCountryInEU($customerAddress->getCountry())) { $defaultGroupId = $this->_groupManagement->getDefaultGroup($customer->getStore())->getId(); if (!$customer->getDisableAutoGroupChange() && $customer->getGroupId() != $defaultGroupId) { $customer->setGroupId($defaultGroupId); $customer->save(); } } else { $result = $this->_customerVat->checkVatNumber($customerAddress->getCountryId(), $customerAddress->getVatId()); $newGroupId = $this->_customerVat->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); } }
/** * Address after save event handler * * @param \Magento\Framework\Event\Observer $observer * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function afterAddressSave($observer) { if ($this->moduleManager->isEnabled('Magento_PageCache') && $this->cacheConfig->isEnabled() && $this->weeeHelper->isEnabled()) { /** @var $customerAddress Address */ $address = $observer->getCustomerAddress(); // Check if the address is either the default billing, shipping, or both if ($address->getIsPrimaryBilling() || $address->getIsDefaultBilling()) { $this->customerSession->setDefaultTaxBillingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegionId() : null, 'postcode' => $address->getPostcode()]); } if ($address->getIsPrimaryShipping() || $address->getIsDefaultShipping()) { $this->customerSession->setDefaultTaxShippingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegionId() : null, 'postcode' => $address->getPostcode()]); } } }