【问题标题】:Magento - Change attribute select to dropdown list in Adv SearchMagento - 将属性选择更改为高级搜索中的下拉列表
【发布时间】:2011-03-06 02:53:36
【问题描述】:

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

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

有谁知道如何使它看起来像一个下拉列表?

提前致谢

【问题讨论】:

  • 模板/目录搜索/advanced/form.phtml

标签: search magento attributes drop-down-menu


【解决方案1】:

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

我用不同的设置做了一些测试,结果发现在高级搜索中,每个属性都是一个列表(下拉和多选)并且它有超过 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) {
    . . .

如果您将最后一行的数字 2 更改为数字 5,高级搜索将在每个选项少于 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();
}

接下来你需要做的是在表单的 switch 中的一个小的 if 语句(见下文),它将检查属性的名称并在此基础上调用 getAttributeSelectElement() 或我们的新方法 getAttributeDropDownElement() .我把这份工作留给你:)

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

【讨论】:

    【解决方案2】:

    对不起我的英语......我是法国人;-)

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

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

    PoyPoy

    【讨论】:

    • 感谢您的回复。我刚刚仔细检查过,它设置为下拉菜单。发生这种情况还有其他原因吗?
    【解决方案3】:

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

    在你的设计模板目录template/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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-20
      • 2018-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-08
      • 1970-01-01
      相关资源
      最近更新 更多