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

发布于 2024-10-20 12:35:00 字数 286 浏览 2 评论 0原文

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

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

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

提前致谢

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 anyone know how to make this look like a dropdown list instead?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鹿童谣 2024-10-27 12:35:00

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

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

我查看了存储在 /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; ?>

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; ?>
肥爪爪 2024-10-27 12:35:00

抱歉我的英语...我是法语;-)

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

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

波伊波伊

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

南冥有猫 2024-10-27 12:35:00

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文