在进行Magento的相关操作的时候,你可能都想在产品分类页面添加上这一类产品的推荐产品(Featured Products),这类产品一般是销售比较好的,或者是你的利润比较大的产品,那我们应该怎么样添加上这类产品呢?下面是描述如何显示一组推荐产品(Featured Product)。 Featured产品需要在后台为产品增加一个Featured属性。 当管理员在Featured属性上选择Yes时,该产品就以Block的形式显示在产品列表页。
步骤 1) 创建一个”Featured”属性
进入后台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
- Visible on Catalog Pages on Front-end: Yes
Manage Label/Options
- Default: Featured Product
- English: Featured Product
然后保存,然后去Catalog → Attributes → Manage Attributes Sets,把该属性加入到默认属性集。
步骤2). 加一个Block配置到catalog.xml
打开 app/design/frontend/default/default/layout/catalog.xml. 我们在默认Category Layout标签的product list block上方加一个新的Block. 差不多在该文件的73行
Layerout代码
- <catalog_category_default>
- <block type=”catalog/product_featured” name=”product_featured” as=”product_featured” template=”catalog/product/featured.phtml”></block>
- …..
- </catalog_category_default>
|
步骤 3) 创建一个新的Block类从数据库取出所有Featured产品
PHP代码
- <?php
- class MyCompany_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract
- {
- public function getFeaturedProducts(){
- $ids = $this->_getFeaturedProductsIds();
-
- $collection = Mage::getModel(‘catalog/product’)->getCollection();
- $collection->getSelect()->where(“e.entity_id in (?)”, $ids);
- $collection->addAttributeToSelect(‘name’);
- $productList = $collection->load();
-
- return $productList;
- }
-
- public function _getFeaturedProductsIds(){
- // instantiate database connection object
- $categoryId = $this->getRequest()->getParam(‘id’, false);
- $resource = Mage::getSingleton(‘core/resource’);
- $read = $resource->getConnection(‘catalog_read’);
- $categoryProductTable = $resource->getTableName(‘catalog/category_product’);
- //$productEntityIntTable = $resource->getTableName(‘catalog/product_entity_int’); // doesn’t work 🙁
- $productEntityIntTable = (string)Mage::getConfig()->getTablePrefix() . ‘catalog_product_entity_int’;
- $eavAttributeTable = $resource->getTableName(‘eav/attribute’);
- // Query database 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(‘cp.category_id=?’, $categoryId)
- ->where(‘pei.value=1’)
- ->where(‘ea.attribute_code=”featured”‘);
-
- $rows = $read->fetchAll($select);
- $ids = array();
- foreach($rows AS $row) {
- $ids[] = $row[‘product_id’];
- }
- $ret = implode(“,”, $ids);
- return $ids;
- }
- }
- ?>
|
步骤4): 扩展Mage_Catalog_Block_Category_View
创建一个文件叫app/code/local/MyCompany/Catalog/Block/Category/View.php.
PHP代码
- <?php
- class MyCompany_Catalog_Block_Category_View extends Mage_Catalog_Block_Category_View
- {
- public function getFeaturedProductsHtml()
- {
- return $this->getBlockHtml(‘product_featured’);
- }
- }
- ?>
|
步骤5): 修改模板文件
1). 编辑 app/design/frontend/default/default/template/catalog/category/view.phtml ,在
Template代码
- <?php echo $this->getProductListHtml()?>
|
的上面一行加入:
Template代码
- <div style=”border: 1px green solid”><h4>Featured Products</h4> <?php echo $this->getFeaturedProductsHtml()?> </div>
|
2). 创建app/design/frontend/default/default/template/catalog/product/featured.phtml
Template代码
- <?php $_products=$this->getFeaturedProducts() ?>
- <ul>
- <?php foreach($_products AS $_product) { ?>
- <li><a href=”<?php echo $_product->getProductUrl() ?>”><?php echo $this->htmlEscape($_product->getName()) ?></a></li>
- <?php }
- ?>
- </ul>
|
步骤 6) 加一个新的块到app/etc/local.xml,最好的做法是建立一个新的文件app/etc/modules/MyCompany_Catalog.xml(文件名不必一定是MyCompany_Catalog),内容如下:
Layerout代码
- <?xml version=”1.0″?>
- <config>
- <global>
- <blocks>
- <catalog>
- <rewrite>
- <product_featured>MyCompany_Catalog_Block_Product_Featured</product_featured>
- </rewrite>
- <rewrite>
- <category_view>MyCompany_Catalog_Block_Category_View</category_view>
- </rewrite>
- </catalog>
- </blocks>
- </global>
- </config>
|
分析:
步骤2). 加一个Block配置到catalog.xml
步骤5): 修改模板文件的小步骤1), 编辑 app/design/frontend/default/default/template/catalog/category/view.phtml
该例证明:有些扩展涉及到对原系统的更改。
当运行某产品分类网页时,显示app/design/frontend/default/default/template/catalog/category/view.phtml ,接下来调用序列为:
a. <div style=”border: 1px green solid”><h4>Featured Products</h4> <?php echo $this->getFeaturedProductsHtml()?> </div>
b. MyCompany_Catalog_Block_Category_View->getFeaturedProductsHtml() {
$this->getBlockHtml(‘product_featured’);
}
c. 在Catalog.xml中找到product_featured块的定义,调用对应的phtml文件: app/design/frontend/default/default/template/catalog/product/featured.phtml
d. featured.phtml文件中<?php $_products=$this->getFeaturedProducts() ?> 调用对应Block的关键方法getFeaturedProducts().显示到网页上
本文参考自:http://www.igocommerce.com/read-htm-tid-13.html
评论