How to get list of attribute options in frontend view?
-
01-10-2020 - |
Question
I'm writing a custom display for certain attributes based on its label. Within the 'configurable.phtml' file, it has the following lines:
$_product = $this->getProduct();
$_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes());
I can loop through the attributes array and check the label no problem:
$special_attr = false;
foreach ($_attributes as $_attribute) {
echo $_attribute->getLabel().'<br>';
if ($_attribute->getLabel() === 'FooBar') {
$special_attr = $_attribute;
}
}
This will print out all of the labels ("Color", "Size", etc.). I'm trying to get a list of all options and their values given this special attribute now, and I'm completely stumped. All of the other questions I have found have said something along the lines of using ->getSource()->getAllOptions(false)
, however, calling getSource()
on this attribute just returns null.
How can I get all options given this special attribute?
if ($special_attr) {
$options = $special_attr->??????
// list all options??
}
La solution
You can try below code to get options of configurable attributes within configurable.phtml
$special_attr = false;
foreach ($_attributes as $_attribute) {
echo $_attribute->getLabel().'<br>';
if ($_attribute->getLabel() === 'FooBar') {
$special_attr = $_attribute->getPrices();
}
}
As you can see, the trick lies in $_attribute->getPrices()
. This will give you an output like this for color
attribute (if that is configurable)
Array
(
[0] => Array
(
[product_super_attribute_id] => 13
[value_index] => 183
[label] => Brown
[default_label] => Brown
[store_label] => Brown
[is_percent] => 0
[pricing_value] => 10.0000
[use_default_value] => 1
[value_id] => 15
)
[1] => Array
(
[product_super_attribute_id] => 13
[value_index] => 174
[label] => Orange
[default_label] => Orange
[store_label] => Orange
[is_percent] => 0
[pricing_value] => 20.0000
[use_default_value] => 1
[value_id] => 16
)
)
I believe this will be enough in your case.