Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

[Solved] Help retrieving a catalog product’s attribute option value (dropdown)
 
petemcw
Jr. Member
 
Total Posts:  24
Joined:  2007-08-31
 

I am working on a custom API for a client’s Magento site and I’m running into some trouble accessing all the required data that I need to include in the API response.  One of the methods in this API is to return a product’s attributes.  An example product would be a part from a specific television.  The product has an attribute defined called tv_make, and this attribute is defined as a “dropdown” with many options (Sony, Toshiba, etc).

When I try to pull together a collection from the catalog_product model, everything seems to work fine except for the value of attributes like the above tv_make.  I get something like the value “80”, when I’m looking for “Sony”.  Below is the code I a currently have and I’m not sure when the proper join syntax is to pull the attribute’s option value instead of ID.

$collection Mage::getModel('catalog/product')->getCollection()
            ->
setStoreId(self::getStoreId())
            ->
addAttributeToSelect('name')
            ->
addAttributeToSelect('price')
            ->
addAttributeToSelect('tv_make')
            ->
addAttributeToSelect('tv_model')
            ->
addAttributeToFilter('part_number1'$partNumber);

I have also tried things like this, with no luck:

$collection->joinAttribute('brand''catalog_product/tv_make''entity_id'null'left');

Any help in the right direction would be appreciated!

 
Magento Community Magento Community
Magento Community
Magento Community
 
petemcw
Jr. Member
 
Total Posts:  24
Joined:  2007-08-31
 

I think I have figured it out. In addition to the code I posted above, I was looping through the different products returned in the collection.  I was able to pull out the attribute value at that stage.

// sample code
foreach ($collection as $product{
    
echo $product->getAttributeText('tv_make'); // Sony, etc.
}

Hope this helps somone and if there’s a better way to do it let me know!

 
Magento Community Magento Community
Magento Community
Magento Community
 
frontier
Member
 
Total Posts:  41
Joined:  2008-04-17
 

Yes I would like to know this as well.. to use as the value to pass to addAttributeToFilter.
(currently I am having to look in the database and then hardcode the value)

 
Magento Community Magento Community
Magento Community
Magento Community
 
miked2004
Sr. Member
 
Avatar
Total Posts:  214
Joined:  2007-12-13
Atlanta, Georgia
 

To get the value of a select attribute this code works great.

echo $this->htmlEscape($_item->getAttributeText('color'));
 
Magento Community Magento Community
Magento Community
Magento Community
 
ChrisF
Member
 
Total Posts:  37
Joined:  2008-10-27
 

The subject of this thread is similiar to a problem I’m having while learning the Magento API. 

I set up a custom attribute ‘free_shipping’.  It’s a drop down (Yes or No).  In ../template/catalog/product/view.phtml I can do a

<?php if ($free_shipping_text $_product->getAttributeText('free_shipping') == "Yes"): ?>
                        
<li id="container_2" class="tabs-list"><?php echo 'Free Shipping!' ?></li><?php endif;?>

That works fine for the product details page.

But I need free shipping to work on the category list page too, in In ../template/catalog/product/list.phtml.  So we’ve got a

<?php foreach ($_productCollection as $_product): ?>
and then we get the list of products in that category.  The problem is that
$_product->getAttributeText('free_shipping')
never returns any text even though I know it’s set for a couple products and it works in the product detail page.

Any ideas on what I’m doing wrong?

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top