Magento Forum

   
view one or more categories on a choosen page
 
__pete®
Jr. Member
 
Total Posts:  11
Joined:  2007-11-22
 

hi there,

is it possible to get the category view on a page i want? (e.g. homepage could show one) and if that works - is it possible to get more then one category shown on one page? (i don’t want to write a really new module doing all the things which category ->productlist rendering already does ... wink)

thx 4 help,
_pete®

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

In last release you will need to customize a bit.
In file app/code/core/Mage/Catalog/Block/Product/List.php replace method _getProductCollection() with:

protected function _getProductCollection()
    
{
        
if (is_null($this->_productCollection)) {
            $collection 
Mage::getSingleton('catalog/layer');
            if (
$this->getShowRootCategory()) {
                $this
->setCategoryId(Mage::getStoreConfig('catalog/category/root_id'));
            
}
            
if ($this->getCategoryId()) {
                $category 
Mage::getModel('catalog/category')->load($this->getCategoryId());
                
$collection->setCurrentCategory($category);
            
}
            $this
->_productCollection $collection->getProductCollection();
        
}
        
return $this->_productCollection;
    
}

Now you can put in your static page or static block this tag:

{{block type="catalog/product_list" category_id="12"}}

It will show product listing with pager for the category ID you have specified. To show products in root category use:

{{block type="catalog/product_list" show_root_category="true"}}

 
Magento Community Magento Community
Magento Community
Magento Community
 
__pete®
Jr. Member
 
Total Posts:  11
Joined:  2007-11-22
 

this works for one smile

i wanted 2 categories on a page this throws exception that the ‘product_list.toolbar’ already exists ... so i tried to check if i could workarround:

protected function _beforeToHtml()
    
{
        
static $counter 0;
        
$toolbar $this->getLayout()->createBlock('catalog/product_list_toolbar''product_list.toolbar'.(++$counter));

but if i do this only the first category i put in will be rendered:
i don’t use static page for this since i try some other things wink but i think static blocks also won’t work here.

public function indexAction()
    
{
        $this
->loadLayout();
        
        
$homeBlock $this->getLayout()->createBlock('catalog/product_list''homecontent1',
            array(
'category_id'=>'5'));//->setTemplate('homepage/content.phtml');
        
$this->getLayout()->getBlock('content')->append($homeBlock);
        
$homeBlock $this->getLayout()->createBlock('catalog/product_list''homecontent2',
            array(
'category_id'=>'13'));//->setTemplate('homepage/content.phtml');
        
        
$this->getLayout()->getBlock('content')->append($homeBlock);
        
        
        
$this->renderLayout();
    
}

shows 2 times the same category :/

thx for your reply,
_pete®

 
Magento Community Magento Community
Magento Community
Magento Community
 
__pete®
Jr. Member
 
Total Posts:  11
Joined:  2007-11-22
 

changing from singelton to model works for me ... ( took me hours to realize this simple thing ^^)

/**
     * Retrieve loaded category collection
     *
     * @return Mage_Eav_Model_Entity_Collection_Abstract
     */
    protected function _getProductCollection()
    
{
        
/*if (is_null($this->_productCollection)) {
            $collection = Mage::getSingleton('catalog/layer');
            if ($this->getShowRootCategory()) {
                $categoryId = Mage::getStoreConfig('catalog/category/root_id');
                $category = Mage::getModel('catalog/category')->load($categoryId);
                $collection->setCurrentCategory($category);
            }
            $this->_productCollection = $collection->getProductCollection();
        }
        return $this->_productCollection;*/

        
if (is_null($this->_productCollection)) {
//            $collection = Mage::getSingleton('catalog/layer');
            
$collection Mage::getModel('catalog/layer');
            if (
$this->getShowRootCategory()) {
                $this
->setCategoryId(Mage::getStoreConfig('catalog/category/root_id'));
            
}
            
if ($this->getCategoryId()) {
                    $category 
Mage::getModel('catalog/category')->load($this->getCategoryId());
                    
$collection->setCurrentCategory($category);
            
}
            $this
->_productCollection $collection->getProductCollection();
        
}
        
        
        
        
return $this->_productCollection;
    
}

wink
_pete®

 
Magento Community Magento Community
Magento Community
Magento Community
 
page.down
Jr. Member
 
Total Posts:  3
Joined:  2008-06-02
 
__pete® - 22 November 2007 11:46 PM

changing from singelton to model works for me ... ( took me hours to realize this simple thing ^^)
_pete®

it works! Thank You! smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
quanza
Sr. Member
 
Avatar
Total Posts:  97
Joined:  2008-12-02
Tokyo, Japan
 

THANK YOU, THANK YOU, THANK YOU!!!  I’ve been trying to fix something completely different for the past 8 hours, and your single comment about switching from Singleton to Model *finally* fixed it!!!  I can’t believe I overlooked this one thing ... argh!!  But thank you for sharing!

 
Magento Community Magento Community
Magento Community
Magento Community
 
depo
Jr. Member
 
Total Posts:  4
Joined:  2008-02-10
 

Hi,
this post is old, but maybe helps someone.
When you switching from Singleton to Model you can’t use catalog navigation (shoping by...)

You must edit you cache setting on list
For example show more category on frontpage:

On \app\code\local\Mage\Catalog\Block\Product\List.php

protected function _construct()
{
if($this->getRequest()->getRequestUri() != “/"){ // cache all except homepage
$this->addData(array(
‘cache_lifetime’ => 900,
‘cache_tags’ => array(Mage_Catalog_Model_Product::CACHE_TAG),
‘cache_key’ => $this->getCacheKey()
));
}
}

public function getCacheTags()
{
return array(Mage_Catalog_Model_Product::CACHE_TAG);
}

public function getCacheKey()
{
$category = Mage::getModel(’catalog/category’)->load($this->getCategoryId());
return $this->getRequest()->getRequestUri();
}

D.

 
Magento Community Magento Community
Magento Community
Magento Community
 
DamonT
Sr. Member
 
Total Posts:  153
Joined:  2009-10-02
 

Hi in your technique below, it doesn’t work for me unless i add the template
here is the code with the template:

{{block type="catalog/product_list" category_id="506" template="catalog/category/view.phtml" }} (id 506 is a category with 5 sub categories.)

but then I get a call to member function getImageUrl() on a non object in <path to above template file>, if I change the template path to point to catalog/product/list.phtml” it then works fine, but it dosnt show categories any more but only products.

I am using Magento ver. 1.3.2.4

any help is appreciated.

Thanks.

Moshe - 22 November 2007 04:29 AM

In last release you will need to customize a bit.
In file app/code/core/Mage/Catalog/Block/Product/List.php replace method _getProductCollection() with:

protected function _getProductCollection()
    
{
        
if (is_null($this->_productCollection)) {
            $collection 
Mage::getSingleton('catalog/layer');
            if (
$this->getShowRootCategory()) {
                $this
->setCategoryId(Mage::getStoreConfig('catalog/category/root_id'));
            
}
            
if ($this->getCategoryId()) {
                $category 
Mage::getModel('catalog/category')->load($this->getCategoryId());
                
$collection->setCurrentCategory($category);
            
}
            $this
->_productCollection $collection->getProductCollection();
        
}
        
return $this->_productCollection;
    
}

Now you can put in your static page or static block this tag:
{{block type="catalog/product_list" category_id="12"}}

It will show product listing with pager for the category ID you have specified. To show products in root category use:
{{block type="catalog/product_list" show_root_category="true"}}

 
Magento Community Magento Community
Magento Community
Magento Community
 
DamonT
Sr. Member
 
Total Posts:  153
Joined:  2009-10-02
 

Hi I still cant get it to work! can anyone help me please?

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