Question

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 Scandiweb\Migration\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Store\Model\ScopeInterface;

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(
    \Magento\Framework\App\Config\Storage\WriterInterface $configWriter,
    \Magento\Framework\App\State $state,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Theme\Model\Config $config,
    \Magento\Framework\Currency $currency,
    \Magento\Theme\Model\ResourceModel\Theme\CollectionFactory $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(\Magento\Framework\App\Area::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

Was it helpful?

Solution 2

I found a solution. There was dependency problem with \Magento\Framework\Currency $currency. When I run the UpgradeData without this line, it works.

OTHER TIPS

The core supposed to check the en_US instead of just en. Check your codes wherein the locale is specified as en only and changed it to en_US

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top