How to Display a Product Attribute URL Link on the Product Page
Ever wanted to provide more information on the product page by linking to another page or even linking to an external site?
Examples include linking to the manufacturer’s product page, technical information, competitors’ pages, or any page within your site.
You can create product attributes that display a link or button on the product page or even the product list.
Say you want to provide a link to the product manufacturer’s technical page.
Step 1: Create a product attribute like “techpage”, using text input and Admin validation set to “URL“.
Step 2: Assign this attribute to the appropriate attribute set(s). The attribute will show up in the product admin page, depending on which group the attribute is located, in the attribute set “tree”.
Step 3: Configure your attribute for each product. In the product admin page, paste the URL in your attribute’s text box. If you set validation to “URL“, this will require the value to begin with http://.
Step 4: Display the link in the catalog. There are many ways to do this, depending on individual site setup. My preference is to use a custom theme that will not be overwritten with Magento upgrades.
Open the ‘template’ file you wish to edit. For the product page, the file is app/design/frontend/default/yourtheme/template/catalog/product/view.phtml
Insert the following link where you want the URL displayed:
<a href="<?php echo $this->htmlEscape($_product->getTechpage()) ?>"><?php echo $this->__('Technical Documentation') ?></a>
If you place the link within a div, you can use css to position the element and style it as a block or button.
TIP: This code is sensitive to the input method used for this link attribute. If the attribute requires validation as a url, Magento will require the attribute’s value to begin with “http://” and this code will work as-is.
However, if you simply want to input the link name “targetsite.com” vs. “http://targetsite.com” when configuring each product, it’s necessary to modify the code:
This is because, if the attribute value is not a valid url, the “http://” makes the difference between displaying an external link vs. appending the link to the end of your base-url.
with: www.targetsite.com without: www.yourstore.com/www.targetsite.com
This trick can come in handy if you really want to simplify product configuration. For example, if the attribute simply had a value of “targetsite”, the following code would generate the link http://www.targetsite.com
<a href="http://www.<?php echo $this->htmlEscape($_product->getBuylink()) ?>.com"><?php echo $this->__('Buy Link') ?></a>
You might also want to link to an internal page - for example, a cms page or category that features the product’s manufacturer.
Follow the steps above EXCEPT set up the attribute to not require validation. You want the value for the attribute to be simple text.
Using the first example of this code, any value for your attribute will produce: http://www.yoursite.com/anyvalue
You can customize the path by adding the appropriate text outside the parentheses, as demonstrated above.