When performing magento operations, you may want to add featuredproducts to the product category page. These products are generally sold well, or if you have a large profit, how should we add such products? The following describes how to display a group of Recommendation products ). The featured attribute must be added to the backend of the featured product. When the Administrator selects yes on the featured attribute, the product is displayed as a block on the product list page.
Step 1) create a "featured" attribute
Go to the background catalog> attributes> Manage attributes> Add new attribute. Add a 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 'droplow'): No
- Visible on catalog pages on front-end: Yes
Manage label/options
- Default: featured product
- English: featured product
Save the settings, go to catalog → attributes → manage attributes sets, and add the attribute to the default attribute set.
Step 2) Add a block to configure it to catalog. xml
Open APP/design/frontend/default/layout/catalog. XML. add a new block above the product list block of the default category layout tag. almost 73 lines in the file
Layerout code
- <Catalog_category_default>
- <Block type = "catalog/product_featured" name = "product_featured" as = "product_featured" template = "catalog/product/featured. phtml"> </block>
- .....
- </Catalog_category_default>
|
Step 3) create a new block class to retrieve all Featured Products from the database
PHP code
- <? 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;
- }
- }
- ?>
|
Step 4): Extended mage_catalog_block_category_view
Create a file named APP/code/local/mycompany/CATALOG/block/category/view. php.
PHP code
- <? PHP
- Class mycompany_catalog_block_category_view extends mage_catalog_block_category_view
- {
- Public Function getfeaturedproductshtml ()
- {
- Return $ this-> getblockhtml ('product _ featured ');
- }
- }
- ?>
|
Step 5): Modify the Template File
1) edit app/design/frontend/default/template/CATALOG/category/view. phtml in
Template code
- <? PHP echo $ this-> getproductlisthtml ()?>
|
Add the following line to the above line:
Template code
- <Div style = "border: 1px green solid"> <H4> Featured Products </H4> <? PHP echo $ this-> getfeaturedproductshtml ()?> </Div>
|
2) create APP/design/frontend/default/template/CATALOG/product/featured. phtml
Template code
- <? PHP $ _ products = $ this-> getfeaturedproducts ()?>
- <Ul>
- <? PHP foreach ($ _ products as $ _ product) {?>
- <Li> <a href = "<? PHP echo $ _ product-> getproducturl ()?> "> <? PHP echo $ this-> htmlescape ($ _ product-> getname ()?> </A> </LI>
- <? PHP}
- ?>
- </Ul>
|
Step 6) Add a new block to APP/etc/local. XML, the best way is to create a new file APP/etc/modules/mycompany_catalog.xml (the file name must not be mycompany_catalog), the content is as follows:
Layerout code
- <? 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>
|
Analysis:
Step 2) Add a block to configure it to catalog. xml
Step 5): Modify the template file. Step 1) edit app/design/frontend/default/template/CATALOG/category/view. phtml
This example proves that some extensions involve changes to the original system.
When running a product category webpage, the APP/design/frontend/default/template/CATALOG/category/view. phtml is displayed. The call sequence is as follows:
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. Find the product_featured block definition in catalog. xml and call the corresponding phtml file: APP/design/frontend/default/template/CATALOG/product/featured. phtml
D. Featured. phtml file <? PHP $ _ products = $ this-> getfeaturedproducts ()?> Call the key method getfeaturedproducts () of the corresponding block to display it on the webpage.
Reference: http://www.igocommerce.com/read-htm-tid-13.html