Thursday, July 1, 2010

Displaying Product's Filter attributes in breadcrumbs.

This is a wonder, why Magento does not allow displaying product's Filter attributes in breadcrumbs.
Thats why I need to create custom code.
Here is the way:
<?php if($crumbs && is_array($crumbs)): ?>
<?php
$current_url = $this->helper('core/url')->getCurrentUrl();
$link_pass = strpos($current_url, '?');
?>
<div class="breadcrumbs">

<ul>

<?php foreach($crumbs as $_crumbName=>$_crumbInfo): ?>

<li class="<?php echo $_crumbName ?>">

<?php if($_crumbInfo['link']): ?>

<a href="<?php echo $_crumbInfo['link'] ?>" title="<?php echo $this->htmlEscape($_crumbInfo['title']) ?>"><?php echo $this->htmlEscape($_crumbInfo['label']) ?></a>

<?php elseif($_crumbInfo['last']): ?>
<?php if($link_pass){ ?><a href="<?php echo $_crumbInfo['link'] ?>" title="<?php echo $this->htmlEscape($_crumbInfo['title']) ?>"><?php }?>
<?php echo $this->htmlEscape($_crumbInfo['label']) ?>
<?php if($link_pass){ ?></a><?php }?>
<?php else: ?>

<?php echo $this->htmlEscape($_crumbInfo['label']) ?>

<?php endif; ?>


</li>

<?php endforeach; ?>

<?php 
if($link_pass){
$final_filter = '';
$pass = 0;
$filter_link = explode("?", $current_url); 
$filter_link = $filter_link[1];
$filter_attr = explode("&",$filter_link);
$filter_len = count($filter_attr);
?>
<li>
<?php 
if(Mage::app()->getRequest()->getParam('cat')){
$id = Mage::app()->getRequest()->getParam('cat');
$_category = Mage::getModel('catalog/category')->load($id);
$cat_url = Mage::getModel("catalog/category")->load($id)->getUrl();
$cat_name = $_category->getName(); 
if($filter_len > 1) { ?>
<a href="<?php echo $cat_url; ?>" style="margin-right:4px;">
<?php } ?>
<?php echo $cat_name; ?>
<?php if(count($filter_link) != 0) { ?>
</a>
<?php } 
}
?>
</li>
<?php
for($i = 0; $i < $filter_len; $i++)
{
$filter_pos = strpos($filter_attr[$i], '=');
$filter_attr1 = substr($filter_attr[$i], 0, $filter_pos);
if( $filter_attr1 != 'cat'){
$filter_attr1 = '<b style="text-transform:capitalize;">'.str_replace("_", " ", $filter_attr1).'</b>';
$final_filter =  $final_filter .", ".$filter_attr1;
$pass = 1;
} 
}
if($pass)    
echo "<li> [".substr($final_filter, 2, strlen($final_filter))." ]</li>";

}
?>
</ul>
</div>

<?php endif; ?>
Everything is done in breadcrumbs.phtml file.

5 comments:

  1. Hi, very nice, I was looking for something like this!

    I see you get the name from the URL. Do you think it's also possible to get the label from the layered navigation?

    for example:
    in layered there's an option:
    SIZE
    baby (50-100)
    Kid (130-200)

    this is a size range. in the Url there will be somthing like: mypage.html?size=12

    In the breadcrumb I would like to see:
    home / clothing / kid (130-200 / etc.

    ReplyDelete
  2. I'm sorry for the late response, Thanks for your suggestions, I shall work with these.

    ReplyDelete
  3. Hi Wahid,
    Thank you for the idea!
    I used firstly your solution then i searched for a solution to uses filters' names instead of codes.
    I found the solutions and i shared with you, perhaps you'll need it one day ;)

    ----
    $layer = Mage::getSingleton('catalog/layer');
    $filters = $layer->getState()->getFilters();
    $count_filters = count($filters);
    $i;
    foreach ($filters as $_filter) {
    $i++;
    $fit = $_filter->getFilter();
    echo "<li>" . $this->stripTags($_filter->getLabel()) . " ";
    if($i != $count_filters) {
    echo "<span> > </span&gt";
    }
    echo "</li>";
    }
    ----

    My best regards

    ReplyDelete
  4. Sorry I could not update this post.

    Yes you absolutely right.

    Thanks for sharing.

    ReplyDelete
  5. If we want hyperlink to every attributes,how to do it??

    ReplyDelete