I’ve been working with Magento version 0.8.16100 for a couple days now trying to implement a Featured Product feature. The Featured Product is a product with an attribute I added from the admin. When the administrator selects “Yes” in the “Featured” attribute, that product will be displayed in a content block.
I’ll explain each step I took to make this custom feature. Please forgive me if I left anything out. The PHP itself I would like to rewrite to be more… professional, but I couldn’t figure out how to query a product by its attribute. Maybe you can help?
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
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 Configurable/Grouped Product: Yes
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
System Properties
Data Type for Saving in Database: String
Globally Editable: 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).
// Query database for all products in this category because I don't know how to query by attribute // this logic bothers me because I'm selecting and instantiating each product in the whole category // just to get the first featured product. The query should include the 'featured' attribute in the WHERE // clause. If you know how to do that, please share :) $select = $read->select() ->from(array('cp'=>$categoryProductTable)) ->join(array('ps'=>$productStoreTable), 'ps.product_id=cp.product_id', array()) ->where('ps.store_id=?', $storeId) ->where('cp.category_id=?', $categoryId); $featuredProductData = $read->fetchAll($select);
// iterate over result set from database foreach ($featuredProductData as $row) {
// instantiate the product object $product = Mage::getModel('catalog/product')->load($row['product_id']);
// if the product is a featured product, return the object if ($product->getData('featured')) { return $product; } }
return; } }
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.
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
I haven’t really integrated any real HTML changes, so I will simply point out the files to create and where to edit the HTML.
Edit app/design/frontend/default/default/template/catalog/category/view.phtml and add the following code:
<?=$this->getFeaturedProductHtml()?>
right above this line:
<?=$this->getProductListHtml()?>
Edit app/design/frontend/default/default/template/catalog/product/featured.phtml and customize your HTML here (see any of the product templates for Product object member variables).
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. If you know how to query by a specific product attribute, please let me know.
Great post Andy. I suggest you move this to the wiki so others can contribute any changes. Once Magento Connect will be online (very soon), you can certainly package this up into a formal Magento Extension.
You should make the featured.phtml template yourself. It should contain the html that defines the apperance of the featured product block.
In the template, you can put $_product = $this->getFeaturedProduct() to get the product, and then you can use it to get product info just as in catalog/product/view.phtml.
I follow your wiki, and online Magento course. But I can not show the featured product at front end.
I debug into it. It seem theline $layout->getBlock($name) of getBlockHtml function in the app/code/core/mage/core/abstract.php return null. So there is no block handler for the “featured_product” block.
But I did add the line of featured_product in the catalog.xml.
The config.xml under my featured product module works, otherwise Magento will not execute the getBlockHtml(’product_featured’) line in the new model CategoryView.php file. The problem happen when Magento parse catalog.xml, it did not store the new block for the featured.phtml under template/catalog/product folder.
Chinesedream,
I did read the thread, and I tried to add the template/catalog/product and the layout folder under my own theme, but it did not solve the problem.
I solve the problem now. When I created the catalog or product, I did not define the layout to either mytheme or default.
That is the cause to the problem.
I worked through the wiki tutorial a few times, and even with my php experiance, this one is confusing me as I am not fully up to speed on the Magento inheritance structure.
I get through all of this, and yea I left it all as MyCompany for now, but when browsing to a category page I now get this:
class MyCompany_Catalog_Block_Category_View extends Mage_Catalog_Block_Category_View { public function getFeaturedProductHtml() { return $this->getBlockHtml('product_featured'); } }class MyCompany_Catalog_Block_Category_View extends Mage_Catalog_Block_Category_View { public function getFeaturedProductHtml() { return $this->getBlockHtml('product_featured'); } }
And that’s it. Rather than extending Mage… it seems to be replacing it. I went through every thing step-by-step several times. No dice. Any ideas?