Thursday, February 9, 2012

Displaying Price filter of catgeory or layer filter in magento programmatically

Hi,

If you need to display layer price filter, we can display in 2 type

one is calling default block but issue is some time price will not display if you click price filter or if cache is disabled
Default Price filter

public function priceHtmlnav($maincategoryId) {
$html ='';
$layer = Mage::getModel('catalog/layer');
$category = Mage::getModel('catalog/category')->load($maincategoryId);
if ($category->getId()) {
$origCategory = $layer->getCurrentCategory();
$layer->setCurrentCategory($category);
}
$r=Mage::getModel('catalog/layer_filter_price')
->setLayer($layer);

$range = $r->getPriceRange();
$dbRanges = $r->getRangeItemCounts($range);
$data = array();

foreach ($dbRanges as $index=>$count) {
$data[] = array(
'label' => $this->_renderItemLabel($range, $index),
'value' => $index . ',' . $range,
'count' => $count,
);
}
foreach($data as $d){
$t=str_replace('<span class="price">', "", $d['label']);
$t=str_replace('</span>', "", $t);
$html .= '<li><a href="'.$category->getUrl().'?price='.$d['value']
.'">'.$t.'</a></li>';
}


return $html;
}

and call this funtion

$html=''
$html .= '
<h4>Price</h4>
<ul class="price_grid">';
$html .= $this->priceHtmlnav($maincategoryId);
$html .='</ul>';
echo $html;


To over the default issue i write custom code so even if cache is disabled the link will display



<?php
define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/app/Mage.php';
Mage::app();
$storename='default';
function _renderItemLabel($range, $value)
{
$store = Mage::app()->getStore();
$fromPrice = $store->formatPrice(($value-1)*$range);
$toPrice = $store->formatPrice($value*$range);

return Mage::helper('catalog')->__('%s - %s', $fromPrice, $toPrice);
}


$html = '<h4>Price</h4>
<ul class="price_grid">';


$layer = Mage::getModel('catalog/layer');
$category = Mage::getModel('catalog/category')->load(6);
if ($category->getId()) {
$origCategory = $layer->getCurrentCategory();
$layer->setCurrentCategory($category);
}


$r=Mage::getModel('catalog/layer_filter_price')
->setLayer($layer);

$range = $r->getPriceRange();
$dbRanges = $r->getRangeItemCounts($range);
$data = array();

foreach ($dbRanges as $index=>$count) {
$data[] = array(
'label' => _renderItemLabel($range, $index),
'value' => $index . ',' . $range,
'count' => $count,
);
}
foreach($data as $d){
$html .= '<li><a href="'.$category->getUrl().'?price='.$d['value']
.'">'.$d['label'].'</a></li>';
}
$html .='</ul>';

echo $html;



?>


You can check same post in my magento forum with file attached
http://www.magentocommerce.com/boards/viewthread/272901/

No comments:

Post a Comment