Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Featured Product Tabs On Homepage
 
Baseballtips
Sr. Member
 
Total Posts:  83
Joined:  2008-08-01
 

I’m need to be able to have featured products on my homepage in a tabs.

I’ve created a file called Feature.php in app/code/local/BBT/Catalog/Block/Product that looks like this:

<?php
class BBT_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract
{

    
protected $_categoryIds = array();
    protected 
$_categories = array();
    protected 
$_featuredProducts = array();
    
    function 
getFeaturedCategories() {
        
if (count($this->_categoryIds) > 0{
            
foreach ($this->_categoryIds as $id{
                $category 
Mage::getModel('catalog/category')->load($id);
                
array_push($this->_categories$category);
            
}
        }
        
return $this->_categories;
    
}
    
    
function getFeaturedProducts() {
        $featuredCategories 
$this->_categories;
        
$products Mage::getModel('catalog/product');
        
        foreach (
$featuredCategories as $category{
            $categoryName 
$category->getName();
            
$categoryProducts $products->getCollection()->addCategoryFilter($category);
            
$this->_featuredProducts[$categoryName] = array();
            foreach (
$categoryProducts as $product{
                array_push
($this->_featuredProducts[$categoryName]$product);
            
}
        }
        
return $this->_featuredProducts;
    
}

    
public function addCategory($catId{
        array_push
($this->_categoryIds$catId);
        
//$this->_categoryId = $catId; // add the category Id number to categoryIds
        
        
return $this;
    
}
}

I’ve created a template for that Block called featured.phtml in app/design/frontend/bbt/default/template/catalog/product:

<?php 
    $featuredCategories 
$this->getFeaturedCategories(); 
    
$featuredProducts $this->getFeaturedProducts();
?>
<hr />
<
h1>Featured Products Will Go Here</h1>
    <!--
open tabs-->
    <
ul class="action-list tabs">
        
<?php foreach ($featuredCategories as $featureTab): ?>
        
<li class="tab"><a href="#<?php echo $featureTab->getName(); ?> content"><?php echo $featureTab->getName(); ?><span class="tab-cap"></span></a></li>
        
<?php endforeach; ?>
    
</ul>
    <!--
close tabs-->
    
    
<?php 
        
foreach ($featuredCategories as $featureContent):
            
$catName $featureContent->getName();
    
?>
    
<!--open tab-content-->
    <
ul class="action-list tab-content">
        <
a name="#<?php echo $catName; ?> content"></a>
        
<?php
            
            
foreach ($featuredProducts[$catName] as $product):
        
?>
        
<li>
            <
a href="<?php echo $product->getUrl(); ?>"><?php echo $product->getName(); ?></a>
        </
li>
        
<?php endforeach; ?>
    
</ul>
    <!--
close tab-content-->
    
<?php endforeach; ?>
<hr />

Then I put this in my homepage using the CMS:

<reference name="content">
    <
block type="catalog/product_featured" name="home.catalog.product.featured" alias="product_featured" template="catalog/product/featured.phtml">
        <
action method="addCategory"><catId>41</catId></action>
        <
action method="addCategory"><catId>56</catId></action>
        <
action method="addCategory"><catId>86</catId></action>
    </
block>
</
reference>

Everything works except for the products. I can’t get it to output the names and URLs of the products like I want. But the tabs, anchors, and containers are all there.

Anyone have an idea how I can get this working?

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
Baseballtips
Sr. Member
 
Total Posts:  83
Joined:  2008-08-01
 

I’m still working on this. If anyone has some suggestions I’m all open.

Thanks everyone.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Baseballtips
Sr. Member
 
Total Posts:  83
Joined:  2008-08-01
 

I could do something closer to what’s in this thread for my getFeaturedProducts function, but I need to be able to filter by category id and I can’t figure out what the attribute is on a product that says which category it belongs to.

Anyone have an idea?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Baseballtips
Sr. Member
 
Total Posts:  83
Joined:  2008-08-01
 

my Feature.php looks like this now:

<?php
class BBT_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract
{
    
protected $_categoryIds = array();
    protected 
$_categories = array();
    protected 
$_featuredProducts = array();
    
    function 
getFeaturedCategories() {
        
if (count($this->_categoryIds) > 0{
            
foreach ($this->_categoryIds as $id{
                $category 
Mage::getModel('catalog/category')->load($id);
                
array_push($this->_categories$category);
            
}
        }
        
return $this->_categories;
    
}
    
    
function getFeaturedProducts($category{
        $categoryId 
$category->getId();
        
$productModel Mage::getModel('catalog/product');
        
        
$this->_featuredProducts $productModel->getCollection()
            ->
addAttributeToFilter('category_ids', array('eq'=>$categoryId))
            ->
addAttributeToSelect(array('name''url'));
        
        return 
$this->_featuredProducts;
    
}

    
public function addCategory($catId{
        array_push
($this->_categoryIds$catId);
        
//$this->_categoryId = $catId; // add the category Id number to categoryIds
        
        
return $this;
    
}
}

I can’t figure out what to use as the condition here

->addAttributeToFilter('category_ids', array('eq'=>$categoryId))
to get it to filter out only the selected category.
 
Magento Community Magento Community
Magento Community
Magento Community
 
Baseballtips
Sr. Member
 
Total Posts:  83
Joined:  2008-08-01
 

Never mind. I figured out what was up.

 
Magento Community Magento Community
Magento Community
Magento Community
 
eiocreative
Jr. Member
 
Total Posts:  21
Joined:  2009-01-24
 

Forums are not just to help you. If you figure something out then please take the time to explain so it might help someone else. Thanks.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Baseballtips
Sr. Member
 
Total Posts:  83
Joined:  2008-08-01
 

Well here’s what my files ended up looking like. There might be a more efficient/better way to do this, but I got this working so I’m good with it for now.

In the files and file names below, “BBT” is the name space that I am using for our Baseball Tips store. This will have to be changed to match the name space you have chosen for your template.

I created a file in app/code/local/BBT/Catalog/Block/Product/ called Featured.php.

<?php
class BBT_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract
{
    
protected $_categoryIds = array();
    protected 
$_categories = array();
    protected 
$_featuredProducts = array();
    
    function 
getFeaturedCategories() {
        
if (count($this->_categoryIds) > 0{
            
foreach ($this->_categoryIds as $id{
                $category 
Mage::getModel('catalog/category')->load($id);
                
array_push($this->_categories$category);
            
}
        }
        
return $this->_categories;
    
}
    
    
function getFeaturedProducts($selectedCat{
        $productModel 
Mage::getModel('catalog/product');
        
        
$this->_featuredProducts $productModel->getCollection()
            ->
addAttributeToSelect(array('name''url''price''buyer_incentive''short_description''status''availability'))
            ->
addCategoryFilter($selectedCat)
            ->
load();
        
        return 
$this->_featuredProducts;
    
}
    
public function addCategory($catId{
        array_push
($this->_categoryIds$catId);
        
//$this->_categoryId = $catId; // add the category Id number to categoryIds
        
        
return $this;
    
}
    
    
public function getPriceHtml($product)
    
{
        $this
->setTemplate('catalog/product/price.phtml');
        
$this->setProduct($product);
        return 
$this->toHtml();
    
}
}

I created a file in app/code/local/BBT/Catalog/etc/ called config.xml.

<?xml version="1.0"?>
<config>
    <global>
        <
blocks>
            <
catalog>
                <
rewrite>
                    <
product_featured>BBT_Catalog_Block_Product_Featured</product_featured>
                </
rewrite>
            </
catalog>
        </
blocks>
    </global>
</
config>

I created a file in app/etc/modules/ called BBT_All.xml.

<?xml version="1.0"?>
<config>
    <
modules>
        <
BBT_Catalog>
            <
codePool>local</codePool>
            <
active>true</active>
        </
BBT_Catalog>
    </
modules>
</
config>

I created a file in app/design/frontend/bbt/default/template/catalog/product/ called featured.phtml.

<?php 
    $featuredCategories 
$this->getFeaturedCategories(); 
    
$featuredProducts;
?>

<div class="featured-products has-tabs">
    <!--
open tabs-->
    <
ul class="action-list tabs">
        
<?php
            $tabCount 
1;
            
$tabTotal count($featuredCategories);
            
$tabClass;
            foreach (
$featuredCategories as $featureTab):
                if (
$tabCount >= $tabTotal && $tabTotal 1{
                    $tabClass 
'tab last';
                
else if ($tabCount == || $tabTotal == 1{
                    $tabClass 
'tab first active';
                
else {
                    $tabClass 
'tab';
                
}
        ?>
        
<li class="<?php echo $tabClass; ?>"><a href="#<?php echo $featureTab->getName(); ?> content"><?php echo $featureTab->getName(); ?><span class="tab-cap"></span></a></li>
        
<?php 
            $tabCount
++;
            endforeach;
        
?>
    
</ul>
    <!--
close tabs-->
    
    
<?php
        $contentCount 
1;
        
$contentClass;
        foreach (
$featuredCategories as $featureContent):
            
$catName $featureContent->getName();
            
$featuredProducts $this->getFeaturedProducts($featureContent);
            if (
$contentCount >= $tabTotal && $tabTotal 1{
                $contentClass 
'action-list tab-content last';
            
else if ($contentCount == || $tabTotal == 1{
                $contentClass 
'action-list tab-content first active';
            
else {
                $contentClass 
'action-list tab-content';
            
}
    ?>
    
<!--open tab-content-->
    <
ul class="<?php echo $contentClass; ?>">
        <
a name="<?php echo $catName; ?> content"></a>
        
<?php
            
foreach ($featuredProducts as $product):
        
?>
        
<li class="item">
            <
div class="product-image">
                <
img src="<?php echo $this->helper('catalog/image')->init($product, 'small_image'); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($product, 'small_image')) ?>" />
            </
div>
            <
h5 class="product-name"><a href="<?php echo $product->getProductUrl(); ?>"><?php echo $this->htmlEscape($product->getName()); ?></a></h5>
            <
div class="product-info">
                <
class="short-description"></p>
                <
class="buyer-incentive"><?php echo $product->getAttributeText('buyer_incentive'?></p>
                <
div class="price-and-buy">
                    <
strong class="product-price"><?php echo $this->getPriceHtml($product?></strong>
                    
<?php if ($product->isSaleable()): ?>
                    
<a href="<?php echo $product->getProductUrl(); ?>">Learn More</a>
                    
<?php else: ?>
                    
<div class="out-of-stock"><?php echo $this->__('Out of stock'?></div>
                    
<?php endif; ?>
                
</div>
            </
div>
        </
li>
        
<?php endforeach; ?>
    
</ul>
    <!--
close tab-content-->
    
<?php
        $contentCount
++;
        endforeach;
    
?>
</div>

continued in next post

 
Magento Community Magento Community
Magento Community
Magento Community
 
Baseballtips
Sr. Member
 
Total Posts:  83
Joined:  2008-08-01
 

Finally I edited my homepage in the CMS section of the backend to include this in the Layout Update XML

<reference name="content">
    <
block type="catalog/product_featured" name="home.catalog.product.jppicks" alias="product_jppicks" template="catalog/product/featured.phtml">
        <
action method="addCategory"><catId>171</catId></action>
        <
action method="addCategory"><catId>172</catId></action>
        <
action method="addCategory"><catId>173</catId></action>
        <
action method="addCategory"><catId>174</catId></action>
        <
action method="addCategory"><catId>175</catId></action>
    </
block>
    
    <
block type="catalog/product_featured" name="home.catalog.product.hotdvds" alias="product_hotdvds" template="catalog/product/featured.phtml">
        <
action method="addCategory"><catId>99</catId></action>
    </
block>
</
reference>

The “<catId>” nodes are exactly what they sound like, category IDs. You can include as many or as few as you want and there will be a tab created on the homepage for them. If you want to tabbed sections just create another block of the type “catalog/product_featured” and add categories to it. I did this so that popular dvds will be in a separate tab section than all of the other products.

The categories I’m using are set up so that they’re not displayed in the main navigation and they can’t be navigated to. The title of these categories will be pulled through and used as the name for your tabs so, if you use this, be sure to have category names that make sense to your customers.

This will output the HTML that is needed for tabs. Created the Javascript to make the HTML act like tabs is up to you.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Ubukus2
Member
 
Total Posts:  34
Joined:  2008-09-19
 

I found this - after some research - to use filter on product.list on categories :

‘category_ids’ => array(’eq’ => ‘435, 439’),

Little contribution that could help.

 
Magento Community Magento Community
Magento Community
Magento Community
 
upendrajetavat
Jr. Member
 
Total Posts:  1
Joined:  2012-03-01
 

i have create file feature.php file cinfig.xml file FPD_ALL.xml file feature.phtml file in given location bou featured product not display in my home page .

What is the problem please tell me

File Attachments
Feature.php  (File Size: 2KB - Downloads: 25)
config.xml  (File Size: 1KB - Downloads: 35)
featured.phtml  (File Size: 4KB - Downloads: 22)
FPD_All.xml  (File Size: 1KB - Downloads: 31)
 
Magento Community Magento Community
Magento Community
Magento Community
 
oleolehop
Jr. Member
 
Total Posts:  1
Joined:  2013-07-04
 

Simple Tabs Magento extension is pleasant for me. It helped me to solve the problem like your - now I advertise my products in the form of tabs with contents or not on my homepage. It has very simple configuration and its configuration take 5 minutes of my free time.

 
Magento Community Magento Community
Magento Community
Magento Community
 
billypop
Jr. Member
 
Total Posts:  2
Joined:  2013-07-07
 

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top