moduleDataSetup = $moduleDataSetup; $this->taxRateRepository = $taxRateRepository; $this->searchCriteriaFactory = $searchCriteriaFactory; $this->regionFactory = $regionFactory; } /** * {@inheritdoc} */ public function apply() { $this->moduleDataSetup->getConnection()->startSetup(); //Update the tax_region_id $taxRateList = $this->taxRateRepository->getList($this->searchCriteriaFactory->create()); /** @var \Magento\Tax\Api\Data\TaxRateInterface $taxRateData */ foreach ($taxRateList->getItems() as $taxRateData) { $regionCode = $this->parseRegionFromTaxCode($taxRateData->getCode()); if ($regionCode) { /** @var \Magento\Directory\Model\Region $region */ $region = $this->regionFactory->create(); $region->loadByCode($regionCode, $taxRateData->getTaxCountryId()); if ($taxRateData->getTaxPostcode() === null) { $taxRateData->setTaxPostcode('*'); } $taxRateData->setTaxRegionId($region->getRegionId()); $this->taxRateRepository->save($taxRateData); } } $this->moduleDataSetup->getConnection()->endSetup(); } /** * {@inheritdoc} */ public static function getDependencies() { return [ UpdateTaxClassAttributeVisibility::class ]; } /** * {@inheritdoc} */ public static function getVersion() { return '2.0.3'; } /** * {@inheritdoc} */ public function getAliases() { return []; } /** * Parse region from tax code. * * @param string $taxCode * @return string */ private function parseRegionFromTaxCode($taxCode) { $result = ''; $parts = explode('-', $taxCode, 3); if (isset($parts[1])) { $result = $parts[1]; } return $result; } }