Call-back icon  Sales: Call 877.832.5289 (N America)|310.295.4144 (International)

Magento

eCommerce Software for Online Growth

Magento Forum

   
Page 2 of 3
“Featured Product” feature
 
nafnaf1000
Sr. Member
 
Total Posts:  119
Joined:  2008-02-21
 

@pelted

did you forget to put a “<?php” on the top?

ps… I never get this code to work for me…

 
Magento Community Magento Community
Magento Community
Magento Community
 
redpen
Sr. Member
 
Total Posts:  232
Joined:  2008-01-23
 

Just got this working.  Can it be used to show more than one featured product per category?

 
Magento Community Magento Community
Magento Community
Magento Community
 
altansar
Member
 
Total Posts:  32
Joined:  2008-07-08
 

Hi everybody,

to begin I want to thanks Andy for this usefull “featured product” module.

It works for me, but I have a little question :

I would like to use this module to display products of any categories on my home page, instead of displaying a product in a category page.
So I tried to modify the code of the function getFeaturedProduct() in the app/code/local/MyCompany/Catalog/Block/Product/Featured.php file.
But I didn’t found how to select every products of my catalog, instead of selecting products from a particular category.

Thank you, and sorry for my english.

 
Magento Community Magento Community
Magento Community
Magento Community
 
altansar
Member
 
Total Posts:  32
Joined:  2008-07-08
 

I finally found a solution, it was not hard at least.

Here is the code to select all product marked as featured from any category :

public function getFeaturedProduct()
        
{
            $resource 
Mage::getSingleton('core/resource');
            
$read $resource->getConnection('catalog_read');
            
$productEntityIntTable = (string)Mage::getConfig()->getTablePrefix() . 'catalog_product_entity_int';
            
$eavAttributeTable $resource->getTableName('eav/attribute');
            
$categoryProductTable $resource->getTableName('catalog/category_product');
            
            
$select $read->select()
                ->
from(array('cp'=>$categoryProductTable))
                ->
join(array('pei'=>$productEntityIntTable), 'pei.entity_id=cp.product_id', array())
                ->
joinNatural(array('ea'=>$eavAttributeTable))
                
                ->
where('pei.value=1')
                ->
where('ea.attribute_code="featured"');
                
            
$row $read->fetchRow($select);
                
            return ((
Mage::getModel('catalog/product')->load($row['product_id'])));
        
}

Now i’m going to try to display a random item from all the items marked as featured, because I actually get the same item at every refresh.
I’ll post the code here if I find something.

 
Magento Community Magento Community
Magento Community
Magento Community
 
altansar
Member
 
Total Posts:  32
Joined:  2008-07-08
 

Ok, I finally have a working function to display a random article from theire who are marked as featured on my home page. You have to follow this tutorial and just change the function getFeaturedProduct() from app/code/local/MyCompany/Catalog/Block/Product/Featured.php by this code :

public function getFeaturedProduct()
        
{
            $resource 
Mage::getSingleton('core/resource');
            
$read $resource->getConnection('catalog_read');
            
$productEntityIntTable = (string)Mage::getConfig()->getTablePrefix() . 'catalog_product_entity_int';
            
$eavAttributeTable $resource->getTableName('eav/attribute');
            
$categoryProductTable $resource->getTableName('catalog/category_product');
            
            
$select $read->select()
                ->
from(array('cp'=>$categoryProductTable))
                ->
join(array('pei'=>$productEntityIntTable), 'pei.entity_id=cp.product_id', array())
                ->
joinNatural(array('ea'=>$eavAttributeTable))
                
                ->
where('pei.value=1')
                ->
where('ea.attribute_code="featured"');
            
            
$res $read->fetchAll($select);
            return ((
Mage::getModel('catalog/product')->load($res[array_rand($res)]['product_id'])));
        
}

 
Magento Community Magento Community
Magento Community
Magento Community
 
brianpat
Member
 
Total Posts:  38
Joined:  2008-04-05
 

I did as being said in the tutorial in wiki page. But it doesn’t show on any page. Is it working for any person? If so can anyone put this in the extension?

 
Magento Community Magento Community
Magento Community
Magento Community
 
cibernoid
Sr. Member
 
Avatar
Total Posts:  205
Joined:  2008-02-12
 
brianpat - 09 July 2008 07:35 AM

I did as being said in the tutorial in wiki page. But it doesn’t show on any page. Is it working for any person? If so can anyone put this in the extension?

I’ve just tried this and it doesn’t wokr to me. When I add the block on file “local.xml” all category pages are blank (without products).

 
Magento Community Magento Community
Magento Community
Magento Community
 
Ron Phillips
Member
 
Total Posts:  49
Joined:  2008-04-16
 

Not to steal the significance of Andy’s thread, http://www.magentocommerce.com/boards/viewthread/4780/P0/, as it’s what got me started modding out my functions. And to get this to work you need to follow the same steps as far as getting this in place, so I won’t be redundant. Also I’m not the best of documenters.

Simply though looking at the way the Magento team handled New and Random, I simply thought, why don’t we do this with the Featured function. It would make it cleaner and utilize more of the core functions instead of using Zend DB to figure out how to build a query statement. So I don’t take much credit for this. It was already there, finding it was a chore though.

I did want it to be flexible though so that I can make it global or by category, change the order and the product number. And please note that it still is a bit custom for my needs and I’m pulling only the attributes I need.

public function getProductFeatured(){
        
    $product 
Mage::getModel('catalog/product');
    if(
$this->getCategoryId())
        
$category Mage::getModel('catalog/category')->load($this->getCategoryId());
    
    
$collection $product->getCollection()
            ->
addAttributeToSelect('name')
            ->
addAttributeToSelect('price')
            ->
addAttributeToSelect('image')
            ->
addAttributeToSelect('tax_class_id')
            ->
addAttributeToFilter('featured',array('yes'=>true));

    if(
$category)
    
$collection->addCategoryFilter($category);

    
$collection ->addStoreFilter();        
        
    if(
$this->getOrderby())    
        
$collection->getSelect()->order($this->getOrderby());
    if(
$this->getNumProducts())
        
$collection->getSelect()->limit($this->getNumProducts());

    
Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($collection);
    
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);

    return 
$collection;

}

This a snippet from my singlebycategory.phtml template that I utilize on the “Shop Now” block of my current project: http://www.netizenron.com/boomsite.php.

<?php 
$chosen_category 
Mage::getModel('catalog/category')->load($this->getCategoryId());

$_productCollection $this->getProductFeatured();
?>

<?php 
if(!$_productCollection):?>
<div class="note-msg">
    
<?=$this->__('There are no products matching the selection.')?>
</div>
<?php else:?>

<?php 
foreach($_productCollection->getItems() as $_product): ?>

<div class="random" style="width:120px; text-align:center; float:left; margin:10px 8px;">
    <
class="image" href="<?=$_product->getProductUrl()?>"><img src="<?=$this->helper('catalog/image')->init($_product, 'image')->resize(100, 150);?>" alt="<?=$this->htmlEscape($_product->getName())?>"  border="0"/></a>
    <
div style="font-size:.75em"><a href="<?=$chosen_category->getUrl()?>" title="<?=$this->__('More products from this category ...')?>" style="text-decoration:none; color:#333333;"><?=$this->__("Browse All<br>")?><b style="font-size:1.35em;"><?=$chosen_category->getName()?>  <span style="color:#CC0000; margin-bottom:3px;">&raquo;</span></b></a></div>
</
div>

<?php endforeach;?>

//unset catalog to allow using template for multiple categories on single page
<?php endif; Mage::unregister("_singleton/catalog/layer");?>
Block invocation in the CMS
{{block type="catalog/product_custom" category_id="8" num_products="1" orderby="rand()" template="catalog/product/singlesbycategory.phtml"}}

 Signature 

Ron Phillips

 
Magento Community Magento Community
Magento Community
Magento Community
 
HueCity
Jr. Member
 
Total Posts:  1
Joined:  2008-07-12
 

It didn’t worked for me.

 
Magento Community Magento Community
Magento Community
Magento Community
 
tilzinger
Jr. Member
 
Total Posts:  30
Joined:  2007-12-27
 

I successfully got this module working, however, I’m lacking a couple of things.

1. I have my featured product displayed on the same page as several other products on the category page, and the featured product is listed at the top of the page as a featured item (created from this module) then it’s also shown further down in the page with the rest of the product collection. How do I avoid repetition of the featured item?

2. I can only display the featured items name, SKU, description, and photo. I need to display all the options so a user can purchase the featured item without having to click into it’s detail page… how do I do this? Below is the code I have in featured.phtml

<script type="text/javascript">
    var 
optionsPrice = new Product.OptionsPrice(<?php echo $this->getJsonConfig() ?>);
</script>
<?php $_product
=$this->getFeaturedProduct() ?>
<div class="productDetail">
    <
form action="<?php echo $this->getAddToCartUrl($_product) ?>" method="post" id="product_addtocart_form">
        <
div class="productPhoto">
            <
img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(400, 390); ?>" width="400" height="390" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />
        </
div>
        <
div class="productInfo">
            <
h2><?php echo $this->htmlEscape($_product->getName()) ?></h2>
            <
class="small">Style# T608J01</p>
            
<?php if ($_product->getDescription()):?>
            
<p><?php echo nl2br($_product->getDescription()) ?></p>
            
<?php endif;?> 
            
            <?php 
if (!$this->hasOptions()):?>
                <?php 
echo $this->getChildHtml('tierprices'?>
                
<div class="add-to-holder">
                    
<?php if($_product->isSaleable()): ?>
                        <?php 
echo $this->getChildHtml('addtocart'?>
                        <?php 
if( $this->helper('wishlist')->isAllow() || $_compareUrl=$this->helper('catalog/product_compare')->getAddUrl($_product)): ?>
                            
<span class="add-or"><?php echo $this->__('OR'?></span>
                        
<?php endif; ?>
                    <?php 
endif; ?>
                    <?php 
echo $this->getChildHtml('addto'?>
                
</div>
            
<?php else:?>
                <?php 
echo $this->getChildHtml('addto'?>
            <?php 
endif; ?> 
            
            <?php 
if ($this->hasOptions()):?>
                <?php 
echo $this->getChildChildHtml('container1'''truetrue?>
                <?php 
echo $this->getChildChildHtml('container2'''truetrue?>
            <?php 
endif;?>  
            
            <?php 
//echo $this->getChildHtml('product_additional_data') ?>  
        
</div>
    
          <
input type="hidden" name="product" value="<?php echo $_product->getId() ?>" />
          <
input type="hidden" name="related_product" id="related-products-field" value="" />
    </
form>
    <
script type="text/javascript">
            var 
productAddToCartForm = new VarienForm('product_addtocart_form');
            
productAddToCartForm.submit = function(){
                    
if (this.validator.validate()) {
                            this
.form.submit();
                    
}
            }
.bind(productAddToCartForm);
    
</script>
</div>

 
Magento Community Magento Community
Magento Community
Magento Community
 
davidgrun
Member
 
Total Posts:  44
Joined:  2008-07-10
 

I got this error:

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’); } }
Fatal error: Class ‘MyCompany_Catalog_Block_Category_View’ not found in /nfs/c02/h04/mnt/45486/domains/kehot.com.ar/html/app/code/core/Mage/Core/Model/Layout.php on line 445

What should I do??

THANKS!!

 Signature 

I love Magento!  (...just, please, make it run a bit faster...)

 
Magento Community Magento Community
Magento Community
Magento Community
 
Naresh Vajawat
Jr. Member
 
Total Posts:  27
Joined:  2008-09-22
 

I removed the <category_view> block in local xml and it displayed the products correctly. but i do not see the featured products. i followed the steps as in in WIKI but doesn’t help. let me know if there are any other issues.

cibernoid - 14 July 2008 03:37 AM

brianpat - 09 July 2008 07:35 AM
I did as being said in the tutorial in wiki page. But it doesn’t show on any page. Is it working for any person? If so can anyone put this in the extension?

I’ve just tried this and it doesn’t wokr to me. When I add the block on file “local.xml” all category pages are blank (without products).

 
Magento Community Magento Community
Magento Community
Magento Community
 
gideonvalor
Jr. Member
 
Avatar
Total Posts:  2
Joined:  2008-10-15
Cumberland, RI
 

Trying to get this to show up in my sidebar.  Any suggestions?

 
Magento Community Magento Community
Magento Community
Magento Community
 
BungleFeet
Jr. Member
 
Total Posts:  11
Joined:  2008-08-20
 

I’ve had great success implementing a “Featured Products” block using the techniques described in Andy’s wiki article and Ron’s post earlier in this thread.  I did stumble a couple of times though, so I thought I’d put together a step-by-step summary.

My aim was to have show a “Featured Products” block on the front page of my site, with the ability to restrict the products shown to a particular category, or show products from all categories.  I wanted to have a random selection of my featured products displayed, and I wanted the structure and styling of the block to match the regular product grid display, minus the toolbars bottom and top.  I didn’t want the featured products to appear above every product_list block, which is what Andy’s wiki article does.

Here’s how it’s done:

1: Follow Step 1 in Andy’s wiki article.

2: Create the file app/code/local/MyCompany/Catalog/Block/Product/Featured.php.  The code in this file is based on Ron’s, with two small differences:  you need to add the argument ->addAttributeToSelect(’status’) to the getCollection() call, to avoid the out of stock bug.  Also, I changed if($category) to if(isset($category)), as the former was causing an error when showing global featured products (no category_id attribute set).  My amended version is below.

<?php
class MyCompany_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract
{
    
public function getProductFeatured(){
            
        $product 
Mage::getModel('catalog/product');
        if(
$this->getCategoryId())
            
$category Mage::getModel('catalog/category')->load($this->getCategoryId());
        
        
$collection $product->getCollection()
                ->
addAttributeToSelect('name')
                ->
addAttributeToSelect('price')
                ->
addAttributeToSelect('image')
                ->
addAttributeToFilter('featured',array('yes'=>true))
                ->
addAttributeToSelect('status');
    
        if(isset(
$category)) {
            $collection
->addCategoryFilter($category);
        
}
    
        $collection 
->addStoreFilter();        
            
        if(
$this->getOrderby())    
            
$collection->getSelect()->order($this->getOrderby());
        if(
$this->getNumProducts())
            
$collection->getSelect()->limit($this->getNumProducts());
    
        
Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($collection);
        
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
    
        return 
$collection;
    
    
}
}
?>

3: Write the code to mark up and display the result set.  I wanted my featured products block to look like the standard product_list in grid mode, so I just copied app/design/frontend/default/default/template/catalog/product/list.phtml into featured.phtml in the same directory.  I then replaced <?php $_productCollection=$this->getLoadedProductCollection() ?> with <?php $_productCollection = $this->getProductFeatured() ?>, and removed all the code pertaining to the toolbars and the list mode.

4: Add the product_featured block rewrite to local.xml, as per Andy’s wiki article, step 6.  However, do not add the category_view rewrite, as this will interfere with the normal category view.

5: Finally, insert the line {{block type="catalog/product_featured" category_id="8" num_products="1" orderby="rand()" template="catalog/product/featured.phtml"}} in the CMS page where you want the block to appear.  If you omit category_id, you will get featured products from all categories.

Hopefully this clarifies and brings together the excellent ideas from Andy and Ron.

Cheers,

Bungle