Question

I notice the 'featured product' attribute exists in the latest version of Magento. How can I make my featured products display on the homepage in the RWD theme?

Thank you

Was it helpful?

Solution

I don't see this attribute as default added to Magento 1.9.x it is however very easy to create a product collection based on a custom product attribute i.e. 'featured_product'.

Once you have built the collection you can create the html you want to display via a template file, i.e. a single product feature, or a multi product slider.

The steps to do this are

  • create a new custom product collection method
  • create a new layout block
  • create a new template

For example if I want to create a product collection for visible products with the attribute 'featured_product' I create a new file called Featured.php in magento/app/code/local/Mage/Catalog/Block/Product

<?php
class Mage_Catalog_Block_Product_Featured extends mage_Catalog_Block_Product_Abstract
{
    protected $_productsCount = null;
    protected $_categoryId = null;

    const DEFAULT_PRODUCTS_COUNT = 12;

/**
 * Initialize block's cache
 */
protected function _construct()
{
    parent::_construct();

    $this->addColumnCountLayoutDepend('empty', 6)
        ->addColumnCountLayoutDepend('one_column', 5)
        ->addColumnCountLayoutDepend('two_columns_left', 4)
        ->addColumnCountLayoutDepend('two_columns_right', 4)
        ->addColumnCountLayoutDepend('three_columns', 3);

    $this->addData(array(
        'cache_lifetime'    => 86400,
        'cache_tags'        => array(Mage_Catalog_Model_Product::CACHE_TAG),
    ));
}

/**
 * Get Key pieces for caching block content
 *
 * @return array
 */
public function getCacheKeyInfo()
{
    return array(
       'CATALOG_PRODUCT_NEW',
       Mage::app()->getStore()->getId(),
       Mage::getDesign()->getPackageName(),
       Mage::getDesign()->getTheme('template'),
       Mage::getSingleton('customer/session')->getCustomerGroupId(),
       'template' => $this->getTemplate(),
       $this->getProductsCount(),
       Mage::app()->getStore()->getCurrentCurrencyCode(),
       empty($this->_categoryId) ? "ALL" : $this->_categoryId
    );
}

/**
 * Prepare collection with new products and applied page limits.
 *
 * return Mage_Catalog_Block_Product_New
 */
protected function _beforeToHtml()
{
    $collection = Mage::getResourceModel('catalog/product_collection');
    $collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());

    $collection = $this->_addProductAttributesAndPrices($collection)
        ->addStoreFilter()
        ->addAttributeToSelect('featured_product')
        ->addFieldToFilter(array(
            array('attribute'=>'ext_enable','eq'=>'1'),
        ))
        ->addAttributeToSort('name', 'desc')
        ->setPageSize($this->getProductsCount())
        ->setCurPage(1)
    ;

    // Limit to a specific category
    //if(!empty($this->_categoryId)) $collection->addCategoryFilter(Mage::getModel("catalog/category")->load($this->_categoryId));

    $this->setProductCollection($collection);

    return parent::_beforeToHtml();
}

/**
 * Set a specific category to take the products from.
 *
 * @param $catid
 * @return Mage_Catalog_Block_Product_New
 */
public function setCategory($catid)
{
    $this->_categoryId = (int)$catid;
    return $this;
}

/**
 * Set how much product should be displayed at once.
 *
 * @param $count
 * @return Mage_Catalog_Block_Product_New
 */
public function setProductsCount($count)
{
    $this->_productsCount = $count;
    return $this;
}

/**
 * Get how much products should be displayed at once.
 *
 * @return int
 */
public function getProductsCount()
{
    if (null === $this->_productsCount) {
        $this->_productsCount = self::DEFAULT_PRODUCTS_COUNT;
    }
    return $this->_productsCount;
    }
}

Then I add the following to my layout, e.g. if I want the block to appear on the home page then I can place it within the cms_index_index section

            <block type="catalog/product_featured" name="catalog.product.featured" alias="featured_products">
            <action method="setTemplate"><template>catalog/product/featured.phtml</template></action>
        </block>

Then in my template I create a new file in catalog/product called featured.phtml, here I can iterate through the product collection and create the html I want to display, e.g. here is a simple bulleted list.

    <?php
    $_helper = $this->helper('catalog/output');
    $_productCollection = $this->getProductCollection();
?>
<div class="featured-products">
    <p>Featured Products - <?php echo count($_productCollection) ?></p>
    <ol class="featured-products-list" id="featured-products-list">
    <?php foreach ($_productCollection as $_product): ?>
        <li><?php echo $_product->getName() ?></li>
    <?php endforeach; ?>
    </ol>
</div>

When I refresh my store caches and goto the home page the featured product html will be displayed.

There are lots of modules that will help you do this but you can quickly achieve it with this example or by creating your own module.

Here is a link to a similar solution from the Magento wiki https://wiki.magento.com/display/m1wiki/How+to+Create+a+Featured+Product

OTHER TIPS

Follow this link for fetching Feature product on homepage http://inchoo.net/magento/featured-products-on-magento-frontpage-tutorial/

http://inchoo.net/magento/featured-products-on-magento-frontpage/

This tutorial I am explaining how to featured product to magento home page.

In the phtml file :

<?php
$featuredProducts = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('featured_product', 1)
->addAttributeToFilter('status', 1);

?> In local.xml

<cms_index_index>
<reference name="content">
    <block type="core/template" name="home.featured.product" template="custom/featuredproduct.phtml"/>
</reference>

please refer the tutroial for step by step explanation. http://www.pearlbells.co.uk/adding-custom-product-attributes-in-magento/

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top