Magento 1.7 PHP – Fix ‘Call to a Member Function addAttributeToSelect() on a Non-Object’

magento-1.7PHP

I'm getting this error when trying to filter products by a custom attribute.

In magento i have setup my custom attribute as 'featured' with 'yes/no' choice. This attribute has then been assigned to all attribute sets and various products then set to yes on the featured value.

On my homepage (cms page) i am then loading my template as a block

{{block type="catalog/product_list" name="home.catalog.product.list" alias="products_homepage" category_id="45" column_count="4" template="catalog/product/homelist.phtml"}}

And my template code (homelist.phtml) consists of the following

<?php
    $_productCollection=$this->getLoadedProductCollection();
    $_helper = $this->helper('catalog/output');
?>
<?php if(!$_productCollection->count()): ?>
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
<?php else: ?>

<div class="category-products section group clearfix">
    <ul class="products-grid">
<?php // Grid Mode ?>

    <?php $_iterator = 0; ?>

<?php
    $_items = $_productCollection->getItems();
        $_items->addAttributeToSelect('featured');
        $_items->addFieldToFilter(array(
            array('attribute' => 'featured', 'eq' => true),
        ));
?>
<?php $_collectionSize = 5 ?>

    <?php $_columnCount = 5; ?>
    <?php $i=0; foreach ($_items as $_product): ?>
    <?php if ($i++%$_columnCount==0): ?>

    <?php endif ?>
            <li class="item<?php if(($i-1)%$_columnCount==0): ?> first alpha<?php elseif($i%$_columnCount==0): ?> last omega<?php endif; ?> col span_1_of_5">
                <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>
                <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></h2>

 <?php // echo $this->stripTags($this->helper('core/string')->truncate($_product->getDescription(),65,'...', $_remainder, false)) ?> 

 <?php echo $this->stripTags($this->helper('core/string')->truncate($_product->gets_desc(),200,'...', $_remainder, false)) ?> 

                <?php echo $this->getPriceHtml($_product, true) ?>

                <div class="actions">
                    <?php if($_product->isSaleable()): ?>
                        <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
                    <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                    <?php endif; ?>

                </div>
            </li>

    <?php if ($i%$_columnCount==0 && $i!=$_collectionSize): ?>

    <?php endif ?>

    <?php if($i==5): ?>
        <?php break; ?>
    <?php endif; ?>
    <?php endforeach ?>

    <?php if ($i%$_columnCount==0): ?>

    <?php endif ?>


<?php endif; ?> 

</ul>

</div>
        <script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>

Best Answer

I took a look on your code and have notices to this code

$_items = $_productCollection->getItems();
$_items->addAttributeToSelect('featured');
$_items->addFieldToFilter(array(
    array('attribute' => 'featured', 'eq' => true),
));

First of all methods addAttributeToSelect and addFieldToFilter can be called only on object which is collection. You call them on items of the collection.

So your code should look like this

$_productCollection->addAttributeToSelect('featured');
$_productCollection->addFieldToFilter(array(
    array('attribute' => 'featured', 'eq' => true),
));
$_items = $_productCollection->getItems();