本文共 5613 字,大约阅读时间需要 18 分钟。
Last modified by
tranquang on Thu, October 21, 2010 13:34
| This tutorial will show you how to implement a Featured Product feature. The Featured Product is a product with an attribute added from the administrative UI. When the administrator selects “Yes” in the “Featured ” attribute, that product will be displayed in a content block on the category page.
I’ll explain each step I took to make this custom feature. Please forgive me if I left anything out.
Note: For me the featured product only showed up if the category was not an anchor .
Create a new attribute by going to Catalog > Attributes > Manage Attributes > Add New Attribute.
Attribute Properties
-
Attribute Identifier:
featured -
-
Catalog Input Type for Store Owner:
Yes/No -
Unique Value (not shared with other
product s):
No -
-
Input Validation for Store Owner:
None -
Apply To:
All Product Types
Front End Properties
-
-
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
Save the new attribute and go to Catalog → Attributes → Manage Attributes Sets to add the attribute to the default feature set.
Open MyCompany/app/design/frontend/default/default/layout/catalog.xml. We want to add a new <block> right above the product list block in the default category layout.
Insert the block configuration on line 73 (default catalog.xml).
-
<block type ="catalog/product _featured " name ="product _featured " as ="product _featured " template ="catalog/product /featured .phtml" > </block>
Create a new file, and directories: app/code/local/MyCompany/Catalog/Block/Product /Featured .php
-
-
class MyCompany_Catalog_Block_
Product _
Featured extends Mage_Catalog_Block_
Product _Abstract
-
-
public function get
Featured 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 -
-
$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 "' ) ;
} -
-
-
$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 ) ;
-
-
-
$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 ')) { -
-
-
-
-
$product id =
array_unique ( $product id ) ;
-
-
foreach ( $product id as $id ) { -
$product [ $i ] = Mage::
getModel ( 'catalog/product ' ) ->
load ( $id ) ;
-
-
-
-
-
-
We’re almost there!
Create a new file, and directories, called app/code/local/MyCompany/Catalog/Block/Category/View.php. We’re extending the core class here so our module will be separate from the core code base. When upgrading, we won’t have to worry about our code not working or having to patch files.
-
-
class MyCompany_Catalog_Block_Category_View
extends Mage_Catalog_Block_Category_View
-
-
public function get
Featured Product Html
( ) -
-
return $this ->
getBlockHtml ( 'product _featured ' ) ;
-
-
-
Edit app/design/frontend/default/default/template/catalog/category/view.phtml and add the following code:
-
<? =
$this ->
getFeatured Product Html ( ) ?>
right above this line:
-
<? =
$this ->
getProduct ListHtml ( ) ?>
Create app/design/frontend/default/default/template/catalog/product /featured .phtml and add some product info HTML to show the featured product . Here is an example that simply displays a link to the product :
-
<?php $_product =
$this ->
getFeatured Product ( ) ?> -
Check this out: <a href=
"<?php echo $_product ->getProduct Url() ?>" ><?php
echo $this ->
htmlEscape ( $_product ->
getName ( ) ) ?></a>
Add the following inside the config global tag:
-
-
-
-
<product _featured > MyCompany_Catalog_Block_
Product _
Featured </product _featured > -
-
-
<category_view> MyCompany_Catalog_Block_Category_View
</category_view> -
-
-
I hope this helps you add a “Featured Product ” feature. It certainly feels thorough, but if I left anything out, please let me know and I’ll be happy to help.
Thanks,
Andy
转载地址:http://vccli.baihongyu.com/