Call-back icon  Sales: Call 800.374.8146 (N America)|757.278.0920 (International)

Magento

Open Source eCommerce Evolved

Magento Forum

   
Page 1 of 2
“Featured Product” feature
 
andy101010
Jr. Member
 
Total Posts:  12
Joined:  2008-02-14
 

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

<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

class MyCompany_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract
{
    
public function getFeaturedProduct()
    
{
        
// instantiate database connection object
        
$storeId 0;
        
$categoryId $this->getRequest()->getParam('id'false);
        
$resource Mage::getSingleton('core/resource');
        
$read $resource->getConnection('catalog_read');
        
$productStoreTable $resource->getTableName('catalog/product_store');
        
$categoryProductTable $resource->getTableName('catalog/category_product');
        
        
// 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).

Step 6) Add new blocks to the app/etc/local.xml

Add

<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.  If you know how to query by a specific product attribute, please let me know.

Thanks,
Andy

 
Magento Community Magento Community
Magento Community
Magento Community
 
RoyRubin
Magento Team
 
Avatar
Total Posts:  843
Joined:  2007-08-07
Los Angeles, CA
 

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.

 Signature 

Roy Rubin
Magento Team

 
Magento Community Magento Community
Magento Community
Magento Community
 
sherrie
Enthusiast
 
Avatar
Total Posts:  783
Joined:  2007-12-14
Illinois, USA
 

This looks promising - keep up the good work, I would love to have that feature as an extension!

 Signature 

Creativity is falling in love with the world. – Dewitt Jones
Current Projects: Gladrag Industries & Aqua Gear Direct • Currently Running: PHP 5.2.5/MySQL 5.0.45/Magento 1.1.3 on HostGator

 
Magento Community Magento Community
Magento Community
Magento Community
 
Serial Killa
Member
 
Avatar
Total Posts:  61
Joined:  2008-03-22
Malaysia
 

Awesome, can’t wait to try this out ,

 Signature 

"I walk across the dreaming sands under the pale moon: through the dreams of countries and cities, past dreams of places long gone and times beyond recall.”

 
Magento Community Magento Community
Magento Community
Magento Community
 
Yoke Lee
Member
 
Avatar
Total Posts:  71
Joined:  2008-05-08
China
 

Andy,

I’ve tried these steps of yours.

But I’ve a trouble, how to put it on the homepage ?

featured.phtml wasn’t there, should i make it myself ? can you give me example code or file of it?

Thank you !

 Signature 

Hope it helps ! And please share with us if it is ! Thanks.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Anders Rasmussen
Sr. Member
 
Avatar
Total Posts:  127
Joined:  2007-10-16
Denmark
 

Yoke Lee:

Take a look at the wiki: http://www.magentocommerce.com/wiki/how_to_create_a_featured_product
It is an updated version of Andy’s feature.

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.

 Signature 

Anders Rasmussen
Crius (in Danish)

 
Magento Community Magento Community
Magento Community
Magento Community
 
lisali
Sr. Member
 
Avatar
Total Posts:  279
Joined:  2008-04-28
London, UK
 

This would be great to have as an extension, or integrated into the admin interface - I find this to be the main thing Magento lacks at this stage.

 Signature 

ExtraCall.com - UK Telecommunications & VOIP

 
Magento Community Magento Community
Magento Community
Magento Community
 
Yoke Lee
Member
 
Avatar
Total Posts:  71
Joined:  2008-05-08
China
 

@Anders :

Thank you! I’ll try that.

However I’ve read that article first before found this page, and lucky me, i can post questions here !

 Signature 

Hope it helps ! And please share with us if it is ! Thanks.

 
Magento Community Magento Community
Magento Community
Magento Community
 
jared
Jr. Member
 
Total Posts:  7
Joined:  2008-05-09
 

Looks like a feature that hopefully will be built in.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Adam Shuy
Member
 
Avatar
Total Posts:  41
Joined:  2008-02-20
 

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.

Can anybody out there help ASAP?

 
Magento Community Magento Community
Magento Community
Magento Community
 
chinesedream
Guru
 
Total Posts:  606
Joined:  2007-08-31
San Francisco, CA
 

Adam Shuy, have you tried this?

http://www.magentocommerce.com/boards/viewthread/4454/

Look for dan_w’s post (#9)

 Signature 

I am orangutan Fatimah, I want to marry chimpanzee Tan ahming, so that when mankind extinct from his own doing, me and my cintaku Ahming will make a new race that respects all things on earth.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Anders Rasmussen
Sr. Member
 
Avatar
Total Posts:  127
Joined:  2007-10-16
Denmark
 

@Adam:

Did you remember to update local.xml as shown in step 6 in the wiki?

 Signature 

Anders Rasmussen
Crius (in Danish)

 
Magento Community Magento Community
Magento Community
Magento Community
 
Adam Shuy
Member
 
Avatar
Total Posts:  41
Joined:  2008-02-20
 

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.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Adam Shuy
Member
 
Avatar
Total Posts:  41
Joined:  2008-02-20
 

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.

 
Magento Community Magento Community
Magento Community
Magento Community
 
pelted
Jr. Member
 
Avatar
Total Posts:  4
Joined:  2008-05-23
 

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?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Anders Rasmussen
Sr. Member
 
Avatar
Total Posts:  127
Joined:  2007-10-16
Denmark
 

@pelted:

Are you saying that the php code for the class is printed out in the browser when you go to the category page instead of being interpreted?

 Signature 

Anders Rasmussen
Crius (in Danish)

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
Page 1 of 2
 
Sales: Call 800.374.8146 (North America) 757.278.0920 (International)
© Copyright 2008 Varien. Magento is a trademark of Irubin Consulting Inc. DBA Varien
Privacy Policy|Terms of Service
Magento Community Count
33431 users|325 users currently online|74324 forum posts