Adding Attributes to the “list.phtml” for the Magento Compare Page
Introduction
The “list.phtml” file is responsible for displaying product information on the compare page in Magento. In this guide, we’ll walk you through adding more attributes to the compare list and troubleshooting a common error.
The Current State
You’re currently seeing the following information on your compare page:
- Product image
- Model name
- Price
- Add to cart button
However, you would like to display additional details such as:
- Brand or vineyard name
- Stock availability
- Delivery time
To achieve this, we need to modify the “list.phtml” file in your theme.
The Error: Call to a Member Function on a Non-object
The error message Fatal error: Call to a member function getResource() on a non-object indicates that you’re trying to access an object method or property for which the object is not actually defined. In this case, $_product might not be initialized in your “list.phtml” file.
The Solution
Step 1: Understand the Product Object
In Magento, products are fetched from the database and passed as an argument to the “list.phtml” template. However, if you’re using a custom block or layout, this might not happen by default.
Step 2: Check Your Layout XML File
Ensure that your compare page’s layout XML file is correctly set up to pass the product object to the “list.phtml” template. You can find the relevant XML in app/design/frontend/{your_theme}/layout/catalog_product_compare.xml.
The block definition should look something like this:
<block type="catalog/product_compare_list" name="catalog.product.compare.list">
<action method="setProductCollection">
<product_collection>
<filter name="type_id" value="simple"/>
</filter>
</action>
</block>
Step 3: Use the Product Object in Your Template
Assuming your product object is correctly passed to the template (you can verify this by doing a print_r($_product) at the beginning of your “list.phtml”), you should be able to access its attributes.
You provided the code that worked on the product page:
<?php echo ($_product->getResource()->getAttribute('hersteller')->getFrontend()->getValue($_product) && $_product->getResource()->getAttribute('hersteller')->getFrontend()->getValue($_product) != "Nein") ? $_product->getResource()->getAttribute('hersteller')->getFrontend()->getValue($_product) : ''); ?>
<?php echo ($_product->getResource()->getAttribute('weingut')->getFrontend()->getValue($_product) && $_product->getResource()->getAttribute('weingut')->getFrontend()->getValue($_product) != "Nein") ? $_product->getResource()->getAttribute('weingut')->getFrontend()->getValue($_product) : ''); ?>
<?php echo ($_product->getResource()->getAttribute('ob_hersteller')->getFrontend()->getValue($_product) && $_product->getResource()->getAttribute('ob_hersteller')->getFrontend()->getValue($_product) != "Nein") ? $_product->getResource()->getAttribute('ob_hersteller')->getFrontend()->getValue($_product) : ''); ?>
Step 4: Handling the Error
If you still encounter the Call to a member function getResource() on a non-object error, it’s likely due to the product object not being passed correctly.
Here are some troubleshooting steps:
- Check if your layout XML file is properly set up.
- Verify that the parent block (catalog_product_compare_list) is defined and rendered before calling your custom template.
- Ensure that the product collection is properly initialized in your block or controller.
Conclusion
Adding attributes to the “list.phtml” requires a good understanding of Magento’s object-passing mechanisms between layout files, blocks, and templates.
Remember to always backup your original files before making changes. If you’re unsure about any part of this process, feel free to reach out to our team at LaravelCompany.
Happy coding!