Magento - 更改属性选择到高级搜索中的下拉列表

时间:2011-03-06 02:53:37

标签: search magento attributes drop-down-menu

我一直试图找到一种方法来强制属性显示为下拉菜单,而不是一块选项,但没有运气。代码当前如下所示:

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

有人知道如何让它看起来像下拉列表吗?

提前致谢

3 个答案:

答案 0 :(得分:2)

我今天早些时候遇到了同样的问题,最奇怪的是我的属性(下拉)具有相同的属性,但一个显示下拉菜单,另一个显示高级搜索中的多选菜单。

我做了一些不同设置的测试,结果发现在高级搜索中,每个属性都是一个列表(下拉和多选),它有两个以上的选项显示为多选。

我看了一下存储在/app/code/core/Mage/CatalogSearch/Block/Advanced/Form.php中的Mage_CatalogSearch_Block_Advanced_Form,我看到了这个2的条件被检查的地方。 magento核心团队就是这样做的,以确保'yesno'或布尔列表显示为下拉列表。

在上面提到的文件中,从第173行开始(在当前版本的magento上) 是以下代码:

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) {
    . . .

如果您使用数字5更改最后一行的数字2,则高级搜索将在每个少于6个选项的属性上显示下拉菜单。

我为自己做的是添加了一个新方法getAttributeDropDownElement(),下面是getAttributeSelectElement(),如下所示:

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();
}

接下来你需要做的是在表单开关中的一个小if语句(参见下文),它将检查属性的名称,并根据它调用getAttributeSelectElement()或我们的新方法getAttributeDropDownElement() 。我把这份工作交给你了:)

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

答案 1 :(得分:0)

抱歉我的英文...我是法国人; - )

在管理员面板中,您可以选择属性的类型

确保将您的属性声明为列表。在我的Magento版本中,它是代码和范围之后的属性管理面板中的第三个信息。

PoyPoy

答案 2 :(得分:0)

Magento有一个用于生成选项的类,可用作Mage_Core_Block_Html_Select类(/app/code/core/Mage/Core/Block/Html/Select.php)。

在您的设计模板目录模板/ catalogsearch / advanced / form.phtml上,替换

echo $this->getAttributeSelectElement($_attribute);

使用

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();