23

So I have the store code and I would really like simply the store id. Ideally I do not want to simply load the store completely as I only need the id.

One idea I had was to load this via a collection but I was wondering if there was another way via a helper or resource.

  • 3
    For me this works perfectly: Mage::app()->getStore($storeCode)->getId(); – Hatef Jul 24 '15 at 15:15
10

Haven't tested this but I seem to remember you can get the ID by using the method loadConfig in the model Mage_Core_Model_Store

After that you can get the ID by calling getId() on the model.

The loadConfig method uses the loaded configuration to retrieve store data

$store = Mage::getConfig()->getNode()->stores->{$code};

Which gives you the following values

  • id
  • code
  • website_id
  • 3
    Strange - for me (Magento 1.9.0.0) this returns NULL. What worked for me was Mage::getConfig()->getNode('stores')->{$code} – Doug McLean Jul 8 '15 at 12:38
  • Or even better : Mage::getConfig()->getNode('stores')->{$code}->{'system'}->{'store'}->{'id'}; – anshuman Apr 21 '16 at 14:26
  • @dougMcLean got the same problem on EE 1.14.1.0, this appears when "config" cache is enabled - looks like Magento cache issue. Sander would you be so kind to edit your answer using Mage::getConfig()->getNode('stores')->{$code}, so the solution works for both enabled/disabled cache cases? – Kate Suykovskaya Dec 13 '16 at 9:58
  • I have multistore, how can i display selected store products using store id, my code -> justpaste.it/6fi1f – Gem Aug 9 '18 at 8:05
21

Mage::getModel('core/store')->load($storeCode, 'code')->getId()

Isn't this the simplest way to do?

  • 2
    It may be simplest but might not be the best for site performance to load the full store when I only need the id. – David Manners Apr 27 '15 at 16:30
  • Ok, makes sense. Then direct SQL in a resource model :) – Zifius Apr 28 '15 at 7:40
5

I don't know if this is something that's peculiar to 1.9.0.0 but I couldn't get either Mage::getConfig()->getNode()->stores->{$code} or Mage::getModel('core/store')->loadConfig($code) (which in fact calls Mage::getConfig()->getNode()->stores->{$code}) to return anything.

The first was close... with a subtle change in form, this gets a result, though it's waaay more info than you need:

$store = Mage::getConfig()->getNode('stores')->{$code}

What worked for me, to get just the store id, was this:

$storeId = Mage::getConfig()->getNode('stores')->{$code}->system->store->id;

...still inefficient, of course, loading all that config info just for one little number. But hey, that's Magento for you.

4

The following should work:

<?php echo Mage::getModel('core/store')->loadConfig($code)->getId(); ?>
  • Thanks for the answer, though this appears to be the same as the earlier one :) – David Manners Jan 28 '15 at 11:05
  • Yes, noticed it after I posted my response :) – Mayers Jan 28 '15 at 11:19

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.