开发者

Magento - Change attribute select to dropdown list in Adv Search

I've been trying to find a way of forcing an attribute to show as a dropdown rather than a block of options but had no luck. The code current looks like this:

case 'select': ?>
    <div class="input-box"> <?php echo $this->getAttributeSelectElement($_attribute) ?> </div>
    <?php endswitch; ?>

Does开发者_C百科 anyone know how to make this look like a dropdown list instead?

Thanks in advance


I had the same problem earlier today and the strangest thing was that I had the attributes (drop down) with the same properties but one displaying a drop down menu and the other a multi select menu in the advanced search.

I did some testing with different settings and it turned out that in the advanced search every attribute that is a list (drop down and multi-select) and it has more than 2 options is displayed as multi-select.

I had a look at Mage_CatalogSearch_Block_Advanced_Form stored in /app/code/core/Mage/CatalogSearch/Block/Advanced/Form.php and I saw where this condition of 2 is checked. The magento core team made it like this to make sure that the 'yesno' or boolean list are displayed as dropdown.

In the above mentioned file, starting from line 173 (on the current version of magento) is the following code:

public function getAttributeSelectElement($attribute)
{
    $extra = '';
    $options = $attribute->getSource()->getAllOptions(false);

    $name = $attribute->getAttributeCode();

    // 2 - avoid yes/no selects to be multiselects
    if (is_array($options) && count($options)>2) {
    . . .

If you change the number two on the last line with the number 5, advanced search will display drop down menu on every attribute that has less than 6 options.

What I did for myself is I added a new method, getAttributeDropDownElement(), bellow getAttributeSelectElement() that looks like this:

public function getAttributeDropDownElement($attribute)
{
    $extra = '';
    $options = $attribute->getSource()->getAllOptions(false);

    $name = $attribute->getAttributeCode();

    // The condition check bellow is what will make sure that every
    // attribute will be displayed as dropdown
    if (is_array($options)) {
        array_unshift($options, array('value'=>'', 'label'=>Mage::helper('catalogsearch')->__('All')));
    }



    return $this->_getSelectBlock()
        ->setName($name)
        ->setId($attribute->getAttributeCode())
        ->setTitle($this->getAttributeLabel($attribute))
        ->setExtraParams($extra)
        ->setValue($this->getAttributeValue($attribute))
        ->setOptions($options)
        ->setClass('multiselect')
        ->getHtml();
}

The next thing you need to do is a small if statement within the switch of the form (see bellow) that will check the name of the attribute and base on that to call either getAttributeSelectElement() or our new method getAttributeDropDownElement(). I leave this job to you :)

 case 'select': ?>
   <div class="input-box"> <?php echo $this->getAttributeSelectElement($_attribute) ?>     </div>
   <?php endswitch; ?>


Sorry for my English...i'm french ;-)

In your admin panel, you can choose the type of your Attributes

Make sure that your attribute is declared as a list. In my Magento version, it's the third information in the attribute admin panel after code and scope.

PoyPoy


Magento has a class for generating selects available as a Mage_Core_Block_Html_Select class (/app/code/core/Mage/Core/Block/Html/Select.php).

On your design template directory template/catalogsearch/advanced/form.phtml, replace

echo $this->getAttributeSelectElement($_attribute);

With

echo $this->getLayout()->createBlock('core/html_select')
                    ->setOptions( $_attribute->getSource()->getAllOptions(true))
                    ->setName($_attribute->getAttributeCode())
                    ->setClass('select')
                    ->setId($_attribute->getAttributeCode())
                    ->setTitle($this->getAttributeLabel($_attribute))
                    ->getHtml();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜