Magento2: There is no way to get the ID of the current WEBSITE in the Admin Panel

Created on 23 May 2017  路  10Comments  路  Source: magento/magento2


Preconditions


  1. Magento CE 2.0.4
  2. PHP 7.0.15-0ubuntu0.16.04.4 (cli) ( NTS )
  3. mysql Ver 14.14 Distrib 5.7.18, for Linux (x86_64)

Steps to reproduce

  1. URL {...}/admin/admin/system_config/edit/key/.{..}/section/general/website/2/

Expected result

  1. main.DEBUG: "2" {"is_exception":false} [] -For the selected website with ID 2

Actual result

  1. main.DEBUG: "1" {"is_exception":false} []


Task: get the ID of the current WEBSITE in Admin Panel after saving the settings

events.xml
<event name="admin_system_config_changed_section_current_store">

use Magento\Framework\Event\ObserverInterface;

class CurrentStore implements ObserverInterface
{
    protected $logger;
    protected $store;

    public function __construct(
        \Psr\Log\LoggerInterface $loggerInterface,
        \Magento\Store\Model\StoreResolver $storeResolver 
    )
    {
        $this->logger           = $loggerInterface;
        $this->store            = $storeResolver;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {

        try {
            $_websiteId = $this->store->getCurrentStoreId();
            $this->helper->debug(json_encode($_websiteId));
        } catch (Exception $e) {
            $this->logger->critical($e);
        }
    }
}

I get at all selected websites:
main.DEBUG: "1" {"is_exception":false} []

The same situation:

...
    public function __construct(
        \Psr\Log\LoggerInterface $loggerInterface,
       \Magento\Store\Model\StoreManagerInterface $storeManager
    )
    {
        $this->logger           = $loggerInterface;
        $this->store             = $storeManager;
    }
...
        $_websiteId = $this->store->getWebsite()->getId();
        $this->debug(json_encode($_websiteId));

        $_websiteId = $this->store->getStore()->getWebsiteId();
        $this->debug(json_encode($_websiteId));
...

Return for all selected websites:
main.DEBUG: "1" {"is_exception":false} []

Still added:

use Magento\Store\Model\Website;
use Magento\Store\Model\Store;

-It did not help

Cannot Reproduce Clear Description Format is valid

Most helpful comment

(Updated)

class CurrentStore 
{
    public function __construct(
        \Magento\Framework\App\Request\Http $request,
        //...
    ) {
       $websiteId = (int) $request->getParam('website', 0); 
       // ... 
    }
}

It works!

All 10 comments

When you're in the Admin, the "current store" is not the same as the "configuration scope" you selected to apply config settings. So it makes sense that when you request the website ID while in the Admin it always return the same value (EDIT: which happens to be the value of the default store view).

If you want to retrieve the ID for configuration scope selected in the Admin UI, then try checking the request parameters for the value of the current configuration scope. E.g. see the last part of this URL in the system configuration area: http://127.0.0.1:32781/admin/admin/system_config/edit/key/{...}/section/general/store/3/

Steps to reproduce:

  1. Selected in the drop-down list of websites: "Main Website" (1'st on the list)
  2. Redirected to URL {...}/admin/admin/system_config/edit/key/.{..}/section/general/website/1/
  3. Opened my module: Stores > Configuration > MyModule
  4. I pressed the button Save Config and caused an event:
    <event name="admin_system_config_changed_section_current_store">
  5. debug.log > main.DEBUG: "1" {"is_exception":false} []

I repeat the steps with the second website:

  1. Selected in the drop-down list of websites: "Second Website" (2'nd on the list)
  2. Redirected to URL {...}/admin/admin/system_config/edit/key/.{..}/section/general/website/2/
  3. Opened my module: Stores > Configuration > MyModule
  4. I pressed the button Save Config and caused an event:
    <event name="admin_system_config_changed_section_current_store">
  5. debug.log > main.DEBUG: "1" {"is_exception":false} []

The entry in the file debug.log is formed by the methods described above
$_websiteId = $this->store->getWebsite()->getId();
$_websiteId = $this->store->getStore()->getWebsiteId();

I realized, that to use this in this case does not make sense
\Magento\Store\Model\StoreResolver::getCurrentStoreId()
as it is necessary to know the current WEBSITE and not STORE

There remains the question of method:
\Magento\Store\Model\StoreManagerInterface::getWebsite();

How to make them work correctly or there are options for circumventing the situation?

If what you want is to retrieve the configuration scope (website "2" in the second case) then you shouldn't be checking the StoreManager at all. Check the "website" parameter in the request instead (\Magento\Framework\App\Request\Http).

E.g. (not tested):

class CurrentStore 
{
    public function __construct(
        \Magento\Framework\App\Request\Http $request,
        //...
    ) {
       $websiteId = (int) $request->getParam('website', 0);
       // ... 
    }
}

In the process of testing received incorrect values:

Get URL _{...}/admin/admin/system_config/edit/section/{...}/store/2/key/_

...
    public function __construct(
        \Psr\Log\LoggerInterface $loggerInterface,
       \Magento\Store\Model\StoreResolver $storeResolver
    )
    {
        $this->logger           = $loggerInterface;
        $this->store             = $storeResolver;
    }
...
        $_storeId = $this->store->getCurrentStoreId();
        $this->debug(json_encode($_storeId));
...

debug.log > main.DEBUG: "1" {"is_exception":false} []

In the continuation of history

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface');

URL {...}/admin/admin/system_config/edit/section/{...}/store/2/key/

$_getStoreId = $storeManager->getStore()->getId(); // return 1
$_getStoreWebsiteId = $storeManager->getStore()->getWebsiteId(); // return 1
$_getWebsiteId = $storeManager->getWebsite()->getId(); // return 1

URL {...}/admin/admin/system_config/edit/key/.{..}/section/general/website/2/

$_getStoreId = $storeManager->getStore()->getId(); // return 1
$_getStoreWebsiteId = $storeManager->getStore()->getWebsiteId(); // return 1
$_getWebsiteId = $storeManager->getWebsite()->getId(); // return 1

you shouldn't be checking the StoreManager at all

See my comment above for the solution.

(Updated)

class CurrentStore 
{
    public function __construct(
        \Magento\Framework\App\Request\Http $request,
        //...
    ) {
       $websiteId = (int) $request->getParam('website', 0); 
       // ... 
    }
}

It works!

@PauleHan, thank you for your report.
We were not able to reproduce this issue by following the steps you provided. If you'd like to update it, please reopen the issue.
We tested the issue on 2.3.0-dev, 2.2.0, 2.1.9

If what you want is to retrieve the configuration scope (website "2" in the second case) then you shouldn't be checking the StoreManager at all. Check the "website" parameter in the request instead (\Magento\Framework\App\Request\Http).

E.g. (not tested):

class CurrentStore 
{
    public function __construct(
        \Magento\Framework\App\Request\Http $request,
        //...
    ) {
       $websiteId = (int) $request->getParam('website', 0);
       // ... 
    }
}

I had same issue. after migrating multi-stores website from 1.9.3.4 to 2.3.3
Default Stores and Website IDs were returning.

Solution from @gsomoza worked for me.

Was this page helpful?
0 / 5 - 0 ratings