I have checked lot and tried many things, but I am still not getting the product multiselect selected values label on product view page.
I have product attribute called package which is multiselect,
Code that create the product attribute
$this->addAttribute(
'catalog_product',
'package',
array(
'group' => 'Package',
'backend' => 'eav/entity_attribute_backend_array',
'frontend' => '',
'class' => '',
'default' => '',
'label' => 'Package',
'input' => 'multiselect',
'type' => 'text',
'source' => 'npm_recurrex/package_source',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'is_visible' => 1,
'required' => 0,
'searchable' => 0,
'filterable' => 0,
'unique' => 0,
'comparable' => 0,
'visible_on_front' => 0,
'user_defined' => 1,
)
);
this works fine, I am successfully saving the product.
But in frontend product view page when I say
Mage::log(print_r($_product->getData('package'), true));
Its prints the result as 1,2
But I wanted to display option labels of multiselect not option id's.
So I tried with this code
Mage::log(print_r($_product->getAttributeText('package'), true));
It prints nothing, just blank space :(.
I have checked this link but no use.
I am confused with this, Where I am wrong? and what is the wrong thing?
Can anybody explain me what is happening in my case?
my getOptionText Method
public function getOptionText($value)
{
$options = $this->getAllOptions(false);
foreach ($options as $item) {
if ($item['value'] == $value) {
return $item['label'];
}
}
return false;
}
Best Answer
The method
getAttributeText
looks like this:Notice the last 2 lines.
They mean that the method calls
getOptionText
from the source model class.I see that your attribute has a custom source model
npm_recurrex/package_source
. Make sure that model has the methodgetOptionText
or it's parent class has it and returns what your need.[EDIT]
Your
getOptionText
is wrong.It would work if the attribute would be of type
select
.But it doesn't work for multiselects.
Here is a scenario. Actually I'm using your scenario.
$_product->getData('package')
returns1,2
.And I assume your options are something like this:
In this case, your method receives as parameter
1,2
and you check if there is an element in$options
withvalue = 1,2
. and there isn't.Try to make your method look like this:
Untested code so look out for typos.