Solving Fatal Error: Call to a Member Function Count() on a Non-Object
Introduction
When working with Magento CMS pages, you might encounter errors like “Call to a Member Function count() on a Non-object”. This occurs when you try to access the count of an object that has not been initialized. In your case, the issue arises from copying and pasting code from catalog/product/list.phtml into your custom CMS block without properly initializing the product collection.
Understanding the Error
The error message indicates that you’re trying to call the count() function on a variable ($_productCollection) which is not an object or array. The count() function expects a collection, but it’s receiving a null value because the collection hasn’t been loaded or initialized correctly in your CMS block.
Step-by-Step Solution
1. Correctly Load the Product Collection
In your indexController.php, you need to load the product collection with the desired category filter before passing it to your CMS block template. Here’s how:
<?php
class Thaneuk_Cmsproducts_IndexController extends Mage_Core_Controller_Front_Action{
public function IndexAction() {
$categoryId = 41; // Replace with your desired category ID
$limit = 5;
$_productCollection = Mage::getModel('catalog/product')
->getCollection()
->addCategoryFilter($categoryId)
->setPageSize($limit);
$this->loadLayout();
$block = $this->getLayout()->createBlock('core/template');
$block->setTemplate('cmsproducts/index.phtml')->setData('product_collection', $_productCollection);
$this->getLayout()->getBlock('content')->append($block);
}
}
In this code:
- Replace
41with the ID of the category whose products you want to display. - I’ve set a limit of 5 products per page. You can adjust this as needed.
- The
addCategoryFilter()method ensures that only products from the specified category are included in the collection.
2. Access the Product Collection in Your Template
In your cmsproducts/index.phtml template, you should access the product collection like this:
<?php $_productCollection = $this->getData('product_collection'); ?>
Here’s how you can loop through the products and display them:
<?php if (count($_productCollection)) > 0): ?>
<?php foreach ($_productCollection as $_product): ?>
<h2><?php echo $_product->getName(); ?></h2>
<p class="price"><?php echo $this->helper('core')->currency($_product->getPrice(), true, false); ?></p>
<!-- Add more product details here -->
<?php endforeach; ?>
<?php else: ?>
<p>No products available in this category.</p>
<?php endif; ?>
3. Ensure Proper Initialization
In your CMS block definition:
<reference name="cms/block">
<action method="setData"></action>
</reference>
The setData action is used to set the variables for the CMS block template. In our case, we’re setting a variable named product_collection.
Conclusion
To solve the “Call to Member Function count() on Non-object” error:
- Ensure you load and initialize your product collection correctly in the controller.
- Access the product collection in your template using
$this->getData('product_collection'). - Loop through the products and display them in your template.
This approach ensures that the product collection is properly loaded before trying to access it in your CMS block template.