0

I am using Magento 1.9.3.6 version. I have an attribute called brand. I want to get brand attribute's values collection by filter in order to show it in input text box as autocomplete suggestions.

brand attribute

I have tried the following code:

$attributeId = Mage::getResourceModel("eav/entity_attribute")->getIdByCode("catalog_product","brand");
$attribute = Mage::getModel("catalog/resource_eav_attribute")->load($attributeId);
$attributeOptions = $attribute ->getSource()->getAllOptions();

I get this output in autocomplete input box:

auto complete

When I tried like this,

 $attributeOptions = $attribute ->getSource()->getAllOptions()->addAttributeToFilter("value", "brand");

it shows error.

Fatal error: Call to a member function addAttributeToFilter() on array in .../CustomController.php on line 52

How to get attribute values as array filter by brand values.

Piyush
5,9249 gold badges35 silver badges67 bronze badges
asked Dec 1, 2017 at 4:25

2 Answers 2

0

Try below code

<?php
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', 'brand');
if($attribute && $attribute->getId()) {
 $options = Mage::getModel('eav/entity_attribute_source_table')->setAttribute($attribute)
 ->getAllOptions(false);
 foreach ($options as $option) {
 print_r($option);
 }
}
?>
answered Dec 1, 2017 at 5:12
1
  • I am getting the attribute values in array. I want to filter in the array values. Commented Dec 1, 2017 at 6:14
0

Try below code , it will get all option of the particular attribute

$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'brand');
$allOptions = $attribute->getSource()->getAllOptions(true, true);
$optionArray = array();
foreach ($allOptions as $key => $value) {
 $optionArray[$value['value']] = $value['label'];
}
print_r($optionArray);
answered Dec 1, 2017 at 5:15
2
  • I am getting the attribute values in array. I want to filter in the array values. Commented Dec 1, 2017 at 6:14
  • I don't know any way to filter attribute option label, but you can add a if condition to check matching label before adding option to the $optionArray in above code foreach loop Commented Dec 1, 2017 at 6:35

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.