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

Page 1 of 4
“Featured” products block on home page - Not working --Help please
 
srinigenie
Guru
 
Avatar
Total Posts:  539
Joined:  2008-02-04
 

Hi all,

I had referred to the post
http://www.magentocommerce.com/boards/viewthread/4454/
and attempted to use this functionality of displaying products on the home page driven by a custom attribute ‘featured_yn’ which when set for a product will display this on my store home page. The Block class gets instantiated but the template HTML is not rendering :(

Followed the below steps:
1. Create an attribute “featured_yn” and set possible values to be “Yes” and “No”.
2. Attach the attribute to the attributeset.
3. Create the Block class app/code/local/XYZ/Catalog/Block/Product/Featured.php

<?php
class XYZ_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract
{
    
public function __construct()
    
{
        parent
::__construct();

        
$storeId    Mage::app()->getStore()->getId();
        
$product    Mage::getModel('catalog/product');
        
/* @var $product Mage_Catalog_Model_Product */
        
$todayDate  $product->getResource()->formatDate(time());
        
$products   $product->setStoreId($storeId)->getCollection()
            ->
addAttributeToFilter('featured_yn', array('Yes'=>true))
            ->
addAttributeToSelect(array('name''price''small_image'), 'inner')
            ->
addAttributeToSelect(array('special_price''special_from_date''special_to_date'), 'left')
        ;
       
/* @var $products Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection */
        
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
        
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);

      
$products->setOrder('hot_deals')->setPageSize(5)->setCurPage(1);

        
$this->setProductCollection($products);

    
}
}

4. Create the template file app/design/frontend/xyzInterface/default/template/catalog/product/featured.phtml

<?php 
echo 'Test check calling featured products =='.$_products->getSize();
if ((
$_products $this->getProductCollection()) && $_products->getSize()): ?>
<div class="home-page-cntr">
<?php $i=0; foreach ($_products->getItems() as $_product): ?>
    <?php 
if ($i>5): continue; endif; ?>

<div class="home-page-item">
        <
div class="home-page-img">
            <
a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>">
                 <
img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(65,65); ?>" alt="<?php echo $this->htmlEscape($_product->getName()) ?>"/>
            </
a>
        </
div>
        <
div class="home-page-txt">
            <
p><class="product-name" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>)"><?php echo $_product->getName() ?></a></p>
            
<?php echo $this->helper('review/product')->getSummaryHtml($_product'short'?>
            <?php 
echo $this->helper('catalog/product')->getPriceHtml($_product?>
            
</div>
</
div>
<?php $i++; endforeach; ?>
<?php 
for($i;$i%5!=0;$i++): ?>
    <?php 
endfor ?>
</div>
<?php endif; ?>

5. Add CSS markup. Added the required CSS markup in boxes.css

/* CUSTOM STYLES */
/* Controls featured items on home page */
.home-page-cntr{
    margin
15px 0 0 0;
    
font-size11px;
    
}
.home-page-cntr a{
    text
-decorationnone;
    
font-weightnormal;
    
}
.home-page-cntr a:hover {
    color
#000000;}
.homeContent h3 {
    width
150px;
    
font-size12px;
    
padding2px 5px;
    
color#fff;
    
background-color#4F3E2E;
}
.home-page-item, .home-page-item1{
    float
left;
    
width110px;
    
height120px;
    
padding8px 8px 8px 0;
    
margin5px 5px 10px 0px;
    
line-height16px;
    
}

.home-page-img{
    text
-aligncenter;
    
border1px solid #eee;
    
width100px;
    
height125px;
    
padding2px;
    
}
.home-page-img:hover {
    border
1px solid #006699;}
.home-page-txt{
    font
-size11px;
    
color#003366;
    
}

6. Update homepage CMS from admin login (CMS->Manage Pages->LayoutUpdate XML)

<reference name="content">
<
block type="catalog/product_featured" name="home.catalog.product.featured" alias="product_featured" template="catalog/product/featured.phtml"/>
<
block type="reports/product_viewed" name="home.reports.product.viewed" template="reports/home_product_viewed.phtml" after="product_new"/>
</
reference>

<
reference name="right">
  <
block type="reports/product_viewed" before="right.permanent.callout" name="left.reports.product.viewed" template="reports/product_viewed.phtml" />
            <
action method="unsetChild"><name>cart_sidebar</name></action>
            <
action method="unsetChild"><name>right.poll</name></action>
<
action method="unsetChild"><alias>right.reports.product.viewed</alias></action>
<
action method="unsetChild"><alias>right.reports.product.compared</alias></action>
</
reference>

7. Added block class in custom local module catalog config.xml file (app\code\local\XYZ\Catalog\etc\config.xml)

....
..
.
    <
blocks>
      <
catalog>
        <
rewrite>
          <
product_featured>XYZ_Catalog_Block_Product_Featured</product_featured>
        </
rewrite>        
      </
catalog>
    </
blocks>
<global>
..
..

Cache was disabled. The design interface is correctly set to “xyzInterface” in admin configuration for my store.

Can’t think of checking anything else :( ...can someone please help?

Thanks in advance!!!

 
Magento Community Magento Community
Magento Community
Magento Community
 
srinigenie
Guru
 
Avatar
Total Posts:  539
Joined:  2008-02-04
 

Any comments? Suggestions?

 
Magento Community Magento Community
Magento Community
Magento Community
 
cshontz
Sr. Member
 
Avatar
Total Posts:  91
Joined:  2007-11-07
 
srinigenie - 20 April 2008 08:07 PM

Any comments? Suggestions?

Did you ever get anywhere on this? I’m trying to figure out the same thing right now.

 
Magento Community Magento Community
Magento Community
Magento Community
 
srinigenie
Guru
 
Avatar
Total Posts:  539
Joined:  2008-02-04
 

kept staring at my code for enough number of times and gave up :( ... see if you can spot something wrong in my code

 
Magento Community Magento Community
Magento Community
Magento Community
 
cshontz
Sr. Member
 
Avatar
Total Posts:  91
Joined:  2007-11-07
 

Will do. Mine is nearly identical to yours, but I’m going to keep working at it. If I figure it out I’ll post up. Good luck!

 
Magento Community Magento Community
Magento Community
Magento Community
 
cshontz
Sr. Member
 
Avatar
Total Posts:  91
Joined:  2007-11-07
 

Well, that didn’t take long. I managed to get my featured products block to render by doing what you did, and then modifying (or creating) MyCompany.xml in app/etc/modules/, which essentially declares the module. So it looks like this: (in your case MyCompany = XYZ)

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

Hope that helps you, srinigenie! smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
bloggleme
Sr. Member
 
Avatar
Total Posts:  101
Joined:  2008-05-06
 

Hmm.. I tried this and nothing is rendering. Is XYZ the name of the module, or should it be the name of my theme? For instance, I have app/design/frontend/default/newtheme/

should xyz = newtheme?

Please help me out, I’m stuck.

 
Magento Community Magento Community
Magento Community
Magento Community
 
nafnaf1000
Sr. Member
 
Total Posts:  209
Joined:  2008-02-21
 

ok, got this working… grin

You can see it here…

http://www.hbaetc.com/magento/

all local codes. NO core changes so you will not have any problem with update (i hope,)

 
Magento Community Magento Community
Magento Community
Magento Community
 
nafnaf1000
Sr. Member
 
Total Posts:  209
Joined:  2008-02-21
 

Now lets soo of we can put in an opthion for random....

Please post if you know how to do that

 
Magento Community Magento Community
Magento Community
Magento Community
 
bloggleme
Sr. Member
 
Avatar
Total Posts:  101
Joined:  2008-05-06
 

Got mine working smile wish there was a way to display the products according to sales - kind of like a best sellers module. Or even a top rated module (based on product ratings).

 
Magento Community Magento Community
Magento Community
Magento Community
 
nafnaf1000
Sr. Member
 
Total Posts:  209
Joined:  2008-02-21
 

@bloggleme;

There should be a way to do that as the do it in the admin......

 
Magento Community Magento Community
Magento Community
Magento Community
 
bloggleme
Sr. Member
 
Avatar
Total Posts:  101
Joined:  2008-05-06
 

Oh yea I’m sure its possible. I’d never be able to find the functions though.

 
Magento Community Magento Community
Magento Community
Magento Community
 
nafnaf1000
Sr. Member
 
Total Posts:  209
Joined:  2008-02-21
 

i did it… you can turn it on and off in the back end smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
nafnaf1000
Sr. Member
 
Total Posts:  209
Joined:  2008-02-21
 

Followed the below steps:
1. Create an attribute “home_page_featured” and set possible values to be “Yes” and “No”.
2. Attach the attribute to the attribute set

add app/etc/modules/MyCompany_HomePageFeatured.xml

<?xml version="1.0"?>
<config>
    <
modules>
        <
MyCompany_HomePageFeatured>
            <
active>true</active>
            <
codePool>local</codePool>
        </
MyCompany_HomePageFeatured>
    </
modules>
</
config>
add app/code/local/MyCompany/HomePageFeatured/Block/Product/Featured.php
<?php
class MyCompany_HomePageFeatured_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract
{
    
public function __construct()
    
{
        parent
::__construct();

        
$storeId    Mage::app()->getStore()->getId();
        
$product    Mage::getModel('catalog/product');
        
/* @var $product Mage_Catalog_Model_Product */
        
$todayDate  $product->getResource()->formatDate(time());
        
$products   $product->setStoreId($storeId)->getCollection()
            ->
addAttributeToFilter('home_page_featured', array('Yes'=>true))
            ->
addAttributeToSelect(array('name''price''small_image'), 'inner')
            ->
addAttributeToSelect(array('special_price''special_from_date''special_to_date'), 'left')
        ;
       
/* @var $products Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection */
        
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
        
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);

      
$products->setOrder('hot_deals')->setPageSize(3)->setCurPage(1);

        
$this->setProductCollection($products);

    
}
}

add app/code/local/MyCompany/HomePageFeatured/etc/config.php

<?xml version="1.0"?>
<config>
    <
modules>
        <
MyCompany_HomePageFeatured>
            <
version>0.1.0</version>
        </
MyCompany_HomePageFeatured>
    </
modules>
    <global>
    <
blocks>
      <
catalog>
        <
rewrite>
          <
product_featured>MyCompany_HomePageFeatured_Block_Product_Featured</product_featured>
        </
rewrite>        
      </
catalog>
    </
blocks>
    </global>
</
config>
add block code in to CMS page.
<reference name="content">
<
block type="catalog/product_featured" name="home.catalog.product.featured" alias="product_featured" template="catalog/product/new.phtml"/></reference>

as you can see there is no PHTML file here as i am using the new.phtml as i want it to look the same. You can copy new.phtml (or what ever phtml you want ) and call it featured.phtml and then change it text from “NEW ITEMS” to “Featured Items”

have fun.

if it don not work let me know but it should work.

 
Magento Community Magento Community
Magento Community
Magento Community
 
bloggleme
Sr. Member
 
Avatar
Total Posts:  101
Joined:  2008-05-06
 

coolio, nafnaf, did you say you knew how to do a best sellers module? Or you found existing code for it? A Best sellers module depends on the # of sales on the product, instead of an attribute.

Let me know, thanks.

 
Magento Community Magento Community
Magento Community
Magento Community
 
nafnaf1000
Sr. Member
 
Total Posts:  209
Joined:  2008-02-21
 

In the admin there is a opthion to see bestsellers. So that tels me that the code is there and it can be done. I am not a programer so I can not just program it but the opthion is there. If it is in the back end it can be in the front end. I think I just have to re do the phtml file. I will work on it and let u know

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 4