Magento – Adding external product link for all your products

This tutorial is all about add an external link, either it is a Amazon product link, demo link, or any external link. This script is what you need for your magento. Within this page, you will find the step by step screenshot of creating your own attribute/set, and calling those attribute from your own template.

First you will need to create all the attribute / attribute set for your products. Below is an example of creating an external product links.

Here’s something I had created for my template selling site.

First select the Attribute from the menu, and click on Manage Attributes

Click the Add New icon to create your Attribute.

Now Save the attribute you had just created.

Click the Add New icon to create your Attribute.

Now Save the attribute you had just created.

Click the Add New icon to create your Attribute.


Click on the Side tab “Manage Label/Options”

Then type the following.

Now Save the attribute you had just created.

Click the Add New icon to create your Attribute.

Now Save the attribute you had just created.

Click the Add New icon to create your Attribute.

Now Save the attribute you had just created.

Click on the Add New Set

Here’s the product set that we had created “Product External Link”

Drag and drop the Attributes you just created and drop it into the Product External Link

Like the below image

Now save, and open your code and tweak the the following

Please open list.phtml and view.phtml. This file should be located at your template folder
app/design/frontend/default/[YOUR_THEME]/template/catalog/product/list.phtml
app/design/frontend/default/[YOUR_THEME]/template/catalog/product/view.phtml
Your button phtml may look a bit different, the idea is to get find this similar code and make a tiny tweak to it.

<?php if($_product->isSaleable()): ?>
	<p><button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></p>
<?php else: ?>
	<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>

Put the following code above your button code.

<?php
    $_productForExternalLink = Mage::getModel('catalog/product')->load($_product->getId());
    if (1 == $_productForExternalLink->getData('product_external_link')) {
        $linkTargetAttributeObj = $_productForExternalLink->getResource()->getAttribute("product_external_link_target");
        if ($linkTargetAttributeObj->usesSource()) {
            $link_target = $linkTargetAttributeObj->getSource()->getOptionText($_productForExternalLink->getData('product_external_link_target'));
        }
?>

    <div class="product_external_link">
        <a href="<?php echo $_productForExternalLink->getData('product_external_link_url'); ?>" target="<?php echo $link_target; ?>" style="background:url('<?php echo $_productForExternalLink->getData('product_external_link_image'); ?>') repeat-x; display:inline-block; margin:0 3px 8px;">
            <span><?php echo $_productForExternalLink->getData('product_external_link_text'); ?></span>
        </a>
    </div>

<?php
    } else {
?>

	<?php if($_product->isSaleable()): ?>
        <p><button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></p>
    <?php else: ?>
        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
    <?php endif; ?>
<?php
    }
?>
Related Posts Plugin for WordPress, Blogger...

I hope you have enjoyed this post, be sure to subscribe to my rss feed by click the subscribe button at the top.

Good Luck!

Be Sure to Subscribe to our feed or follow us on twitter to get the latest updates/patches
End of Article, Thanks for reading.
You can leave a response, or trackback from your own site.

22 Responses to “Magento – Adding external product link for all your products”

  1. surf camps says:

    Very nice post. I just stopped by by your website and wanted to say that I have truly enjoyed checking your posts. Any way, I’ll be subscribing to your feed and also I’m hoping you post again soon!

  2. Chris says:

    Thanks for a great article; I wanted to ask if you had any idea whey the following condition would fail?

    $_productForExternalLink = Mage::getModel('catalog/product')->load($_product->getId());
    if(1 == $_productForExternalLink->getData('product_external_link')) {
    //rest of code

    $_productForExternalLink isn't holding any vaule?

    I wonder if it has something to do with my confusion re the 2x different attribute sets you have above.

    eg.

    Product External Link
    Product Demo Link

    Could you possible explain these further? Thanks again.

    • designersandbox says:

      This should be the one that reference with your attribute name:
      ('product_external_link') change this according to your attribute name set at the back-end.

  3. Approved for commercial use?

  4. Ahad says:

    This is exactly what I wanted to do, add a manufacturer link to the product page. The problem I have is that I found that piece of Button code in two places in list.phtml not in view.phtml. I do not receive any error but I dont see any link on the product page. Should I replace the new code in both places in list.phtml? Should I replace it or just add your code above the button code (making in total four button codes in list.phtml). please help!

  5. Ahad says:

    I did not find your email so I am puting my file contents here. Above two parts are my list.phtml file. Below I will add my view.phtml file.

    • Ahad says:

      <?php
      $_helper = $this->helper('catalog/output');
      $_product = $this->getProduct();
      ?>
      <script type="text/javascript">
      var optionsPrice = new Product.OptionsPrice(<?php echo $this->getJsonConfig() ?>);
      </script>
      <div id="messages_product_view"><?php echo $this->getMessagesBlock()->getGroupedHtml() ?></div>
      <div class="product-view">
      <div class="product-essential">
      <form action="<?php echo $this->getAddToCartUrl($_product) ?>" method="post" id="product_addtocart_form"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
      <div class="no-display">
      <input type="hidden" name="product" value="<?php echo $_product->getId() ?>" />
      <input type="hidden" name="related_product" id="related-products-field" value="" />
      </div>

      <div class="product-shop">
      <div class="product-name">
      <h1><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></h1>
      </div>

      <?php /*if ($this->canEmailToFriend()): ?>
      <p class="email-friend"><a href="<?php echo $this->helper('catalog/product')->getEmailToFriendUrl($_product) ?>"><?php echo $this->__('Email to a Friend') ?>
      <?php endif;*/ ?>

      <?php echo $this->getChildHtml('alert_urls') ?>
      <?php echo $this->getChildHtml('product_type_data') ?>
      <?php echo $this->getTierPriceHtml() ?>

      <?php if (!$this->hasOptions()):?>
      <div class="add-to-box">
      <?php if($_product->isSaleable()): ?>
      <?php echo $this->getChildHtml('addtocart') ?>
      <?php if( $this->helper('wishlist')->isAllow() || $_compareUrl=$this->helper('catalog/product_compare')->getAddUrl($_product)): ?>
      <span class="or"><?php echo $this->__('OR') ?></span>
      <?php endif; ?>
      <?php endif; ?>
      <?php echo $this->getChildHtml('addto') ?>
      </div>
      <?php else:?>
      <?php echo $this->getChildHtml('addto') ?>
      <?php endif; ?>

      <?php if ($_product->getShortDescription()):?>
      <div class="short-description">
      <h2><?php echo $this->__('Quick Overview') ?></h2>
      <div class="std"><?php echo $_helper->productAttribute($_product, nl2br($_product->getShortDescription()), 'short_description') ?></div>
      </div>
      <?php endif;?>

      • Ahad says:

        <?php echo $this->getChildHtml('other');?>

        <?php if ($_product->isSaleable() && $this->hasOptions()):?>
        <?php echo $this->getChildChildHtml('container1', '', true, true) ?>
        <?php endif;?>

        </div>

        <div class="product-img-box">
        <?php echo $this->getChildHtml('media') ?>
        </div>

        <div class="clearer"></div>
        <?php if ($_product->isSaleable() && $this->hasOptions()):?>
        <?php echo $this->getChildChildHtml('container2', '', true, true) ?>
        <?php endif;?>

        </form>
        <script type="text/javascript">
        //<![CDATA[
        var productAddToCartForm = new VarienForm('product_addtocart_form');
        productAddToCartForm.submit = function(){
        if (this.validator.validate()) {
        this.form.submit();
        }
        }.bind(productAddToCartForm);
        //]]>
        </script>
        </div>

        <div class="product-collateral">
        <?php echo $this->getChildHtml('description') ?>
        <?php echo $this->getChildHtml('additional') ?>
        <?php echo $this->getReviewsSummaryHtml($_product, false, true)?>
        <?php echo $this->getChildHtml('upsell_products') ?>
        <?php echo $this->getChildHtml('product_additional_data') ?>
        </div>
        </div>

        Thanks for the support.

  6. Paul says:

    cool. of course, and not to nitpick, but it looks like you're not just pasting code above this block. appears to also
    require a closing curly after the button block. This should be obvious to coders, but I imagine a lot of lay people might try this and miss that important detail.

  7. Rob says:

    Hey, thanks for your code. This is working great in view.phtml.
    But it does not work in list.phtml and I can't wrap my head around the mistake.

    I am getting an error:
    Parse error: syntax error, unexpected T_ENDFOREACH in /kunden/300840_82057/…/template/catalog/product/list.phtml on line 101

    Is there a solution for this?

    Thank you for the post! Exactly what I needed!

  8. Bryan says:

    If I ad this to a product will it show on the product display in the Wishlist?
    Thanks!

  9. ravi says:

    Very Good article

    Thanks
    JeevanSathi

  10. sulo says:

    Hi Please help :(
    I've followed your steps but it seems that I'm missing a part.
    I only copied the code to view.phtml so that it is only at the individual product page but it brings an empty product page! http://gulegule.net/magento/category-1/product-1….
    what am I doing wrong (magento 1.4.1.1)

  11. Jodi says:

    I was wondering is there a way to just do a flat out redirect to the external link when you click the product from image view? This seems like it may work but I would love a suggestion about skipping the whole product page and going strait to the external buy site.

  12. prayogo says:

    This is great tutorial. Working good for me. However, I have free downloadable product where visitor can buy with $0 and register to download it. Add to Cart must work as what Magento default has – how to modify it a combination of buynowurl external link and also buy now/add to cart?
    Or simple way to fill buynowurl text attribute so add to cart is also working in our own checkout flow website. Deeply appreciate if you have solution for this.

Leave a Reply