10

I working with a custom extension related with multiple websites. I face with an issue, define a script code execute in the Admin Panel by getting Website Id. I expected result website_id like 0 (default ID in store_website table). But when I using this function below it always return 1.

/** @var \Magento\Store\Model\StoreManagerInterface */
$this->_storeManager->getStore()->getWebsiteId(); //return 1, expect 0
$this->_storeManager->getWebsite()->getId(); //return 1

My Question: Any way to get current website_id in the Admin Panel or define a function run in adminhtml area?

Note: This function needs to work on both the frontend and backend.

Any help would be appreciated.

  • check first multi website exist or not in your store. – Suresh Chikani Nov 25 '16 at 5:22
17
+50

The general idea is to pass the scope, chosen, through the request parameter.

I think you should use the similar code like in the product edit action:

/** @var \Magento\Store\Model\StoreManagerInterface $storeManager */
$storeManager = $this->_objectManager->get('Magento\Store\Model\StoreManagerInterface');
$storeId = (int) $this->getRequest()->getParam('store', 0);
$store = $storeManager->getStore($storeId);
$storeManager->setCurrentStore($store->getCode());

In this case $store->getWebsiteId() should be actual (selected) website id. But this can be done only in case you use the store block which works in the same way as a standard magento block (to select a store scope):

store selector

Here is my result in the example when I select the default store view (id == 1):

1 store view debug example

and here is for the default global scope (All Store Views, website id is 0):

global scope debug example


Update:

You can add a method like this in the helper and use it where you want:

/**
 * @var \Magento\Framework\App\State
 */
protected $state;

/**
 * @var \Magento\Store\Model\StoreManagerInterface
 */
protected $storeManager;

/**
 * Data constructor.
 * @param Context $context
 * @param \Magento\Framework\App\State $state
 * @param \Magento\Store\Model\StoreManagerInterface $storeManager
 */
public function __construct(
    Context $context,
    \Magento\Framework\App\State $state,
    \Magento\Store\Model\StoreManagerInterface $storeManager
) {
    parent::__construct($context);
    $this->state = $state;
    $this->storeManager = $storeManager;
}

/**
 * @return int
 */
public function resolveCurrentWebsiteId()
{
    if ($this->state->getAreaCode() == \Magento\Framework\App\Area::AREA_ADMINHTML) {
        // in admin area
        /** @var \Magento\Framework\App\RequestInterface $request */
        $request = $this->_request;
        $storeId = (int) $request->getParam('store', 0);
    } else {
        // frontend area
        $storeId = true; // get current store from the store resolver
    }

    $store = $this->storeManager->getStore($storeId);
    $websiteId = $store->getWebsiteId();

    return $websiteId;
}

Result should be like this:

  1. Backend without scope selector - 0

    backed without store selector

  2. Backed with scope selector - selected website id or 0 if nothing selected (All Store Views)

    backend with store selector

  3. Frontend - current website id

    frontend


If you are looking for the short-way on the frontend:

You can use the Magento\Store\Model\StoreResolver class for that purpose:

/**
 * @var \Magento\Store\Model\StoreResolver
 */
private $storeResolver;

/**
 * @param \Magento\Store\Model\StoreResolver $storeResolver
 */
public function __construct(
    \Magento\Store\Model\StoreResolver $storeResolver
) {
    $this->storeResolver = $storeResolver;
}

/**
 * Returns the current store id, if it can be detected or default store id
 * 
 * @return int|string
 */
public function getCurrentStoreId()
{
    return $this->storeResolver->getCurrentStoreId();
}

It is correctly detects the currently selected store view on the frontend area, but useless for the admin area.

  • It's working in the Admin Panel. However, I want to it will get current website in the frontend. This function always return website_id = 0 – Thao Pham Nov 29 '16 at 2:59
  • This function needs to work on both the frontend and backend. – Thao Pham Nov 29 '16 at 3:07
  • @Bill Can you clarify in which place you are trying to get the store id? Is it plugin or maybe observer? – Siarhey Uchukhlebau Nov 29 '16 at 7:34
  • Hi @Siarhey Uchukhlebau, I want to get it from a class. This class may be called from any other class e.g: Controller, Model, Helper, Block, ... – Thao Pham Nov 29 '16 at 8:14
  • @Bill I updated the answer, check please – Siarhey Uchukhlebau Nov 29 '16 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.