1

In one magento installation I have multiple websites, stores and storeviews.

Storeviews are used for internationalisation (i.e. NL, DE, EN etc)

Most of our productranges are available in one, maximum two B2C websites/stores.

Part of this Magento installation is one B2B (resellers) website/store (with two storeviews, one NL, one EN) where all products are listed.

Pricing in this store is hidden using sitewards/b2bprofessional, but guests (not-logged-in) are able to browse the complete catalog.

On the product pages I'd like to show not-logged-in users a link to the other website(s)/storeview(s) (just those storeviews in their language of course) in this same magento installation where they as a B2C customer should/could buy.

I've got the easy part ("if(! Mage::helper('customer')->isLoggedIn()){};") covered ;)

Now for the hard part: How to query Magento for the correct URL(s) of the other storeviews this product is available in?

1

In view.phtml I added the following code as a proof of concept.

Of course it needs some cleanup and I'll probably end up writing a module for this specific functionality, but I'm glad it works as expected.

Any comments / thoughts on how to make this code cleaner/better are appreciated!

<?php 
$current_website_id     = Mage::app()->getWebsite()->getId(); // gets current website ID
$current_locale_code    = Mage::getStoreConfig('general/locale/code'); // gets locale like nl_NL for current store  
$store_ids              = $_product->getStoreIds(); // gets all store IDs this product is available in 
$idPath                 = sprintf('product/%d', $_product->getId()); // Complete idPath for lookup in url_rewrite table 
$coreUrl                = Mage::getModel('core/url_rewrite'); // Load the rewrite model 

foreach ($store_ids as $store_id) { // Iterate through each storeId
    $website_id = Mage::getModel('core/store')->load($store_id)->getWebsiteId(); // Get the website ID of this Store ID
    $locale     = Mage::getStoreConfig('general/locale/code', $store_id); // Get the Locale of this Store ID

    if ($current_website_id !== $website_id) : // We just need the URLs for OTHER websites that contain the same product ... 
        if ($current_locale_code === $locale) : // Only the URL for the SAME locale (no need to send dutch people to an English website... 
            $coreUrl->setStoreId($store_id); // Set the URL Rewrite model to the Store ID currently being processed.. 
            $coreUrl->loadByIdPath($idPath); // Load the Rewritten Url for this product/store ID
            $baseUrl = Mage::app()->getStore($store_id)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK); // get the Base URL for the store ID currently being processed 
            echo $this->__('Also available at ').$baseUrl.$coreUrl->getRequestPath(); // Echo the URL where this product is also available 
        endif; 
    endif; 
}   
?>
0

You can just use getStoreIds() on your product object. It will return an Array that you can iterate through.

If you're in catalog/product/view.phtml you can do it like this:

$store_ids = $_product->getStoreIds();
foreach ($store_ids as $store_id) {
     switch ($store_id) {
        case 1:
              // store id 1 action here
        break;
        case 2:
              // store id 2 action here
        break;
        case 3:
              // store id 3 action here
        break;
    }
}
  • Thx! Currently building on your answer, looks positive so far. I'll post my final code here once done. – Ottonet Jan 23 '15 at 19:21

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.