在Magento产品分类页面创建推荐产品

时光总嘲笑我的痴心妄想 提交于 2020-03-02 18:49:30

原文:http://shuimu.js.cn/page-created-in-magento-products-recommended-products-featured-products.html

在进行Magento的相关操作的时候,你可能都想在产品分类页面添加上这一类产品的推荐产品FeaturedProducts),这类产品一般是销售比较好的,或者是你的利润比较大的产品,那我们应该怎么样添加上这类产品呢?下面是描述如何显示一组推荐产品(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代码


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

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

  1. <?php  
  2. class MyCompany_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract  
  3. {  
  4.     public function getFeaturedProducts(){  
  5.       $ids = $this->_getFeaturedProductsIds();  
  6.         
  7.       $collection = Mage::getModel(‘catalog/product’)->getCollection();  
  8.       $collection->getSelect()->where(“e.entity_id in (?)”, $ids);  
  9.       $collection->addAttributeToSelect(‘name’);  
  10.       $productList = $collection->load();  
  11.         
  12.       return $productList;  
  13.     }  
  14.       
  15.     public function _getFeaturedProductsIds(){  
  16.         // instantiate database connection object  
  17.         $categoryId = $this->getRequest()->getParam(‘id’, false);  
  18.         $resource = Mage::getSingleton(‘core/resource’);  
  19.         $read = $resource->getConnection(‘catalog_read’);  
  20.         $categoryProductTable = $resource->getTableName(‘catalog/category_product’);  
  21.         //$productEntityIntTable = $resource->getTableName(‘catalog/product_entity_int’); // doesn’t work 

       
  22.         $productEntityIntTable = (string)Mage::getConfig()->getTablePrefix() . ‘catalog_product_entity_int’;  
  23.         $eavAttributeTable = $resource->getTableName(‘eav/attribute’);  
  24.         // Query database for featured product  
  25.         $select = $read->select()  
  26.                        ->from(array(‘cp’=>$categoryProductTable))  
  27.                        ->join(array(‘pei’=>$productEntityIntTable), ‘pei.entity_id=cp.product_id’, array())  
  28.                        ->joinNatural(array(‘ea’=>$eavAttributeTable))  
  29.                        ->where(‘cp.category_id=?’, $categoryId)  
  30.                        ->where(‘pei.value=1′)  
  31.                        ->where(‘ea.attribute_code=”featured”‘);  
  32.   
  33.                        $rows = $read->fetchAll($select);  
  34.         $ids = array();  
  35.         foreach($rows AS $row) {  
  36.           $ids[] = $row['product_id'];  
  37.         }  
  38.         $ret = implode(“,”, $ids);  
  39.         return $ids;  
  40.     }  
  41. }  
  42. ?>  

步骤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 getFeaturedProductsHtml()  
  5.     {  
  6.         return $this->getBlockHtml(‘product_featured’);  
  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>Featured Products</h4> <?php echo $this->getFeaturedProductsHtml()?> </div>

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

  1. <?php $_products=$this->getFeaturedProducts() ?>  
  2. <ul>  
  3. <?php foreach($_products 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_featured>MyCompany_Catalog_Block_Product_Featured</product_featured>  
  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>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().显示到网页上

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!