原文: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
- <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代碼code
- <?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().顯示到網頁上