2

I have a website which is separated into two stores: Mens and Womens. I am trying to list the categories of both stores in the footer template which is shared by both sites.

Ideally I would have:

MENS

  • -MensCat1
  • -MensCat2
  • -MensCat3
  • -MensCat4

WOMENS

  • -WomensCat1
  • -WomensCat2
  • -WomensCat3
  • -WomensCat4
etc.

I can pull the categories of a particular store with the following:

$children = Mage::getModel('catalog/category')->load(306)->getChildrenCategories();
                foreach($children as $child){
                    echo $child->getName() . " " . $child->getUrl() . "<br/>";
                }

where 306 is the id of the Mens Root Category.

That will give me

  • -MensCat1
  • -MensCat2
  • -MensCat3
  • -MensCat4

The problem is the URLs will be http://{{base_url}}/womens/MensCat1 if I am in the womens store.

Is there a better way to do this so that the URLs are relative to the stores they belong to, or to force the store url to change in the getUrl request?

5

To get the base url from another store there are, as far as I can see, 2 options.

Build the url yourself: building the links yourself by retrieving a specific Store's url and prepending it

$base_url = Mage::app()->getStore($storeId)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);

And then concatenating the category URL to it.

[EDIT: second solution is better (don't use first)]

Store emulation: Run a part of your code emulated under the mensstore to retrieve the right categories and URL's

$appEmulation = Mage::getSingleton('core/app_emulation');
$store_id = 1; // The ID if your men store
$initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($store_id);

$children = Mage::getModel('catalog/category')->load(306)->getChildrenCategories();
foreach($children as $child){
   echo $child->getName() . " " . $child->getUrl() . "<br/>";
}

$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
  • 1
    +1 for the store emulation approach. I would not recommend building the links manually. You might get in trouble. What if a category url is not indexed? – Marius Sep 26 '13 at 7:31
  • I agree, the second approach is probably better. I'm not sure if it's acceptable performance wise since you're loading a whole store just for some links. – Sander Mangel Sep 26 '13 at 7:36
  • I don't think it's that harmful. Anyway...for the first approach you still load a store :) – Marius Sep 26 '13 at 7:37
  • oh shoot, you're right. ok made an edit to point out the second is better ^^ – Sander Mangel Sep 26 '13 at 7:39
  • Hi Sander, thanks for your suggestions. I can't get Method 2 to work for me. It keeps returning the /womens/ url when in the womens store and vice versa. I'm using hard coded ids for mens root category and mens store id but the URLs still show the current store. – James Sep 27 '13 at 0:00

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.