How to add “getAttributeText” to “related view” section?
-
29-09-2020 - |
Question
I am getting the error Fatal error: Call to a member function getAttributeText() on a non-object in /catalog/product/list/related.phtml on line 21
while add "getAttributeText" in the file /catalog/product/list/related.phtml
.
This is code:
<?php
$merchant_name = $_product->getAttributeText('merchant_name');
if ($merchant_name){
echo "By ". $_product->getResource()->getAttribute('merchant_name')->getFrontend()->getValue($_product);
}
elseif ($_product->getIsEbayaffiliate()){
echo "By eBay";
}
else {
echo "By Home Done";
}
?>
FYI, I am using the same "getAttributeText" in the following files and it's working perfectly:
1. catalog/product/view.phtml
2. catalog/product/list.phtml
3. catalog/product/new.phtml
4. catalog/product/special.phtml
Solution
Is it as simple as changing the variable $_product
to $_item
?
The error message you see is telling you that $_product
is not an object.
Typically in related.phtml
we write
foreach($this->getItems() as $_item)
{
$_item->getName();
}
But I note you may also need to ensure that the attribute merchant_name
has been included in the related items collection.
[no longer relevant and or misleading]
I prepared a longer in-depth answer but it is not rigorous and so I will report more tomorrow.
As a test if you echo out get_class($_item)
because I can't remember if $_item
in related.phtml
is Mage_Catalog_Model_Product
. If $_item isn't Mage_Catalog_Model_Product
then try $_item->getProduct()
but I haven't tried it.