博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How To Create a Featured Product
阅读量:4199 次
发布时间:2019-05-26

本文共 5613 字,大约阅读时间需要 18 分钟。

How To Create a Featured Product

Last modified by
tranquang on Thu, October 21, 2010 13:34
|   
- Table of Contents

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

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).

  1.    
    <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

  1. <?php
  2. class MyCompany_Catalog_Block_
    Product _
    Featured
    extends Mage_Catalog_Block_
    Product _Abstract
  3.      
    {
  4.          
    public
    function get
    Featured
    Product
    (
    )
  5.          
    {
  6.  
  7.              
    // instantiate database connection object
  8.              
    $storeId = Mage::
    app
    (
    ) ->
    getStore
    (
    ) ->
    getId
    (
    ) ;   
  9.              
    $categoryId =
    $this ->
    getRequest
    (
    ) ->
    getParam
    (
    'id' ,
    false
    ) ;
  10.              
    $resource = Mage::
    getSingleton
    (
    'core/resource'
    ) ;
  11.              
    $read =
    $resource ->
    getConnection
    (
    'catalog_read'
    ) ;
  12.              
    $categoryProduct Table =
    $resource ->
    getTableName
    (
    'catalog/category_product '
    ) ;
  13.              
    //$product EntityIntTable = $resource->getTableName('catalog/product _entity_int'); // doesn't work :(
  14.              
    $product EntityIntTable =
    ( string
    ) Mage::
    getConfig
    (
    ) ->
    getTablePrefix
    (
    ) .
    'catalog_product _entity_int' ;
  15.              
    $eavAttributeTable =
    $resource ->
    getTableName
    (
    'eav/attribute'
    ) ;
  16.              
    // Query database for featured product
  17.              
    if
    (
    $categoryId
    )
    {
  18.              
    $select =
    $read ->
    select
    (
    )
  19.                              ->
    from
    (
    array
    (
    'cp' =>
    $categoryProduct Table
    )
    )
  20.                              ->
    join
    (
    array
    (
    'pei' =>
    $product EntityIntTable
    ) ,
    'pei.entity_id=cp.product _id' ,
    array
    (
    )
    )
  21.                              ->
    joinNatural
    (
    array
    (
    'ea' =>
    $eavAttributeTable
    )
    )
  22.                              ->
    where
    (
    'cp.category_id=?' ,
    $categoryId
    )
  23.                              ->
    where
    (
    'pei.value=1'
    )
  24.                              ->
    where
    (
    'ea.attribute_code="featured "'
    ) ;
    }
  25.                
    else
    {
  26.                
  27.                  
    $select =
    $read ->
    select
    (
    )
  28.                              ->
    from
    (
    array
    (
    'cp' =>
    $categoryProduct Table
    )
    )
  29.                              ->
    join
    (
    array
    (
    'pei' =>
    $product EntityIntTable
    ) ,
    'pei.entity_id=cp.product _id' ,
    array
    (
    )
    )
  30.                              ->
    joinNatural
    (
    array
    (
    'ea' =>
    $eavAttributeTable
    )
    )
  31.                              ->
    where
    (
    'pei.value=1'
    )
  32.                              ->
    where
    (
    'ea.attribute_code="featured "'
    ) ;
  33.                
    }
  34.              
    $featured Product Data =
    $read ->
    fetchAll
    (
    $select
    ) ;
  35.              
    $i =
    0 ;
  36.              
    $product =
    array
    (
    ) ;
  37.              
    $product id =
    array
    (
    ) ;
  38.              
    foreach
    (
    $featured Product Data
    as
    $row
    )
    {
  39.            
  40.                
    // instantiate the product object
  41.                
    //$product id[$i] = Mage::getModel('catalog/product ')->load($row['product _id']);
  42.                
    $product id
    [
    $i
    ] =
    $row
    [
    'product _id'
    ] ;
  43.            
  44.                
    // if the product is a featured product , return the object
  45.                
    // if ($product ->getData('featured ')) {
  46.                
  47.                
    //}
  48.                
    $i ++;
  49.            
    }
  50.        
    $product id =
    array_unique
    (
    $product id
    ) ;
  51.        
    $i =
    0 ;
  52.        
    foreach
    (
    $product id
    as
    $id
    )
    {
  53.            
    $product
    [
    $i
    ] = Mage::
    getModel
    (
    'catalog/product '
    ) ->
    load
    (
    $id
    ) ;
  54.            
    $i ++;
  55.        
    }
  56.        
    return
    $product ;
  57.        
    }
  58. }
  59. ?>

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.

  1. <?php
  2.    
    class MyCompany_Catalog_Block_Category_View
    extends Mage_Catalog_Block_Category_View
  3.    
    {
  4.        
    public
    function get
    Featured
    Product Html
    (
    )
  5.        
    {
  6.            
    return
    $this ->
    getBlockHtml
    (
    'product _featured '
    ) ;
  7.        
    }
  8.    
    }
  9. ?>

Edit app/design/frontend/default/default/template/catalog/category/view.phtml and add the following code:

  1.    
    <? =
    $this ->
    getFeatured Product Html
    (
    )
    ?>

right above this line:

  1.    
    <? =
    $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 :

  1. <?php
    $_product =
    $this ->
    getFeatured Product
    (
    )
    ?>
  2. 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:

  1.    
    <blocks>
  2.        
    <catalog>
  3.            
    <rewrite>
  4.                
    <product _featured > MyCompany_Catalog_Block_
    Product _
    Featured
    </product _featured >
  5.            
    </rewrite>
  6.            
    <rewrite>
  7.                
    <category_view> MyCompany_Catalog_Block_Category_View
    </category_view>
  8.            
    </rewrite>
  9.          
    </catalog>
  10.    
    </blocks>

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/

你可能感兴趣的文章
带你深入理解STL之Vector容器
查看>>
带你深入理解STL之Deque容器
查看>>
带你深入理解STL之Stack和Queue
查看>>
带你深入理解STL之Set和Map
查看>>
Redis源码剖析--源码结构解析
查看>>
Redis源码剖析--动态字符串SDS
查看>>
Redis源码剖析--双端链表Sdlist
查看>>
Redis源码剖析--字典dict
查看>>
Redis源码剖析--跳跃表zskiplist
查看>>
Redis源码剖析--整数集合Intset
查看>>
Redis源码剖析--对象object
查看>>
Redis源码剖析--字符串t_string
查看>>
Redis源码剖析--快速列表quicklist
查看>>
Redis源码剖析--列表list
查看>>
Android开发学习 之 五、基本界面控件-4时间控件
查看>>
详细解读Jquery的$.get(),$.post(),$.ajax(),$.getJSON()用法
查看>>
同步与异步的区别
查看>>
IT行业--简历模板及就业秘籍
查看>>
JNI简介及实例
查看>>
DOM4J使用教程
查看>>