Monday 3 September 2012

Create Magento Featured Products

n this article, I will guide you how to display the featured products on frontent Home Page

Step1:  Create attribute "featured".


Go to admin-> Catalog-> Attributes-> Manage Attributes-> add new attribute:

Attribute Properties

Attribute Identifier: featured

Scope: Store View

Catalog Input Type for Store Owner: Yes/No

Unique Value (not shared with other products): No

Values Required: No

Input Validation for Store Owner: None

Apply To: All Product Types

Front End Properties

Use in quick search: No

Use in advanced search: Yes

Comparable on Front-end: No

Use In Layered Navigation (Can be used only with catalog input type ‘Dropdown’): No

Manage Label/Options

Default: Featured Product

English: Featured Product

Step2:  Adding these attributes to attribute set.

After creating attributes, you need to add them into attribute sets, if you use default attribute set (or any attribute set you define), please add these attributes into the attribute set you used. (Below is example for Default Attribute Set).

Step 3:

Go to folder: …\app\design\frontend\default\your theme\template\catalog\product

Create file featured.phtml and add the following code into this file

<?php

//{{block type="core/template" show_total="4" name="product_featured" as="product_featured" template="catalog/product/featured.phtml}}

$totalPerPage = ($this->show_total) ? $this->show_total : 12;

$visibility = array(

                      Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,

                      Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG

                  );

$storeId = Mage::app()->getStore()->getId();

$_productCollection = Mage::getResourceModel('reports/product_collection')

                        ->addAttributeToSelect('*')   

                        ->setStoreId($storeId)

                        ->addStoreFilter($storeId)  

                        ->addAttributeToFilter('visibility', $visibility)

                        ->addAttributeToFilter('featured', true)

                        ->setOrder('created_at', 'desc')

                        ->addAttributeToSelect('status')

                        ->setPageSize($totalPerPage);

Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($_productCollection);

Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($_productCollection);

Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($_productCollection);

?>

<h1>Featured product (test module)</h1>

<br><br>

<div style="width:95%; text-align:center;">

<?php $_collectionSize = $_productCollection->count() ?>

    <table cellspacing="0" class="generic-product-grid" id="product-list-table">

    <?php $i=0; foreach ($_productCollection as $_product): ?>

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

    <tr>

    <?php endif ?>

        <td>

            <p class="product-image">

                <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">

                    <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(170, 170); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />

                </a>

            </p>

            <h5><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h5>

            <div>

            <?php if ($_product->getSpecialPrice()>0):?>

                <span class="old"><?php echo $this->helper('checkout')->formatPrice($_product->getPrice()) ?></span>&nbsp;

                <span class="new"><?php echo $this->helper('checkout')->formatPrice($_product->getSpecialPrice()) ?></span>

            <?php else: ?>

                <span class="new"><?php echo $this->helper('checkout')->formatPrice($_product->getPrice()) ?></span>

            <?php endif; ?>

            </div>

            <?php echo $_product->getShortDescription() ?>

            <div class="clear"></div>

          

        </td>

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

    </tr>

    <?php endif ?>

    <?php endforeach ?>

    <?php for($i;$i%4!=0;$i++): ?>

          <td class="empty-product">&nbsp;</td>

    <?php endfor ?>

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

    </tr>

    <?php endif ?>

    </table>

    <script type="text/javascript">decorateTable('product-list-table')</script>

</div>

Step 4: Display the featured products on frontent Home Page:

You copy and Paste the following code into CMS home page

{{block type="core/template"  name="product_featured" as="product_featured" template="catalog/product/featured.phtml}}

Step 5:

Go to admin-> Catalog-> Manage Products-> add new Products:

Input full information for product.

You will see Featured Products on left side: choose Yes. And Save Products.

Now go back to Homepage and refresh. You should see the attribute product you’ve created.

No comments:

Post a Comment