This is the blog for getting Idea regarding PHP, Magento, jQuery and JavaScript for Customization.

Tuesday 30 October 2012

Magento : Create featured product


There is easy few steps to create Featured Product,

A) First Create a One Attribute :

    Set this under "Property Teb" of Right side, 

         1.  Attribute Code *                                        =      featured
         2. Scope                                                         =      Store View
         3. Catalog Input Type for Store Owner            =      Yes/No

  Set this under "Manage Label/Options Teb" of Right side, 

        1.  Admin                        =    Featured Product
        2.   Default Store View    =    Featured Product 


B) Now goto Catalog > Attribute > Manage Attribute Set

    1. Click on Default Attribute set,
    2. Here, see your  fetured attribute under Unassigned Attributes,  Drag that attribute inside Groups  general  directory
    3. Save




C)  Now Create a custom page in magento library,

<!---- First Create One Phtml File  ----->

==> Go to app/design/forntend/default/base/tamplet/catalog/product
==> Here Copy any phtml file,
==> Paste that copied file,
==> Change the name with your Name
==> Example ::=> featured.phtml

<!---- Second Create One Php File  ----->

==> Go to app/code/core/mage/catalog/block/product
==> Here Copy PHP file Which name is as your phtml file which you select and copy-paste,
==> Paste that PHP copied file,
==> Change the name with Same name of PHtml file and Must "First character be a Capital"
==> Example ::=> Featured.php


<!---- Third Add this code in Product controller File  ----->

==> Go to app/code/core/mage/catalog/controller
==> Put belove code in this file
==> Example ::=>

public function featuredAction()
{
            $this->loadLayout();
            $this->renderLayout();
}


<!---- Fourth Add this code in Catalog.xml File  ----->

==> Go to app/design/forntend/default/base/layout/catalog.xml
==> Put belove code in this file
==> Example ::=>

<catalog_product_featured>
  <reference name="content">
<block name="category" template="catalog/product/featured.phtml" type="catalog/product_featured" as="featured"/>
  </reference>
</catalog_product_featured>
  
D)  Now Simply copy and paste below code inside featured.phtml file,
 
<?php $featured_products = $this->getFeatureproduct(); ?>
<?php shuffle($featured_products); ?>
<div class="box recently" style="padding-left:15px; padding-right:15px;">
<h3><?php echo $this->__('Featured Products') ?></h3>
<div class="listing-type-grid  catalog-listing">
<?php $_collectionSize = count($featured_products) ?>
<table cellspacing="0" class="recently-list" id="product-list-table">
<?php $i=0; foreach ($featured_products as $_res): ?>
<?php $_product = Mage::getModel('catalog/product')->load($_res['product_id']); ?>
<?php if ($i++%3==0): ?>
<tr>
<?php endif ?>
<td>
<div>
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" 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->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" /></a>
</div>
<p><a class="product-name" href="<?php echo $_product->getProductUrl() ?>" title="< ?php echo $this->htmlEscape($_product->getName()) ?>)"><?php echo $this->htmlEscape($_product->getName()) ?></a></p>
<?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
</td>
<?php if ($i%3==0 && $i!=$_collectionSize): ?>
</tr>
<?php endif ?>
<?php endforeach ?>
<?php for($i;$i%3!=0;$i++): ?>
<td class="empty-product">&amp;amp;amp;amp;amp;nbsp;</td>
<?php endfor ?>
<?php if ($i%3==0): ?>
<?php endif ?>
</table>
<script type="text/javascript">decorateTable('product-list-table')</script>
</div>
</div>


E)  Use this code inside Featured.php,
public function getFeatureproduct()
    {       
        $resource = Mage::getSingleton('core/resource');
        $read = $resource->getConnection('catalog_read');
        $productEntityIntTable = (string)Mage::getConfig()->getTablePrefix() . 'catalog_product_entity_int';
        $eavAttributeTable = $resource->getTableName('eav/attribute');
        $categoryProductTable = $resource->getTableName('catalog/category_product');
        $select = $read->select()
            ->distinct(true)
            ->from(array('cp'=>$categoryProductTable), 'product_id')
            ->join(array('pei'=>$productEntityIntTable), 'pei.entity_id=cp.product_id', array())
            ->joinNatural(array('ea'=>$eavAttributeTable))
            ->where('pei.value=1')
            ->where('ea.attribute_code="featured"');
        $res = $read->fetchAll($select);
        return $res;

    }

F)  Now, To dispaly Featured product on Home

    1. Goto Admin/cms/pages/
    2. click on Home Page
    3. Click on Content Tab and copy
     
        {{block type="catalog/product_featured" name="featured" template="catalog/product/featured.phtml}}

    
 

No comments:

Post a Comment