customerSession = $customerSession; $this->httpContext = $httpContext; $this->weeeTax = $weeeTax; $this->taxHelper = $taxHelper; $this->weeeHelper = $weeeHelper; $this->moduleManager = $moduleManager; $this->cacheConfig = $cacheConfig; $this->storeManager = $storeManager; $this->scopeConfig = $scopeConfig; } /** * @param \Magento\Framework\App\ActionInterface $subject * @param \Magento\Framework\App\RequestInterface $request * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function beforeDispatch( \Magento\Framework\App\ActionInterface $subject, \Magento\Framework\App\RequestInterface $request ) { if (!$this->weeeHelper->isEnabled() || !$this->customerSession->isLoggedIn() || !$this->moduleManager->isEnabled('Magento_PageCache') || !$this->cacheConfig->isEnabled()) { return; } $basedOn = $this->taxHelper->getTaxBasedOn(); if ($basedOn != 'shipping' && $basedOn != 'billing') { return; } $weeeTaxRegion = $this->getWeeeTaxRegion($basedOn); $websiteId = $this->storeManager->getStore()->getWebsiteId(); $countryId = $weeeTaxRegion['countryId']; $regionId = $weeeTaxRegion['regionId']; if (!$countryId && !$regionId) { // country and region does not exist return; } elseif ($countryId && !$regionId) { // country exist and region does not exist $regionId = 0; $exist = $this->weeeTax->isWeeeInLocation( $countryId, $regionId, $websiteId ); } else { // country and region exist $exist = $this->weeeTax->isWeeeInLocation( $countryId, $regionId, $websiteId ); if (!$exist) { // just check the country for weee $regionId = 0; $exist = $this->weeeTax->isWeeeInLocation( $countryId, $regionId, $websiteId ); } } if ($exist) { $this->httpContext->setValue( 'weee_tax_region', ['countryId' => $countryId, 'regionId' => $regionId], 0 ); } } /** * @param string $basedOn * @return array */ protected function getWeeeTaxRegion($basedOn) { $countryId = null; $regionId = null; $defaultCountryId = $this->scopeConfig->getValue( \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_COUNTRY, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, null ); $defaultRegionId = $this->scopeConfig->getValue( \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_REGION, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, null ); if ($basedOn == 'shipping') { $defaultShippingAddress = $this->customerSession->getDefaultTaxShippingAddress(); if (empty($defaultShippingAddress)) { $countryId = $defaultCountryId; $regionId = $defaultRegionId; } else { $countryId = $defaultShippingAddress['country_id']; $regionId = $defaultShippingAddress['region_id']; } } elseif ($basedOn == 'billing') { $defaultBillingAddress = $this->customerSession->getDefaultTaxBillingAddress(); if (empty($defaultBillingAddress)) { $countryId = $defaultCountryId; $regionId = $defaultRegionId; } else { $countryId = $defaultBillingAddress['country_id']; $regionId = $defaultBillingAddress['region_id']; } } return ['countryId' => $countryId, 'regionId' => $regionId]; } }