Magento Forum

   
Adding a Featured product
 
emagine
Member
 
Total Posts:  36
Joined:  2007-12-10
 

I found a thread and saw where I can add a category called ‘featured’ in the root category and drop items into this. I have put this little bit of code in my home page in the cms block and it works - lovely.

<h2 class="category-head">Featured Product</h2>
<
div id="featured_product">
{{block type="catalog/product_list" category_id="22"}} 
</div>

The thing I dont understand how to do is how would I limit this to one product, and not a list or grid view of the thing. I would love to drop one product into the category and have it put out a new template altogether. One where I could slap it into some nice cms and show that product image, name, description and price and a link to the page, without all the paging and add to cart etc.

Anyone know how to go about this, as I am befuddled. I tried a few things, like changing the code to:

{{block type="catalog/product_view" category_id="22"}}

I guess I would love to be able to say:

{{block type="catalog/product_view" category_id="22" limit="1" template="featured.phtml"}}

Anyone out there have some hints?

 
Magento Community Magento Community
Magento Community
Magento Community
 
mr_dimsum
Member
 
Avatar
Total Posts:  35
Joined:  2008-01-28
 

Do you have a working demonstration online I can take a look at? =) Would love to see how this works!

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

@emagine: You will need to make 3 changes:

Mage_Core_Model_Email_Template_Filter:

// REPLACE THIS: (APPROX LINE 42)
            $block $layout->createBlock($type);
            if (!
$block{
                
return '';
            
}
            
if (!empty($blockParameters['template'])) {
                $block
->setTemplate($blockParameters['template']);
            
}

            $block
->addData($blockParameters);

// WITH THIS:
            
$block $layout->createBlock($typenull$blockParameters);
            if (!
$block{
                
return '';
            
}

Mage_Page_Block_Html_Pager:

// REPLACE THIS: (APPROX. LINE 71)
if ((int) $this->getLimit()) {

// WITH THIS:
if ((int) $this->getLimit() && !$this->_collection->getPageSize()) {

Mage_Catalog_Block_Product_List:

// FIND THIS: (APPROX. LINE 71)
            $this->_productCollection $collection->getProductCollection();

// ADD THIS AFTER LINE ABOVE:
            
if ($this->hasLimit()) {
                $this
->_productCollection->setPageSize($this->getLimit());
            
}

Now you can try:

{{block type="catalog/product_list" category_id="22" limit="1" template="featured.phtml"}}

These changes will be available in next release.

 
Magento Community Magento Community
Magento Community
Magento Community
 
seoguy
Member
 
Total Posts:  46
Joined:  2008-01-25
 

If you don’t mind the category template, you could just create another category entitled feature products, for example, set “is enabled” to no then just use
{{block type="catalog/product_list" category_id="PLACE THE CORESPONDING CATEGORY NUMBER HERE"}} code.  (assuming your home page is a cms page)

Chris

 
Magento Community Magento Community
Magento Community
Magento Community
 
AnnaM
Guru
 
Avatar
Total Posts:  325
Joined:  2008-01-29
San Francisco
 
seoguy - 05 February 2008 12:08 PM

If you don’t mind the category template, you could just create another category entitled feature products, for example, set “is enabled” to no then just use
{{block type="catalog/product_list" category_id="PLACE THE CORESPONDING CATEGORY NUMBER HERE"}} code.  (assuming your home page is a cms page)

Chris

Yes I did this earlier today with Chirs’s help. Seems to me the simplest thing is to just have a separate category with the one proudct in it, and you can change that product at will and it will then be the one showing on the page as “Featured” without changing any other code.  If I understand things correctly. that seems the simplest way to me.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Pablo Lopez
Member
 
Avatar
Total Posts:  51
Joined:  2008-01-17
 

the code emagine really helped me out!
But using the limit on the product listing will only filter my results, like a limit on an sql query right?
How can I do to display a limited number of products from a category but choosing those results randomly ?

say for example...having a category with 20 products but showing only 5 randomly selected from that category

thanks!

Also...how can I use the same template choosing / listing limiting logic to a template page?  I know I can use something like :

$this->getLayout()->createBlock('catalog/product_view')->setCategoryId('someid')->toHtml()

but don’t know how to indicate a template nor a limit

thanks again rasberry

 
Magento Community Magento Community
Magento Community
Magento Community
 
Pablo Lopez
Member
 
Avatar
Total Posts:  51
Joined:  2008-01-17
 

Ok, I figured out that I can make two attributes; product_featured_from, and product_featured_to .
Both are date attributes. So far so good.

My idea is instead of asking for a category id (on our case, the category named “featured products") , ask for the values of those attributes!
Will be something like comparing the value of today’s date with the values of those atributes. There I’ll have a collection of products that full fill that condition.

I have NO idea how to accomplish that though. I’m checking Mage_Catalog_Model_Product class and Mage_Catalog_Model_Product_Attribute class , but don’t know how to retrieve a product collection based on an attribute.

I don’t know if I made myself clear...hope someone can help me out!

 
Magento Community Magento Community
Magento Community
Magento Community
 
nekosan
Member
 
Avatar
Total Posts:  32
Joined:  2008-07-15
 

hy

the code above

{{block type="catalog/product_view" category_id="22" limit="1" template="featured.phtml"}}
is all fine and dandy, when you set it in the “Content” part of a cms page.

you can get the parameters using

$this->getCategoryId();
$this->getLimit();

when I add the same code in the “Layout update” part, I can’t get those parameters though. Any idea why?

<reference name="reference_name_goes_here">
            <
block type="catalog/product_featured" category_id="73" name="book_featured" template="catalog/product/featured.phtml" />
        </
reference>

If I call $this->getCategoryId() I don’t get anything. If I add the same code in the content part

{{block type="catalog/product_featured" category_id="73" name="book_featured" template="catalog/product/featured.phtml"}}
I can get the category id using $this->getCategoryId()

Any idea why this happens?

 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 
Pablo Lopez - 22 February 2008 04:10 AM

the code emagine really helped me out!
But using the limit on the product listing will only filter my results, like a limit on an sql query right?
How can I do to display a limited number of products from a category but choosing those results randomly ?

say for example...having a category with 20 products but showing only 5 randomly selected from that category

thanks!

Also...how can I use the same template choosing / listing limiting logic to a template page?  I know I can use something like :

$this->getLayout()->createBlock('catalog/product_view')->setCategoryId('someid')->toHtml()

but don’t know how to indicate a template nor a limit

thanks again rasberry

Not sure if you figured this out yet, but I believe adding a template is as simple as

$this->getLayout()->createBlock('catalog/product_view')->setCategoryId('someid')->setTemplate('catalog/list.phtml');

 
Magento Community Magento Community
Magento Community
Magento Community
 
Pointlessjon
Jr. Member
 
Total Posts:  21
Joined:  2008-07-28
Santa Cruz, California
 

am i missing something totally obvious? as, I’m not getting any errors but the output is blank (for the {{ block etc. }} portion). I’m sure I have products, active, in the category, and other just straight html is appearing in the block.

 
Magento Community Magento Community
Magento Community
Magento Community
 
AngelEyes
Member
 
Total Posts:  74
Joined:  2008-02-25
 

Moshe,

The code in some of those files seems to have changed (1.1.4), any chance you can help.

I have tried a few alternatives to try and create featured products but can’t seem to get them to work. I am wondering if the latest code is not compatible?

Thanks,

Adam

Moshe - 05 February 2008 09:55 AM

@emagine: You will need to make 3 changes:

Mage_Core_Model_Email_Template_Filter:

// REPLACE THIS: (APPROX LINE 42)
            $block $layout->createBlock($type);
            if (!
$block{
                
return '';
            
}
            
if (!empty($blockParameters['template'])) {
                $block
->setTemplate($blockParameters['template']);
            
}

            $block
->addData($blockParameters);

// WITH THIS:
            
$block $layout->createBlock($typenull$blockParameters);
            if (!
$block{
                
return '';
            
}

Mage_Page_Block_Html_Pager:
// REPLACE THIS: (APPROX. LINE 71)
if ((int) $this->getLimit()) {

// WITH THIS:
if ((int) $this->getLimit() && !$this->_collection->getPageSize()) {

Mage_Catalog_Block_Product_List:
// FIND THIS: (APPROX. LINE 71)
            $this->_productCollection $collection->getProductCollection();

// ADD THIS AFTER LINE ABOVE:
            
if ($this->hasLimit()) {
                $this
->_productCollection->setPageSize($this->getLimit());
            
}

Now you can try:
{{block type="catalog/product_list" category_id="22" limit="1" template="featured.phtml"}}

These changes will be available in next release.

 
Magento Community Magento Community
Magento Community
Magento Community
 
jrutter
Jr. Member
 
Total Posts:  24
Joined:  2008-04-08
 

I cannot get this working. I added a category called featured, then referenced it in my cms home page. And nothing is showing up.

What am I doing wrong?

 
Magento Community Magento Community
Magento Community
Magento Community
 
magewidgets
Jr. Member
 
Avatar
Total Posts:  18
Joined:  2010-03-24
USA
 

If you are on 1.4.x CE or higher or 1.7EE or higher you will need to use widgets for adding featured products or a full blown extension vs being able to embed blocks as previously mentioned for 1.3.x and back. You can find a featured product widget here http://www.magewidgets.com/featured-products-widget.html

 
Magento Community Magento Community
Magento Community
Magento Community
 
mkk_kashif
Jr. Member
 
Total Posts:  24
Joined:  2010-12-22
 

Call following code directly from any .phtml file.

<?php
$category_id 
34;
echo 
$this->getLayout()->createBlock('catalog/product_list')->setCategoryId($category_id)->setTemplate('catalog/product/list.phtml')->toHtml();
?>

 
Magento Community Magento Community
Magento Community
Magento Community
 
carlosrob4
Member
 
Total Posts:  67
Joined:  2012-10-28
 

You can avoid coding and add featured product using this Featured Product Layout Extension

View Demo

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