2

Suppose color and price and manufacturer are three attribute codes.

black and blue are attribute values for color.
$0-100 and &101-200 are attribute values for price.
hp and dell are attribute values for price.

On category page, in filter section if I'm selecting attribute values black and &101-200. How can I get corresponding attribute codes?

In filter.phtml file sing this code I'm getting selected attribute code but not the attribute value in a proper format.

    <?php
$appliedFilters = Mage::getSingleton('catalog/layer')->getState()->getFilters();
foreach ($appliedFilters as $item) {
      $items[] = $item->getFilter()->getRequestVar();

} 
foreach ($items as $item) {
     echo ($item."<br>");
   }
4

Try this:

$appliedFilters = Mage::getSingleton('catalog/layer')->getState()->getFilters();

foreach ($appliedFilters as $item):

   $filter = $item->getFilter();
   $items[] = array(
     'code'    => $filter->getRequestVar(),
     'label'   => $item->getName(),
     'value'   => $item->getLabel()
   );

endforeach; 
5
  • it's giving me the selected attribute codes. Though I'm not getting the selected attribute values. after endforeach; I've added my code foreach ($items as $items): echo ($items['code']."<br>"); echo ($items['label'."<br>"]); echo ($items['value'."<br>"]); endforeach; . How I will get selected attribute value at the same time? – amitshree May 12 '15 at 10:52
  • Apologies, the second and third lines should get the item object, I have amended my response – Fran Mayers May 12 '15 at 10:59
  • I've added 'attval' => $item->getValue() to the items array and printed it's value in my foreach loop echo ($items['attval']."<br>"); to print the attribute value(not label). Actually I've to pass these selected attributes in URL to search for products. I'm customizing default search functionality and first selecting n number of attributes and using a search button passing these parameters in search URL. – amitshree May 12 '15 at 11:34
  • ^^ using echo ($items['attval']."<br>"); I'm getting the attribute values for all attributes but for price attribute it's output is "Array" instead of something like "100-200". – amitshree May 12 '15 at 11:56
  • 1
    You can just do : if(is_array($items['attval'])) echo implode('-', $items['attval']); else echo $items['attval']; – Fran Mayers May 12 '15 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.