首页
外贸B2C模板开发
zencart 开发
Magento开发
经典客户案例
zencart 报价
外贸主机
联系我们
 
 
首页 ::  Magento开发 ::  Magento二次开发 ::  magento - 创建一组推荐产品(Featured Products)
 
magento - 创建一组推荐产品(Featured Products)

  作者: Magento   |   2010-07-01     阅读次数:1018     关键字:magento程序,magento程序修改


本文将描述如何显示一组推荐的产品(Featured Product,以后称Featured产品)。 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行

代码:
<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
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
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 ,在

代码:
<?php echo $this->getProductListHtml()?>

的上面一行加入:

代码:
<div style="border: 1px green solid"><h4>Featured Products</h4> getFeaturedProductsHtml()?>

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

代码:
getFeaturedProducts() ?>


    <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),内容如下:

代码:
<?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().






Magento二次开发的相关新闻


更早的新闻

 
 
 
外贸网站模板  |  关于我们  |  核心技术  |  人才招聘  |  帮助中心  |  zencart下载  |  付款方式  |  公司地图  |  联系我们


粤ICP备15004589号-1
深圳市迅捷网络技术有限公司 Copyright © 2004-2016 All Rights Reserved. 版权所有
总公司:深圳市龙华新区民治街道人民路中航天逸大厦A3栋15层     网址:www.zencart-cn.com  电话:0755-28107785
分公司:惠州市惠城区港惠新天地商业大厦2B栋19层 网址:www.0752web.com 电话:0752-2695554  2065565
全国服务热线:137 14307967   售后服务热线:159 19478387 客服QQ: 1512144 408079953 MSN:szsun@msn.com