#StackBounty: #module #setup-upgrade #installdata No region found within the locale 'en' while running UpgradeData.php

Bounty: 50

I’m trying to set up 2 stores I created using InstallData script, but When I run php bin/magento setup:upgrade, I’m getting an error No region found within the locale 'en' and the process stops. What could be the problem? Here is my code:

module.xml:

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Scandiweb_Migration" setup_version="1.1.3" />
</config>

<?php

UpgradeData.php:

namespace ScandiwebMigrationSetup;

use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoFrameworkSetupUpgradeDataInterface;
use MagentoStoreModelScopeInterface;

class UpgradeData implements UpgradeDataInterface
{
const GERMAN_THEME_NAME = 'scandi/german';

const DEFAULT_THEME_NAME = 'scandi/default';

protected $state;

protected $config;

protected $themeCollectionFactory;

protected $_storeManager;

protected $configWriter;

protected $_currency;

public function __construct(
    MagentoFrameworkAppConfigStorageWriterInterface $configWriter,
    MagentoFrameworkAppState $state,
    MagentoStoreModelStoreManagerInterface $storeManager,
    MagentoThemeModelConfig $config,
    MagentoFrameworkCurrency $currency,
    MagentoThemeModelResourceModelThemeCollectionFactory $themeCollectionFactory
) {
    $this->state = $state;
    $this->_storeManager = $storeManager;
    $this->_currency = $currency;
    $this->configWriter = $configWriter;
    $this->themeCollectionFactory = $themeCollectionFactory;
    $this->config = $config;
}

public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    $setup->startSetup();

    if (version_compare($context->getVersion(), '1.1.3', '=')) {
        $this->state->setAreaCode(MagentoFrameworkAppArea::AREA_ADMINHTML);
        $this->setDefaultCurrencies();
        $this->setStoreViewCurrencies();
        $this->assignThemeToStore();
        $this->removeSuffix();
        $this->removeSuffix();
    }
    $setup->endSetup();
}

public function setDefaultCurrencies()
{
    $this->configWriter->save('currency/options/base', 'EUR', 'default', 0);
    $this->configWriter->save('currency/options/default', 'EUR', 'default', 0);
    $this->configWriter->save('currency/options/allow', 'EUR,GBP', 'default', 0);
}

public function setStoreViewCurrencies()
{
    $stores = $this->_storeManager->getStores(true, false);
    foreach ($stores as $store) {
        $storeId = $store->getId();

        if ($store->getCode() == 'scandi_en') {
            $this->allowCurrency('GBP', $storeId);
        }

        if ($store->getCode() == 'scandi_de') {
            $this->allowCurrency('EUR', $storeId);
        }
    }
}

public function allowCurrency($currency, $storeId)
{
    $scope = ScopeInterface::SCOPE_STORE;
    $this->configWriter->save('currency/options/allow', $currency, $scope, $storeId);
    $this->configWriter->save('currency/options/default', $currency, $scope, $storeId);
}

public function assignThemeToStore()
{
    $themes = $this->themeCollectionFactory->create()->loadRegisteredThemes();
    foreach ($themes as $theme) {
        if ($theme->getCode() == self::GERMAN_THEME_NAME) {
            $this->config->assignToStore(
                $theme,
                ['3'],
                ScopeInterface::SCOPE_STORES
            );
        }
        if ($theme->getCode() == self::DEFAULT_THEME_NAME) {
            $this->config->assignToStore(
                $theme,
                ['2'],
                ScopeInterface::SCOPE_STORES
            );
        }
    }
}

public function removeSuffix()
{
    $paths = ['catalog/seo/product_url_suffix', 'catalog/seo/category_url_suffix'];
    foreach ($paths as $path) {
        $this->configWriter->save($path, null, 'default', '0');
    }
}
}

And here is an error:

enter image description here


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.