1

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;
}
asked Jun 3, 2015 at 7:40

1 Answer 1

1

The method getAttributeText looks like this:

public function getAttributeText($attributeCode)
{
 return $this->getResource()
 ->getAttribute($attributeCode)
 ->getSource()
 ->getOptionText($this->getData($attributeCode));
}

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 method getOptionText 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') returns 1,2.
And I assume your options are something like this:

$options[] => array('value' => 1, 'label'=>'Option 1');
$options[] => array('value' => 2, 'label'=>'Option 2');
$options[] => array('value' => 3, 'label'=>'Option 3');
$options[] => array('value' => 4, 'label'=>'Option 4'); 

In this case, your method receives as parameter 1,2 and you check if there is an element in $options with value = 1,2. and there isn't.
Try to make your method look like this:

public function getOptionText($value)
{
 $options = $this->getAllOptions(false);
 $optionsByValue = array();
 foreach ($options as $item) {
 $optionsByValue[$item['value']] = $item['label'];
 }
 $values = explode(',', $value);
 $returnValue = array();
 foreach ($values as $_value) {
 if (isset($optionsByValue[$_value])) {
 $returnValue[] = $optionsByValue[$_value];
 }
 }
 return implode(', ', $returnValue);
}

Untested code so look out for typos.

answered Jun 3, 2015 at 7:47
5
  • Yes, My getOptionText method is present in npm_recurrex/package_source source model and it also contains the getAllOptions, toOptionArray, getFlatColums. Commented Jun 3, 2015 at 7:55
  • @Charlie Please post the getOptionText method in the question Commented Jun 3, 2015 at 8:07
  • I have added the content of getOptionText Method Commented Jun 3, 2015 at 9:21
  • @Charlie. I've updated the code. See if it helps. Commented Jun 3, 2015 at 9:29
  • Yes, You are awesome, You finally helped lot me to get that issue. getAttributeText working now. Thank you Marius Commented Jun 3, 2015 at 9:44

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.