如何添加一个feature产品,如何在magento左侧添加产品!
在使用magento的时候,左侧产品如何使用分类调用的话,在产品详细页面就会报出数据库出错的原因,左侧栏是不能放分类的,但是左侧栏我想动态添加产品怎么办?可以通过添加一个属性,然后调用
出来这个属性的方法调用出来,这样就可以了,呵呵。
方法步骤:
1
增加一个属性
在magento后台:Catalog > Attributes > Manage Attributes > Add New Attribute.
属性选项为:
Attribute Identifier: featured
Scope: Store View
Catalog Input Type for Store Owner: Yes/No
Unique Value (not shared with other product s): 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
2加入属性集
然后进入attribute set ,把这个添加属性添加到这个属性集中
然后去后台进入产品页面,将几个产品的featured属性设为yes,也就是添加几个数据用于测试!
3在xml中添加block
这里用的是在catalog.xml中!
<default>
<reference>
<block type ="catalog/product_featured " name ="product _featured " as ="product _featured " template ="catalog/product/featured.phtml" > </block>
</reference>
</default>
4
在app/code/core/Mage/Catalog/Block/Product下,建立文件Featured.php文件
<?php
class Mage_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract
{
public function getFeatured Product ( )
{
// instantiate database connection object
$storeId = Mage::app ( ) ->getStore ( ) ->getId ( ) ;
$categoryId = $this ->getRequest ( ) ->getParam ( 'id' , false ) ;
$resource = Mage::getSingleton ( 'core/resource' ) ;
$read = $resource ->getConnection ( 'catalog_read' ) ;
$categoryProduct Table = $resource ->getTableName ( 'catalog/category_product ' ) ;
//$product EntityIntTable = $resource->getTableName('catalog/product _entity_int'); // doesn't work :(
$product EntityIntTable = ( string) Mage::getConfig ( ) ->getTablePrefix ( ) . 'catalog_product _entity_int' ;
$eavAttributeTable = $resource ->getTableName ( 'eav/attribute' ) ;
// Query database for featured product
if ( $categoryId ) {
$select = $read ->select ( )
->from ( array ( 'cp' =>$categoryProduct Table ) )
->join ( array ( 'pei' =>$product EntityIntTable ) , '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 "' ) ;}
else {
$select = $read ->select ( )
->from ( array ( 'cp' =>$categoryProduct Table ) )
->join ( array ( 'pei' =>$product EntityIntTable ) , 'pei.entity_id=cp.product _id' , array ( ) )
->joinNatural ( array ( 'ea' =>$eavAttributeTable ) )
->where ( 'pei.value=1' )
->where ( 'ea.attribute_code="featured"' ) ;
}
$featured Product Data = $read ->fetchAll ( $select ) ;
$i =0 ;
$product =array ( ) ;
$product id =array ( ) ;
foreach ( $featured Product Data as $row ) {
// instantiate the product object
//$product id[$i] = Mage::getModel('catalog/product ')->load($row['product _id']);
$product id [ $i ] = $row [ 'product _id' ] ;
// if the product is a featured product , return the object
// if ($product ->getData('featured ')) {
//}
$i ++;
}
$product id =array_unique ( $product id ) ;
$i =0 ;
foreach ( $product id as $id ) {
$product [ $i ] = Mage::getModel ( 'catalog/product ' ) ->load ( $id ) ;
$i ++;
}
return $product ;
}
}
?>
5
建立phtml文件,
catalog/product/featured.phtml
在这个文件中的内容为
<?php
echo $this->getFeatured Product();
?>
6
到这里,就做完了,刷新缓存,就能看到结果,如果没有结果,可能是后台没有添加用于测试的产品数据,当然我这种方式是直接在Mage文件中做的,为了更好的升级问题,推荐做成magento插件的形式!
这个功能的原理就是利用magento的产品数据表中的结构为EAV模型,故在后台添加一个属性,然后把属性为这个值的产品数据调用出来,代码可能有一些空格等问题,如果不行,仔细查看一下,那里有
问题,可能在粘贴的时候出现一些小错误,比如多一些空格等问题。
到此,你可以在左侧栏动态调用出来自己想要的产品了!!
转载请表明出处:MagentoWater
如何添加一个feature产品,如何在magento左侧栏动态添加产品!
2011年5月10日星期二 Asia/Shanghai下午3:06:54
Posted in Magento开发
By