Bài viết hướng dẫn tạo danh sách sản phẩm tiêu biểu trong Magento

Last modified by Mr.Hai on Fri, January 2, 2009 09:05
Source|Old Revisions  |  Back To Group

This is an old revision of the document!


  

Hướng dẫn này sẽ cho bạn thấy làm thế nào để hiển thi6 một sản phẩm tiêu biểu (đặc trưng). Các sản phẩm đặc trưng là một sản phẩm có một thuộc tính được thêm vào trong phần quản trị.

Tôi sẽ giải thích về mỗi bước đi để làm việc tùy chỉnh tính năng này được nhanh chóng.

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

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

  1. <?php
  2. class MyCompany_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract
  3. {
  4.     public function getFeaturedProduct()
  5.     {
  6.         // instantiate database connection object
  7.         $categoryId = $this->getRequest()->getParam('id', false);
  8.         $resource = Mage::getSingleton('core/resource');
  9.         $read = $resource->getConnection('catalog_read');
  10.         $categoryProductTable = $resource->getTableName('catalog/category_product');
  11.         //$productEntityIntTable = $resource->getTableName('catalog/product_entity_int'); // doesn't work :(
  12.         $productEntityIntTable = (string)Mage::getConfig()->getTablePrefix() . 'catalog_product_entity_int';
  13.         $eavAttributeTable = $resource->getTableName('eav/attribute');
  14.    
  15.         // Query database for featured product
  16.         $select = $read->select()
  17.                        ->from(array('cp'=>$categoryProductTable))
  18.                        ->join(array('pei'=>$productEntityIntTable), 'pei.entity_id=cp.product_id', array())
  19.                        ->joinNatural(array('ea'=>$eavAttributeTable))
  20.                        ->where('cp.category_id=?', $categoryId)
  21.                        ->where('pei.value=1')
  22.                        ->where('ea.attribute_code="featured"');
  23.        
  24.         $row = $read->fetchRow($select);
  25.         return Mage::getModel('catalog/product')->load($row['product_id']);
  26.     }
  27. }
  28. ?>

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.

  1.     class MyCompany_Catalog_Block_Category_View extends Mage_Catalog_Block_Category_View
  2.     {
  3.         public function getFeaturedProductHtml()
  4.         {
  5.             return $this->getBlockHtml('product_featured');
  6.         }
  7.     }

Step 5) Modify the templates

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

  1.     <?=$this->getFeaturedProductHtml()?>

right above this line:

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

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

  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




 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs