Magento 1.7 – Get Current Website ID in Admin

adminmagento-1.7magento-enterprisescopewebsites

I am in the admin and my structure of the scopes is the following:

-> Default
----> Website A
--------> Store DE
--------> Store FR
----> Website B
--------> Store DE
--------> Store FR

So, now I am simply reloading the page and in the _afterLoad() method, in my custom Model, which extends the core backend model where, which redirects to my _afterLoad method, I want to get the ID of the current website, that I am loading (when I change the Scope from the upper left drop-down menu).

Mage::app()->getWebsite()->getId() does not help, because it always returns 0. I can get the store code, when I am loading one of the store scopes, and from this get their IDs, but the websites don't have codes, so this cannot help me either.
Mage::getModel('core/website')->getWebsite->getId() does not work either.

I know that the IDs of website A and website B are respectively 1 and 2, but I don't know how to acquire them, when I am in the admin.

Best Answer

Try like this:

$websiteCode = Mage::app()->getRequest()->getParam('website');
if ($websiteCode) {
    $website = Mage::getModel('core/website')->load($websiteCode);
    $websiteId = $website->getId();
    //do your magic with $websiteId
}
else {
    //it means you didn't select a website - either you selected a store view or default.
}
Related Topic