Magento – Displaying custom attribute in dropdown when collection loaded different to default

attributesdropdown-attribute

On the product view page, we're displaying a dropdown of products that are available to the customer as a free gift for that particular product.

We're loading the collection so we can get these products using:-

<?php $productGift = Mage::getModel('catalog/product')->load($giftId); ?>

This is currently displaying the product name which we can get by:-

<?php echo $productGift->getName()?>

We've created a custom attribute to use instead of the product name, so we want to display this in the dropdown instead of the product name but we're unable to use _$item or $_product, it's not a custom attribute of the current product, it needs to get the custom attribute from all the products loaded by Mage::getModel('catalog/product')->load($giftId);.

I'm unsure how to fetch this data.

It would be something like:-

<select id="select_28" class="product-custom-option" title="" name="freeGiftId">
    <option value=""><?php echo $this->__('-- Please Select --')?></option>
    <?php foreach($freeGifts as $giftId): ?>
    <?php $productGift = Mage::getModel('catalog/product')->load($giftId); ?>
    <option value="<?php echo $productGift->getId()?>">                                         
        <?php if ($productGift->getAttributeText('free_eliquid_product_name'); ?>
        <?php echo $productGift->getAttributeText('free_eliquid_product_name');?>
        <?php else: ?>
        <?php echo $productGift->getName()?>
    </option>
    <?php endforeach; ?>
</select>

(If free gift product has custom attribute set then display this otherwise display the product name). How can I get the custom attribute from $productGift? Thanks.

I've tried this…

<?php if ($productGift->getXyz()): ?>
    <?php echo $productGift->getXyz()?>
<?php else: ?>
    <?php echo $productGift->getName()?>

Should this be working? Because it doesn't…

Best Answer

You can directly access your custom attribue of product.

Mage::getResourceModel(‘catalog/product’)->getAttributeRawValue($product_id, $attribute_code, $store_id)

Or you can Directly use

<?php $productGift = Mage::getModel('catalog/product')->load($giftId); 
 $productGift->getYourAttribueName();
?>

Suppose you want color attribute then $productGift->getColor();