Magento 1.7 Product Link – How to Create a Link to a Product in a Different Website/Store View

magento-1.7

I have 2 websites with 2 Store Views per website. 1 website is the 'shop' and 1 is the 'website'. 'Website' does not have cart or customer account functionality but I want to add a button "Shop now!" to the product page that links to the same product in the 'shop' and it should also be the corresponding Store View (Store Views are Swedish and English).

How can I get the link to the same product page as the product page "I'm on" but in another Store View (store id)? I have something like this in mind for the logic of it but I don't know how to finish it and I'd be really grateful for some help;

— UPDATED CODE —

<?php if (Mage::app()->getStore()->getStoreId() == 3): ?>
    <?php $product= Mage::getModel('catalog/product')->setStoreId(1)->load([var product ID]); ?>
    <?php echo '<a href="'.$product->getProductUrl().'">Shop now!</a>'; ?>
<?php else :?>
    <?php $product= Mage::getModel('catalog/product')->setStoreId(2)->load([var product ID]); ?>
    <?php echo '<a href="'.$product->getProductUrl().'">Shop now!</a>'; ?>
<?php endif; ?>

Thanks a bunch in advance and do let me know if I need to clarify anything in my question!

Best Answer

You could build the URL's yourself like you're suggesting but there are a couple of downsides like, for example, having a different URL key on the other store.

I always use the Emulation feature in Magento for these kinds of scenarios.

$appEmulation = Mage::getSingleton('core/app_emulation');
$initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation([the other store ID]);

$product= Mage::getModel('catalog/product')->load([product ID]);
echo '<a href="'.$product->getProductUrl().'">Shop now!</a>';

$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);

This will basically run the code as it was on a different store.

Although I did not test it might also work to set the store ID on the catalog/product model before loading the product.

$product= Mage::getModel('catalog/product')->setStoreId([the other store ID])->load([product ID]);
echo '<a href="'.$product->getProductUrl().'">Shop now!</a>';

Might be worth a try.