Oh snap! You are using an old version of browser. Update your browser to get new awesome features. Click for more details.

Magento add custom Price box in layered navigation.





Hello guys, This code basically adds a Price Fileter with custom text Boxes of min. price and max. price in your layered navigation section. Benefit : We can basically adds our custom price range in this boxes and magento will filter the product list based on this search. Hope this code helps other. Cheers ;)


Open file view.phtml
path : app/design/frontend/default/default/template/catalog/layer/view.phtml
 and replace this code.

<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    design
 * @package     base_default
 * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<?php
/**
 * Category layered navigation
 *
 * @see Mage_Catalog_Block_Layer_View
 */
?>




<?php
// get current price filter and set his value in price box
if(isset($_GET['price'])):
    $orignalprice = $_GET['price'];
    $exploadprice = explode("-",$orignalprice);
    $firstboxvalue = $exploadprice[0];
    $secondboxvalue = $exploadprice[1];
endif;
?>
<?php if($this->canShowBlock()): ?>
    <div class="block block-layered-nav">
        <div class="block-title">
            <strong><span><?php echo $this->__('Shop By') ?></span></strong>
        </div>
        <div class="block-content">
            <?php echo $this->getStateHtml() ?>
            <?php if ($this->getLayer()->getState()->getFilters()): ?>
                <div class="actions"><a href="<?php echo $this->getClearUrl() ?>"><?php echo $this->__('Clear All') ?></a></div>
            <?php endif; ?>
            <?php if($this->canShowOptions()):?>
                <p class="block-subtitle"><?php echo $this->__('Shopping Options') ?></p>
                <dl id="narrow-by-list">
                    <?php $_filters = $this->getFilters() ?>
                    <?php foreach ($_filters as $_filter): ?>
                        <?php
                            // get display attribute code
                            $attributeModel = $_filter->getAttributeModel();
                            if($attributeModel) {
                                $attr_code = $attributeModel->getAttributeCode();
                            }
                        ?>
                        <?php if($_filter->getItemsCount()){ ?>
                            <dt><?php echo $this->__($_filter->getName()) ?></dt>
                            <!--<dt><?php /*echo ($this->__($_filter->getName()) == 'Category' ? 'Category': $this->__($_filter->getName())) */?></dt>-->
                            <!--<dd><?php /*echo $_filter->getHtml() */?></dd>-->
                            <dd>
                                <?php
                                // check if price attribute found that time add custom price filter
                                echo ($this->__($attr_code) == 'price' ? '
                                '.$this->__($_filter->getHtml()).
                                '<div class="filtermaindiv">'.str_replace('.','',$strcurrfirst).'
                                <input id="filterformprice" class="filterformprice" type="text" name="filterformprice" value="'.$firstboxvalue.'">
                                <span class="filtertoclass">'.$this->__("To").'</span>'.
                                str_replace('0','',$strcurrsecond).'
                                <input id="filtertoprice" class="filtertoprice" type="text" name="filtertoprice" value="'.$secondboxvalue.'">
                                <button id="filterbtn" name="filterbtn" class="button" type="button"><span><span>'.$this->__("Go").'</span></span></button></div>
                            ': $this->__($_filter->getHtml()))
                                ?>
                            </dd>
                        <?php } else {
                            // add custom else part and add price filter. this part display when price filter is active.
                            if($attr_code == 'price'){?>
                                <dt><?php echo $this->__('Price'); ?></dt>
                                <dd>
                                    <div class="filtermaindiv">
                                        <?php $curr=Mage::helper('core')->currency(''); $curr1=str_replace('0','',$curr); echo str_replace('.','',$curr1);?>
                                        <input id="filterformprice" class="filterformprice" type="text" name="filterformprice" value="<?php echo ($firstboxvalue != '' ? $firstboxvalue: '');?>">
                                        <span class="filtertoclass"><?php echo $this->__('To');?></span>
                                        <?php $curr=Mage::helper('core')->currency('');$curr1=str_replace('.','',$curr); echo str_replace('0','',$curr1);?>
                                        <input id="filtertoprice" class="filtertoprice" type="text" name="filtertoprice" value="<?php echo ($secondboxvalue != '' ? $secondboxvalue: '');?>">
                                        <button id="filterbtn" name="filterbtn" class="button" type="button">
                                            <span><span><?php echo $this->__('Go');?></span></span>
                                        </button>
                                    </div>
                                </dd>
                            <?php }
                        } ?>
                    <?php endforeach; ?>
                </dl>
                <script type="text/javascript">decorateDataList('narrow-by-list')</script>
            <?php
            else:
            // add custom price filter part when all attribute disable that time display.
            ?>
                <p class="block-subtitle"><?php echo $this->__('Shopping Options') ?></p>
                <dl id="narrow-by-list">
                    <dt><?php echo $this->__('Price'); ?></dt>
                    <dd>
                        <div class="filtermaindiv">
                            <?php $curr=Mage::helper('core')->currency(''); $curr1=str_replace('0','',$curr); echo str_replace('.','',$curr1);?>
                            <input id="filterformprice" class="filterformprice" type="text" name="filterformprice" value="<?php echo ($firstboxvalue != '' ? $firstboxvalue: '');?>">
                            <span class="filtertoclass"><?php echo $this->__('To');?></span>
                            <?php $curr=Mage::helper('core')->currency('');$curr1=str_replace('.','',$curr); echo str_replace('0','',$curr1);?>
                            <input id="filtertoprice" class="filtertoprice" type="text" name="filtertoprice" value="<?php echo ($secondboxvalue != '' ? $secondboxvalue: '');?>">
                            <button id="filterbtn" name="filterbtn" class="button" type="button">
                                <span><span><?php echo $this->__('Go');?></span></span>
                            </button>
                        </div>
                    </dd>
                </dl>
            <?php endif; ?>
            <?php
            // get current page url for price filter
            $customfilterbaseurlcheck = $this->helper('core/url')->getCurrentUrl();

            // check if price attribute set so delete attribute and create new url
            list($file, $parameters) = explode('?', $customfilterbaseurlcheck);
            parse_str($parameters, $output);
            unset($output['price']); // remove the make parameter
            $customfilterbaseurl = $file . '?' . http_build_query($output); // Rebuild the url

            // pass custom parameter and url in main url
            $finalfilterurl = $customfilterbaseurl."&";
            ?>
            <script type="text/javascript">
                // prototype js to on click go button that time load url
                $('filterbtn').observe('click', function() {
                    var filterformprice = $('filterformprice').getValue();
                    var filtertoprice = $('filtertoprice').getValue();

                    // check validation not blank.
                    if(filterformprice != "" || filtertoprice != ""){
                        window.location.href = "<?php echo $finalfilterurl;?>price="+filterformprice+"-"+filtertoprice;
                    }
                });
            </script>
        </div>
    </div>
    <?php endif; ?>
And clear cache and reload browser tab. view layer navigation...
hope this code is useful. :)

6 comments

  1. Too good.. No issues... Does as says.. :)

    ReplyDelete
  2. Hi Nice post
    Can you please mark out or mention the exact places in view.phtml to modify or add the code for this logic to work.
    This will be useful for people using themes other than default like me. :-)
    Also i do not prefer replacing the whole code , and wud appreciate the comment additional code and add it in existing file.

    Anyway nice post and helpful

    ReplyDelete
  3. Worked perfectly.. I need to add Discounts % (difference between regular price and special price in percentage range ) in layered navigation filter. If you have any idea please share.

    ReplyDelete
  4. wooow nice coding it's work as it is thanks

    ReplyDelete