Question

I'm trying to modify some code to output products with a 'featured' attribute set to "yes"

here is what I am working with:

<?php

 class Mage_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract{
    public function __construct(){
        parent::__construct();


        $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'featured');
        $attributeId = $attribute->getId();


        $storeId    = Mage::app()->getStore()->getId();
        $products = Mage::getResourceModel('reports/product_collection')
            ->addOrderedQty()
            ->addAttributeToSelect('*')
            ->addAttributeToSelect(array('name', 'price', 'small_image', 'featured'))
            ->setStoreId($storeId)
            ->addStoreFilter($storeId)
            ->addViewsCount();
        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);

        $products->setPageSize(16)->setCurPage(1);
        $this->setProductCollection($products);
    }
}

this does output products, but not the featured ones.

  • how do I alter the query so that it only selects featured products
  • it looks like it's adding all attributes to the select, is this correct & how do I output an attribute value in my phtml template?
Was it helpful?

Solution

Try to do like

$products = Mage::getResourceModel('reports/product_collection')
            ->addOrderedQty()
            ->addAttributeToSelect('*')
            ->addAttributeToSelect(array('name', 'price', 'small_image', 'featured'))
            ->addAttributeToFilter('featured',array('eq'=>true))
            ->setStoreId($storeId)
            ->addStoreFilter($storeId)
            ->addViewsCount();

Also make sure your attribute code is featured

In phtml file you can get attribute like

<?php $_helper = $this->helper('catalog/output'); ?>
<?php echo $_helper->productAttribute($_product, $_product->getAttributeCode(), 'attribute_code') ?>

OTHER TIPS

Please try bellow code

public function __construct()
    {
        parent::__construct();

        $collection = Mage::getResourceModel('catalog/product_collection');
        $collection = $this->_addProductAttributesAndPrices($collection)
            ->addAttributeToSelect('featured')
            ->addStoreFilter()
            ->addAttributeToFilter('featured',true)
            ->setPageSize(16)
            ->setCurPage(1);

        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);

        $collection->setPageSize(16)
            ->setCurPage(1);
        $this->setProductCollection($collection);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top