24

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.

1
  • 3
    For me this works perfectly: Mage::app()->getStore($storeCode)->getId();
    – hatef
    Commented Jul 24, 2015 at 15:15

4 Answers 4

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
4
  • 3
    Strange - for me (Magento 1.9.0.0) this returns NULL. What worked for me was Mage::getConfig()->getNode('stores')->{$code} Commented Jul 8, 2015 at 12:38
  • Or even better : Mage::getConfig()->getNode('stores')->{$code}->{'system'}->{'store'}->{'id'};
    – anshuman
    Commented Apr 21, 2016 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? Commented Dec 13, 2016 at 9:58
  • I have multistore, how can i display selected store products using store id, my code -> justpaste.it/6fi1f
    – Gem
    Commented Aug 9, 2018 at 8:05
22

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

Isn't this the simplest way to do?

2
  • 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. Commented Apr 27, 2015 at 16:30
  • Ok, makes sense. Then direct SQL in a resource model :)
    – Zifius
    Commented Apr 28, 2015 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(); ?>
2
  • Thanks for the answer, though this appears to be the same as the earlier one :) Commented Jan 28, 2015 at 11:05
  • Yes, noticed it after I posted my response :) Commented Jan 28, 2015 at 11:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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