最近,有个客户想要让所有“筛选器”一直显示。例如,当你想要通过颜色筛选器筛选产品时,发现所有的筛选器都在,那么你就可以轻松地按其它条件筛选而不必返回类别页。

分层导航的文件默认在app/design/frontend/base/default/template/catalog/layer/下。分层导航的模版文件是view.phtml,用来显示筛选器。筛选器状态模版是state.phtml,当我们点击一个筛选器时,他响应结果。我们就要编辑state.phtml。将这个文件拷贝到你的包或主题中。

这是默认的state.phtml文件:

<?php $_filters = $this->getActiveFilters() ?>
<?php if(!empty($_filters)): ?>
<div class="currently">
    <p class="block-subtitle"><?php echo $this->__('Currently Shopping by:') ?></p>
    <ol>
    <?php foreach ($_filters as $_filter): ?>
        <li>
            <a href="<?php echo $_filter->getRemoveUrl() ?>" title="<?php echo $this->__('Remove This Item') ?>" class="btn-remove"><?php echo $this->__('Remove This Item') ?></a>
            <span class="label"><?php echo $this->__($_filter->getName()) ?>:</span> <?php echo $this->stripTags($_filter->getLabel()) ?>
        </li>
    <?php endforeach; ?>
    </ol>
    <div class="actions"><a href="<?php echo $this->getClearUrl() ?>"><?php echo $this->__('Clear All') ?></a></div>
</div>
<?php endif; ?>

我们将要在“currently”div块前面添加当前类别的url通道。代码如下:

<?php $obj = new Mage_Catalog_Block_Navigation(); ?>
<?php $_current_category=$obj->getCurrentCategory()->getUrlPath(); ?> //getting url path of current category
<?php $subs = $obj->getCurrentCategory()->getAllChildren(true); ?> //getting ids of subcategories of current category

现在,我们编辑 “currently”div块。我将它重新命名为state。因为它不再显示当前筛选器。

<?php if(!empty($_filters)): ?>
<div class="state">
    <p class="block-subtitle"><?php echo $this->__('Currently Shopping by:') ?></p>
    <dl>
    <?php foreach ($_filters as $_filter): ?>
            <dd>
                <ol>
                    <?php $attributemodel=$_filter->filter->_data["attribute_model"]; ?>    // getting attribute model that has all filter options in it from currently active filter
                    <?php $attroptions=$attributemodel->getSource()->getAllOptions();?>   // getting attribute options (filters) from attribute model
                    <?php $_categ= Mage::getModel('catalog/category');?>   // object  containing all categories and their information
 
                <?php foreach($subs as $cat_id): ?>
                        <?php $_categ->load($cat_id)?>           //get the subcategory you need
                        <?php $collection = $_categ->getProductCollection(); ?>  //get the product collection (object containing all product information)
                        <?php $collection->addAttributeToSelect('color')?>   // get the desired attribute
                <?php endforeach; ?>

接下来,我们需要提取属性模型的信息和组合链接。每一个属性选项 ($attroptions)都包含属性值和属性标签。

<?php foreach($attroptions as $attr): ?>   // get value and label of each attribute
                        <?php $count=0; ?>
                        <?php if($attr["value"]!=""): ?>
                               <?php $val=$attr["value"] ?>
                                <?php $collection->addFieldToFilter(array(array('attribute'=>'themes','gt'=>10)))?>  // collection of attribute values and labels for all values

//greater then 10 (in this case attribute values range was 18-39)
                                <?php $proddata=$collection->getData() ?>  // get product data for all attribute values
                                <?php if($attr["label"]!= $this->stripTags($_filter->getLabel())): ?>  // make a nice looking label
                                    <?php foreach($proddata as $prod):?>
                                        <?php if($prod["type_id"]=="configurable"): ?>    // in this store all products were configurable
                                            <?php $split=split(",", $prod["color"]);?>     // get the attribute values that correspond with one product (a product may have more

// then one attribute value and they're separated by commas that's why we split the string with "," as deliminator)

即使你设置属性Filterable(with results) ,你仍然需要统计产品,这样只显示有产品的属性值。

 <?php foreach($split as $color): ?>  //check out how many products have the same attribute value
                                                <?php if($color==$attr["value"]): ?>
                                                    <?php $count++;?>
                                                <?php endif; ?>
                                            <?php endforeach; ?>
 
                                        <?php endif;?>
 
                                    <?php endforeach; ?>
 
                                    <?php if($count>0):?>  // check if any product has that attribute value
                                        <li><a href="<?php echo $this->getUrl('').$_current_category ?>?color=<?php echo $attr["value"]?>" ><?php echo  $attr["label"]; ?></a></li>  // if not currently active filter make a link
                                    <?php endif; ?>
 
                                <?php else:?>
                                    <li class="current"> <?php echo $this->stripTags($_filter->getLabel()); ?></li>  // if currently active filter write out the label
                                <?php endif;?>
                        <?php endif; ?>
<?php endforeach;  ?>
 
<?php endforeach; ?> // ending the first for loop (foreach($filters as $filter))
                </ol>
 
            </dd>
    </dl>
    <a class="all" style="float:right;" href="<?php echo $this->getClearUrl()?>">All</a>    // show all products, return from current state back to category view
</div>
<?php endif; ?>

360magento提供专业的基于magento系统的电商网站开发服务,如有需求或相关咨询,请与我们联系