6

As the title says. I don't want the ID of the current website, I want the ID of any website by supplying only that websites name.

I have this code:

$siteModel = Mage::getResourceModel( 'core/website_collection' )->addFieldToFilter( 'name', $site )->getFirstItem();
$siteId = $siteModel->getId();

But $siteId ends up empty.

If all i have the name of a website, then how can i get its ids?

2

You need debug the collection by ->getSelect()->__toString()

for checking it give right value or not.

    $siteModelCollection = Mage::getResourceModel( 'core/website_collection' )->addFieldToFilter( 'name', $site );
// print the Query
    echo $Query = $siteModelCollection ->getSelect()->__toString();
    echo $siteId =$siteModelCollection->getFirstItem()->getId();

Update:

Use load():

for this case you need to call load() function of resource model of core/website for getting proper collection

Now you can get id of first store by using below code:

$siteModelCollection = Mage::getResourceModel('core/website_collection')->addFieldToFilter( 'name', $site)->load();
echo $Query = $siteModelCollection ->getSelect()->__toString();
echo $siteId =$siteModelCollection->getFirstItem()->getId();
  • The first echo returns SELECT main_table.* FROM core_website` AS main_table WHERE (name = 'base')` - this seems ok to me? Does it seem ok to you? The second echo returns an empty string. – Jimmery Sep 3 '15 at 16:05
  • adding ->load() worked perfectly! thank you very much! – Jimmery Sep 3 '15 at 16:22
  • load() should be after filter – Anthony Dec 12 '17 at 19:59
  • ya. right[................ – Amit Bera Dec 12 '17 at 21:02
3

Websites are already loaded on startup, so there is no need for additional queries. You can and should use:

$websites = Mage::app()->getWebsites();

to receive them. Then to look up by name:

foreach ($websites as $id => $website) {
    if ($website->getName() === $name) {
        return $id;
    }
}

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.