0

Is there a possibility in Magento to have a multi-select attribute, for which I would use a textbox in layered navigation instead of showing all items from multi-select?

I have an attribute where I will have hundreds of options and need to use it in layered navigation.

When customer uses invalid value, an error should show up.


Edit: After the help from FlorinelChis I have folowing code in filter.phtml:

<ol>
<?php foreach ($this->getItems() as $_item): ?>
  <?php
  $attributeModel = $this->getAttributeModel();    
  $attribute_code =  $attributeModel->getAttributeCode();
  ?>
    <li>
      <?php if ($attribute_code != 'available_zip'): ?>
        <?php if ($_item->getCount() > 0): ?>
        <a href="<?php echo $this->urlEscape($_item->getUrl()) ?>"><?php echo $_item->getLabel() ?></a>
        <?php else: echo $_item->getLabel() ?>
        <?php endif; ?>
        <?php if ($this->shouldDisplayProductCount()): ?>
        (<?php echo $_item->getCount() ?>)
        <?php endif; ?>
      <?php endif; ?>
    </li>
<?php endforeach ?>
</ol>
  <?php
    if ($attribute_code == 'available_zip'): 
          $cat =  Mage::registry('current_category')->getUrlPath() ;
          $url = Mage::getBaseUrl();
          /*$sendUrl = $this->urlEscape($_item->getUrl()).'+'.$url.$cat.'?'.$attribute_code.'='.$_item->getValue();*/
        echo '<form action="" method="get">';
        echo '<input type="text" size="5" maxlength="5" name="'.$attribute_code.'" />';
        echo '<button type="submit">OK</button>';
        echo '</form>';
  endif; ?>

I have one more thing now: how to send the form with attribute id instead of value?

Swip
  • 173
  • 1
  • 7
  • 17
  • It's not clear from your question what is your attribute type now? is it multi-select or is a text field? – FlorinelChis Jan 23 '13 at 21:03
  • I have a multi-select attribute. But I would like to use text box in layered navigation instead (so as I don't get listed hundreds of lines from the list there). – Swip Jan 23 '13 at 21:11

1 Answers1

1

First, let's find out how Magento displays the filters in left hand navigation

1) Enable Template Path Hints: Magento Enable Template Path Hints

and here is the result:

magento layered navigation filter

2) Let's take a look at app/design/frontend/base/default/template/catalog/layer/filter.phtml (you should copy this file in your theme folder structure)

3) The block ($this in the template file is an instance of Mage_Catalog_Block_Layer_Filter_Attribute which extends Mage_Catalog_Block_Layer_Filter_Abstract)

4) You can see that in Mage_Catalog_Block_Layer_Filter_Abstract a getName() method exists, so you could rely on this function to identify when you attribute is displayed. Don't! The label can change and your code won't be useful any more. Back to our template file, you can get the attribute_code (which is reliable)

//$this is instance of Mage_Catalog_Block_Layer_Filter_Attribute 
$attributeModel = $this->getAttributeModel();    
$attribute_code =  $attributeModel->getAttributeCode();

5) On your template you can have a check based on attribute code, so you display either the standard list or your custom html code with a textarea instead of a huge list.

FlorinelChis
  • 1,488
  • 1
  • 11
  • 24
  • Great help! Thank you! May I just ask you for one more think? Most probably easy to solve - but when I create a form for the attribute (instead of that multi-select), the form get's printed as many times as the number of lines the attribute has. Of course it's because of the foreach used in filter.phtml. But how to prevent this (not to go through the cycle for every single line of the attribute? If I put the form behind the cycle, it gets shown for every attribute instead. – Swip Jan 24 '13 at 12:43
  • Now it's there... I have two issues (described on the bottom) – Swip Jan 24 '13 at 13:19
  • `if ($attribute_code = 'available_zip'): ` - like really? what is = used for? what is == for? what is === for in php? I am starting to feel sorry for the time allocated to explain this. – FlorinelChis Jan 24 '13 at 13:22
  • Don't know what to say - I am really sorry for bothering with that stupid mistake! But the thing which bothers me more is - how to send the form with attribute id instead of value – Swip Jan 24 '13 at 13:30