How To Create a Featured Product
来源:互联网 发布:软件用户操作手册 编辑:程序博客网 时间:2024/05/22 13:34
How To Create a Featured Product
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.
Step 1) Create new "Featured" attribute
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 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
Save the new attribute and go to Catalog → Attributes → Manage Attributes Sets to add the attribute to the default feature set.
Step 2) Add Block configuration to catalog.xml
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>
Step 3) Create new block class that will instantiate the featured product
Create a new file, and directories: app/code/local/MyCompany/Catalog/Block/Product/Featured.php
- <?php
- class MyCompany_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract
- {
- public function getFeaturedProduct()
- {
- // 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');
- $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
- if ($categoryId){
- $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"');}
- else {
- $select = $read->select()
- ->from(array('cp'=>$categoryProductTable))
- ->join(array('pei'=>$productEntityIntTable), 'pei.entity_id=cp.product_id', array())
- ->joinNatural(array('ea'=>$eavAttributeTable))
- ->where('pei.value=1')
- ->where('ea.attribute_code="featured"');
- }
- $featuredProductData = $read->fetchAll($select);
- $i=0;
- $product=array();
- $productid=array();
- foreach ($featuredProductData as $row) {
- // instantiate the product object
- //$productid[$i] = Mage::getModel('catalog/product')->load($row['product_id']);
- $productid[$i] = $row['product_id'];
- // if the product is a featured product, return the object
- // if ($product->getData('featured')) {
- //}
- $i++;
- }
- $productid=array_unique($productid);
- $i=0;
- foreach($productid as $id){
- $product[$i] = Mage::getModel('catalog/product')->load($id);
- $i++;
- }
- return $product;
- }
- }
- ?>
We’re almost there!
Step 4) Extend Mage_Catalog_Block_Category_View
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.
- <?php
- class MyCompany_Catalog_Block_Category_View extends Mage_Catalog_Block_Category_View
- {
- public function getFeaturedProductHtml()
- {
- return $this->getBlockHtml('product_featured');
- }
- }
- ?>
Step 5) Modify the templates
Edit app/design/frontend/default/default/template/catalog/category/view.phtml and add the following code:
- <?=$this->getFeaturedProductHtml()?>
right above this line:
- <?=$this->getProductListHtml()?>
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->getFeaturedProduct() ?>
- Check this out: <a href="<?php echo $_product->getProductUrl() ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a>
Step 6) Add new blocks to the app/etc/local.xml
Add the following inside the config global tag:
- <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>
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
- How To Create a Featured Product
- How to Create a Customer
- how to create a Makefile
- How to make multiple Featured Categories
- How to build a product with InstallShield
- How to Create a REST Protocol
- How to create a user in PostgreSQL
- How to create a javascript class~
- How to create a test plan?
- how to create a sln file
- How to: Create a Numeric Text Box
- How to create a minimal master page
- How to create/display a modal window?
- How to create a new routine
- How to create a PDF file, cheaply?
- How to create a FEA wizard?
- How to create a new process
- How to create a remote desktop user
- 拆分窗口的同步更新
- Servlet的生命周期,并说出Servlet和CGI的区别。
- _makepath和_makepath的使用案例
- play framework学习笔记之 framework ID
- 国楼市调控一周年透视:部分房价松动 整体格局僵持
- How To Create a Featured Product
- od 和 sed
- Gstreamer的一些基本概念与A/V同步分析(转载)
- [原创] Microsoft Word 2010 关闭不正常(提示:已停止工作,Windows正在检查该问题的解决方案。。) 问题的解决
- 线性表经典-选首领问题
- 在C++中怎样获得数组的大小?(原文标题:How Would You Get the Count of an Array in C++?)
- 流媒体行业需了解语言、常用软件、开发工具、相关协议及开发思想
- 各种历史以及我们在技术浪潮中的位置
- ava SE 6 新特性: Instrumentation 新功能