Magento - 创建Feature Products

Create featured product using custom attribute, show in home page:

1st step:- create a attribute featured with yes/no property from Catalog Input Type for Store Owner dropdown
include in attribute set
assign one product featured “yes” from manage products

2nd step: Cms pages -> home page content area write

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


3rd step: create custom module and block file “app/code/local/FeaturedProduct/Catalog/Block/Product/Featured.php”

<?php
class FeaturedProduct_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract{
  public function getFeaturedProduct() {
    //database connection object
    $storeId = Mage::app()->getStore()->getId();    
     
    $resource = Mage::getSingleton('core/resource');
    $read = $resource->getConnection('catalog_read');
    $categoryProductTable = $resource->getTableName('catalog/category_product');
    
    $productEntityIntTable = (string)Mage::getConfig()->getTablePrefix() . 'catalog_product_entity_int';
    $eavAttributeTable = $resource->getTableName('eav/attribute');
  
    // Query for featured product
    $select = $read->select()
             ->from(array('cp'=>$categoryProductTable))
             ->join(array('pei'=>$productEntityIntTable), 'pei.entity_id=cp.product_id', array())
             ->joinNatural(array('ea'=>$eavAttributeTable))
             ->where('pei.value=1')
             ->where('ea.attribute_code="featured"');
    
    $row = $read->fetchRow($select);

    return Mage::getModel('catalog/product')->setStoreId($storeId)->load($row['product_id']);
  }
} 


4th step: Create view file “app/design/frontend/base/default/template/catalog/product/featured.phtml”
<div>
  <p><?php $_product=$this->getFeaturedProduct() ?></p>
<?php //print_r($_product);?>
<?php //Product Image ?>
  <div style="float: left;"> <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>&nbsp;&nbsp;</div>
  <div style="float: left;">
    <p> <a href="<?php echo $_product->getProductUrl() ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></p>
    <p><?php echo $this->htmlEscape($_product->getShortDescription()) ?></p>
  </div>
</div>


5th step: add this code under global tag in “app/etc/local.xml”
<blocks>
  <catalog>
    <rewrite>
      <product_featured>FeaturedProduct_Catalog_Block_Product_Featured</product_featured>
    </rewrite>
  </catalog>
</blocks>


Done.

你可能感兴趣的:(agent)