首页 > 未分类 > 在Magento产品分类页面创建推荐产品(Featured Products)

在Magento产品分类页面创建推荐产品(Featured Products)

在进行Magento的相关操作的时候,你可能都想在产品分类页面添加上这一类产品的 ),这类产品一般是销售比较好的,或者是你的利润比较大的产品,那我们应该怎么样添加上这类产品呢?下面是描述如何显示一组( Product)。 产品需要在后台为产品增加一个属性。 当管理员在属性上选择Yes时,该产品就以Block的形式显示在产品列表页。

步骤 1) 创建一个””属性

进入后台Catalog > Attributes > Manage Attributes > Add New Attribute. 添加一个新的属性
Attribute Properties

  • Attribute Identifier:
  • Scope: Store View
  • Catalog Input Type for Store Owner: Yes/No
  • Unique Value (not shared with other ): 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

然后保存,然后去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代码

  1. <catalog_category_default>
  2. <block type=”catalog/product_” name=”product_” as=”product_” template=”catalog/product/.phtml”></block>
  3. …..
  4. </catalog_category_default>

步骤 3) 创建一个新的Block类从数据库取出所有产品
PHP代码

  1. <?php
  2. class MyCompany_Catalog_Block_Product_ extends Mage_Catalog_Block_Product_Abstract
  3. {
  4. public function get(){
  5. $ids = $this->_getIds();
  6. $collection = Mage::getModel(‘catalog/product’)->getCollection();
  7. $collection->getSelect()->where(“e.entity_id in (?)”, $ids);
  8. $collection->addAttributeToSelect(‘name’);
  9. $productList = $collection->load();
  10. return $productList;
  11. }
  12. public function _getIds(){
  13. // instantiate database connection object
  14. $categoryId = $this->getRequest()->getParam(‘id’, false);
  15. $resource = Mage::getSingleton(‘core/resource’);
  16. $read = $resource->getConnection(‘catalog_read’);
  17. $categoryProductTable = $resource->getTableName(‘catalog/category_product’);
  18. //$productEntityIntTable = $resource->getTableName(‘catalog/product_entity_int’); // doesn’t work :(
  19. $productEntityIntTable = (string)Mage::getConfig()->getTablePrefix() . ‘catalog_product_entity_int’;
  20. $eavAttributeTable = $resource->getTableName(‘eav/attribute’);
  21. // Query database for product
  22. $select = $read->select()
  23. ->from(array(‘cp’=>$categoryProductTable))
  24. ->join(array(‘pei’=>$productEntityIntTable), ‘pei.entity_id=cp.product_id’, array())
  25. ->joinNatural(array(‘ea’=>$eavAttributeTable))
  26. ->where(‘cp.category_id=?’, $categoryId)
  27. ->where(‘pei.value=1′)
  28. ->where(‘ea.attribute_code=””‘);
  29. $rows = $read->fetchAll($select);
  30. $ids = array();
  31. foreach($rows AS $row) {
  32. $ids[] = $row[‘product_id’];
  33. }
  34. $ret = implode(“,”, $ids);
  35. return $ids;
  36. }
  37. }
  38. ?>

步骤4): 扩展Mage_Catalog_Block_Category_View

创建一个文件叫app/code/local/MyCompany/Catalog/Block/Category/View.php.

PHP代码

  1. <?php
  2. class MyCompany_Catalog_Block_Category_View extends Mage_Catalog_Block_Category_View
  3. {
  4. public function getHtml()
  5. {
  6. return $this->getBlockHtml(‘product_’);
  7. }
  8. }
  9. ?>

步骤5): 修改模板文件
1). 编辑 app/design/frontend/default/default/template/catalog/category/view.phtml ,在
Template代码

  1. <?php echo $this->getProductListHtml()?>

的上面一行加入:

Template代码

  1. <div style=”border: 1px green solid”><h4> </h4> <?php echo $this->getHtml()?> </div>

2). 创建app/design/frontend/default/default/template/catalog/product/.phtml
Template代码

  1. <?php $_=$this->get() ?>
  2. <ul>
  3. <?php foreach($_ AS $_product) { ?>
  4. <li><a href=”<?php echo $_product->getProductUrl() ?>”><?php echo $this->htmlEscape($_product->getName()) ?></a></li>
  5. <?php  }
  6. ?>
  7. </ul>

步骤 6) 加一个新的块到app/etc/local.xml,最好的做法是建立一个新的文件app/etc/modules/MyCompany_Catalog.xml(文件名不必一定是MyCompany_Catalog),内容如下:
Layerout代码

  1. <?xml version=”1.0″?>
  2. <config>
  3. <global>
  4. <blocks>
  5. <catalog>
  6. <rewrite>
  7. <product_>MyCompany_Catalog_Block_Product_</product_>
  8. </rewrite>
  9. <rewrite>
  10. <category_view>MyCompany_Catalog_Block_Category_View</category_view>
  11. </rewrite>
  12. </catalog>
  13. </blocks>
  14. </global>
  15. </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> </h4> <?php echo $this->getHtml()?> </div>
b. MyCompany_Catalog_Block_Category_View->getHtml() {
$this->getBlockHtml(‘product_’);
}
c. 在Catalog.xml中找到product_块的定义,调用对应的phtml文件: app/design/frontend/default/default/template/catalog/product/.phtml
d. .phtml文件中<?php $_=$this->get() ?> 调用对应Block的关键方法get().显示到网页上

Advertisements
分类:未分类
  1. 还没有评论。
  1. No trackbacks yet.

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: