Reputation: 11
I have a magento site and sell various items, one of them being table candles. What I'd like is whenever someone comes to my site via say, Google and lands on a particular product page, to be able to say, click here to view all the candles in this category ie view all the other candles that are in the same category as the one they're looking at.(hope that makes sense!)
Is this something that has to be coded in Magento or is it already available ?
Thanks Nige
Upvotes: 0
Views: 1651
Reputation: 328
Assumptions:
1 - $_product is loaded (as in the catalog/product/view.phtml). If it's not, make sure you load it.
2 - $_product belongs to at least one category. If not, nothing will be shown.
There are two scenarios from the customer end. Either way, you first need to load the Category.
Scenario One: User lands on Product page WITH Category in URL:
<?php $category = Mage::registry('current_category'); ?>
Scenario Two: User lands on Product WITHOUT Category in URL:
<?php
$categories = $_product->getCategoryIds();
$category = Mage::getModel('catalog/category')->load($categories[0]);
?>
Once your category is loaded, then you can get the category URL and Name
<?php
$url = $category->getUrl();
$name = $category->getName();
?>
Now create the link:
<a href="<php echo $url; ?>Click here to view all <?php echo $name; ?></a>";
All Together Now:
<?php
$url = null;
if ( $category = Mage::registry('current_category') ) {
$url = $category->getUrl();
$name = $category->getName();
} elseif ( $categories = $_product->getCategoryIds() ) {
$category = Mage::getModel('catalog/category')->load($categories[0]);
$url = $category->getUrl();
$name = $category->getName();
}
$link = is_null($url) ? '' : "<a href=\"{$url}\">Click here to view all {$name}</a>";
?>
You can place that in your catalog/product/view.phtml file, or anywhere else you need it.
Upvotes: 3