0

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?
1

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') ?>
0

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);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.