在Magento產品分類頁面建立推薦產品

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

在進行Magento的相關操做的時候,你可能都想在產品分類頁面添加上這一類產品的推薦產品FeaturedProducts),這類產品通常是銷售比較好的,或者是你的利潤比較大的產品,那咱們應該怎麼樣添加上這類產品呢?下面是描述如何顯示一組推薦產品(Featured Product)。 Featured產品須要在後臺爲產品增長一個Featured屬性。 當管理員在Featured屬性上選擇Yes時,該產品就以Block的形式顯示在產品列表頁。html

步驟 1) 建立一個」Featured」屬性數據庫

進入後臺Catalog > Attributes > Manage Attributes > Add New Attribute. 添加一個新的屬性
Attribute Propertiesapp

  • 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 Propertiesfrontend

  • 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/Optionsfetch

  • Default: Featured Product
  • English: Featured Product

而後保存,而後去Catalog → Attributes → Manage Attributes Sets,把該屬性加入到默認屬性集。ui

步驟2). 加一個Block配置到catalog.xmlthis

打開 app/design/frontend/default/default/layout/catalog.xml. 咱們在默認Category Layout標籤的product list block上方加一個新的Block. 差很少在該文件的73行
Layerout代碼spa


  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代碼code

  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().顯示到網頁上

相關文章
相關標籤/搜索