3

In Magento 1.5 on the product page, I created a product collection (for related products) like this (I've omitted filters):

// $_products = Mage::getResourceModel('catalog/product_collection') // this variation makes no difference.
$_products = Mage::getModel('catalog/product')->getCollection()
                        ->addAttributeToSelect('name')
                        ->addAttributeToSelect('url_path')
                        ->addAttributeToSelect('price')
                        ->addAttributeToSelect('some_text_attribute')
                        ->addAttributeToSelect('some_dropdown_attr'); // does not work for dropdown/multiselect apparently

Now if I echo $_products->getSelect() I see that adding the dropdown attribute did nothing to modify the select. So the data for 'some_dropdown_attr' is not available that way. Isn't there a way to add it to the select. And if not, there certainly must be a way to get the value(s) of any dropdown (or multiselect) attribute for a certain product.

$_text_attr[] = array();
$_dropdown_attr[] = array();
foreach($_products as $_product) {
  $_text_attr[] = $_product->getData('some_text_attribute');
  // 1.) get resource - doesn't work
  // $_dropdown_attr[] = $_product->getResource()->getAttribute('some_dropdown_attr')->getFrontend()->getValue($_product); // results in array of "No"
  // 2.) get attribute text - doesn't work
  // $_dropdown_attr[] = $_product->getAttributeText('some_dropdown_attr'); // results in array of NULLS. 
  // Get attributes - doesn't work - array of "No". Also, even if it did work it seems idiotic to have to get all product attributes just to get the value of one
  // $_attrs = $_product->getAttributes();
  // $_dropdown_attr[] = $_attrs['some_dropdown_attr']->getFrontend()->getValue($_product);
}
3
  • Are you sure that the product you are using for tests has the attribute 'some_dropdown_attr'?
    – Marius
    Jan 18, 2014 at 8:15
  • @Marius Are you suggesting that one of the methods I tried should have worked? The value was visible in the Admin view for the product. I confirmed it was the right attribute by looking at the form field's id using Chrome's "Inspect Element." The id = the attribute code. Jan 19, 2014 at 19:36
  • In my opinion this should work $_product->getAttributeText('some_dropdown_attr')
    – Marius
    Jan 20, 2014 at 7:26

1 Answer 1

2

As @marius points out in the comments the following should work:

$_product->getAttributeText('some_dropdown_attr');

Therefore only the following could be happening:

  • You must have mis-named your dropdown attribute
  • Ensure that the attribute has valid values applied on the current store view

If all else fails, there may be a problem with your attribute setup code; for instance, not visible in the frontend isn't set properly and you're using flat tables...

In that case to debug output it with the following code:

$attr = $_product->getResource()->getAttribute('some_dropdown_attr')->getFrontend()->getValue($_product);

If that works check your catalog attribute configuration or disable flat indexes.

2
  • Is there a way to go around the flat table and use the eav tables as the data source even when flat tables are activated? When I have tried your suggestions, I will let you know if they have worked. Feb 21, 2014 at 5:15
  • Your getResource() code to get the value of the attribute is returning "No", even though that is not the value of the attribute for that product. In the backend, I can see it using chrome's inspect element: <label for="some_dropdown_attr">Some Dropdown</label> and its value is NOT "No". Any ideas? Feb 26, 2014 at 0:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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