Create a recommendation product (Featured Products) on the magento product category page)

Source: Internet
Author: User
Tags php foreach

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

 

  1. <Catalog_category_default>
  2. <Block type = "catalog/product_featured" name = "product_featured" as = "product_featured" template = "catalog/product/featured. phtml"> </block>
  3. .....
  4. </Catalog_category_default>

Step 3) create a new block class to retrieve all Featured Products from the database
PHP code

 

  1. <? PHP
  2. Class mycompany_catalog_block_product_featured extends mage_catalog_block_product_abstract
  3. {
  4. Public Function getfeaturedproducts (){
  5. $ IDs = $ this-> _ getfeaturedproductsids ();
  6. $ Collection = Mage: GetModel ('catalog/product')-> getcollection ();
  7. $ Collection-> getselect ()-> where ("e. entity_id in (?)", $ IDs );
  8. $ Collection-> addattributetoselect ('name ');
  9. $ Productlist = $ collection-> load ();
  10. Return $ productlist;
  11. }
  12. Public Function _ getfeaturedproductsids (){
  13. // Instantiate database connection object
  14. $ Categoryid = $ this-> getrequest ()-> getparam ('id', false );
  15. $ Resource = Mage: getsingleton ('Core/resource ');
  16. $ READ = $ resource-> getconnection ('catalog _ read ');
  17. $ Categoryproducttable = $ resource-> gettablename ('catalog/category_product ');
  18. // $ Productentityinttable = $ resource-> gettablename ('catalog/product_entity_int '); // doesn' t work
  19. $ Productentityinttable = (string) Mage: getconfig ()-> gettableprefix (). 'catalog _ product_entity_int ';
  20. $ Eavattributetable = $ resource-> gettablename ('eav/attribute ');
  21. // Query database for featured product
  22. $ Select = $ read-> select ()
  23. -> From (Array ('cp' => $ categoryproducttable ))
  24. -> Join (Array ('pei '=> $ productentityinttable), 'pei. entity_id = CP. product_id', array ())
  25. -> Joinnatural (Array ('ea '=> $ eavattributetable ))
  26. -> Where ('cp. category_id =? ', $ Categoryid)
  27. -> Where ('pei. value = 1 ′)
  28. -> Where ('ea. attribute_code = "featured "');
  29. $ Rows = $ read-> fetchall ($ select );
  30. $ IDs = array ();
  31. Foreach ($ rows as $ row ){
  32. $ IDs [] = $ row ['product _ id'];
  33. }
  34. $ Ret = implode (",", $ IDs );
  35. Return $ IDs;
  36. }
  37. }
  38. ?>

Step 4): Extended mage_catalog_block_category_view

Create a file named APP/code/local/mycompany/CATALOG/block/category/view. php.

PHP code

 

  1. <? PHP
  2. Class mycompany_catalog_block_category_view extends mage_catalog_block_category_view
  3. {
  4. Public Function getfeaturedproductshtml ()
  5. {
  6. Return $ this-> getblockhtml ('product _ featured ');
  7. }
  8. }
  9. ?>

Step 5): Modify the Template File
1) edit app/design/frontend/default/template/CATALOG/category/view. phtml in
Template code

 

  1. <? PHP echo $ this-> getproductlisthtml ()?>

Add the following line to the above line:

Template code

 

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

 

  1. <? PHP $ _ products = $ this-> getfeaturedproducts ()?>
  2. <Ul>
  3. <? PHP foreach ($ _ products as $ _ product) {?>
  4. <Li> <a href = "<? PHP echo $ _ product-> getproducturl ()?> "> <? PHP echo $ this-> htmlescape ($ _ product-> getname ()?> </A> </LI>
  5. <? PHP}
  6. ?>
  7. </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

 

  1. <? XML version = "1.0"?>
  2. <Config>
  3. <Global>
  4. <Blocks>
  5. <Catalog>
  6. <Rewrite>
  7. <Product_featured> mycompany_catalog_block_product_featured </product_featured>
  8. </Rewrite>
  9. <Rewrite>
  10. <Category_view> mycompany_catalog_block_category_view </category_view>
  11. </Rewrite>
  12. </CATALOG>
  13. </Blocks>
  14. </Global>
  15. </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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.