3

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!

3 Answers 3

7

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.

3
  • Thanks for the quick reply! If I understand it correctly you have some statics in your code of which 'the other store ID' is fine but 'product ID' needs to be programmatic as there are about 500 product pages to add the link to. Is that possible? Url keys will be the same as I'm creating the respective links to stay within the same locale, ie storeID=3 goes to storeID=1 and 4 goes to 2. Please see my updated code example above. Commented Oct 31, 2013 at 9:10
  • Well the product ID can be dynamic as you loop through the products you want. But you are required to reload the whole product model under another store ID to get the right URLs Commented Oct 31, 2013 at 15:15
  • I think the task might be above my experience and code knowledge as I naively thought I could create the links in template rather swiftly. Recognising that this is a forum for pointing the way to solutions, at least when complex, and not a free developer hotline I'll contact you privately and post the solution here when I find it. Commented Oct 31, 2013 at 17:30
0

I think something like the following should work, too:

$product = Mage::getModel('catalog/product')->setStoreId($id)->load();
echo $product->getProductUrl();

Not sure if this feels hacky.

The solution with the emulation is nice too!

0

Since I now have a working solution to this challenge, here it is to be used for anyone needing the same;

In view.phtml, paste the following in the appropriate place where you want the button to appear. You'll most surely also need to add some styling, change store ids to fit your installation and change the button text to fit your locale(s).

<?php /* Check if storeId = 3 (Swedish locale store view under website id 2) */ ?>
<?php if (Mage::app()->getStore()->getStoreId() == 3): ?>
    <?php /* If the storeId is 3 then output a link to the product page under store id 1 / website id 1 */ ?>
    <?php $product= Mage::getModel('catalog/product')->setStoreId(1)->load($_product->getId()); ?>
    <?php echo '<a class="button cart" href="'.$product->getProductUrl().'" target="blank">Handla nu</a>'; ?>
<?php /* If not storeId 3 then it is 4 (English locale store view under website id 2) */ ?>
<?php else: ?>
    <?php /* If the storeId is 4 then output a link to the product page under store id 2 / website id 1 */ ?>
    <?php $product= Mage::getModel('catalog/product')->setStoreId(2)->load($_product->getId()); ?>
    <?php echo '<a class="button cart" href="'.$product->getProductUrl().'" target="blank">Shop now</a>'; ?>
<?php endif; ?>

Enjoy!

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.