valentina.a87
valentina.a87

Reputation: 127

magento get custom attribute label

I've a problem, I want to show the label of a custom attribute in the product page. I explain me better, starting from this link because is what I want to do: http://www.customy.com/blog/how-to-display-video-on-magento-product-page/

I want a video product in the sidebar of the product page, so I create a new custompage.phtml and i put this in the sidebar from catalog.xml, in my custompage.phtml I put this code to have the custom label:

getResource()->getAttribute('video')->getStoreLabel();?>

but I have this error:

"Fatal error: Call to a member function getResource() on a non-object in ..path//"

I have try different code but still have this problem. I think that I forget to put something in my .phtml but I'm new of Magento and I don't know what!

Thank in advance!

Upvotes: 4

Views: 13499

Answers (2)

Adam Moss
Adam Moss

Reputation: 5712

If you don't have access to the product model, I wrote a small query to get it from DB. This could be done better, but should be a decent starting point for your class:

protected $_dbConn;

public function __construct()
{
    $this->_dbConn = Mage::getSingleton('core/resource')->getConnection('core_read');
}

public function getAttributeLabel($code)
{
    $query = "
    SELECT b.value
    FROM eav_attribute a
    JOIN eav_attribute_label b
    ON a.attribute_id = b.attribute_id
    WHERE a.attribute_code = '".$code."'";

    return $this->_dbConn->fetchOne($query);
}

Upvotes: 1

seanbreeden
seanbreeden

Reputation: 6097

Since $_product didn't work then you'll need to load the object before attempting to access the attribute. Try this:

$product_id = Mage::registry('current_product')->getId();
$_product=Mage::getModel('catalog/product')->load($product_id);
echo $_product->getResource()->getAttribute('video')->getStoreLabel();

Upvotes: 6

Related Questions