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 2
Second block taken the id set for the 1st block [SOLVED]
 
Christian_Norway
Sr. Member
 
Total Posts:  135
Joined:  2008-06-18
 

Hi guys,

I’m using several different ways to display products based on a given category on the frontpage of my store.

They are useing the same type="catalog/product_list", but different template .phtml files

This is some of the code om my front page:

<div class="home-spot">

<
div class="home_callout">
<
class="home-callout">
<
img src="{{skin url='images/promo_banner.jpg'}}" border="0" width="470" />
</
p>
</
div>

{{block type="catalog/product_list" category_id="9" template="catalog/product/featured.phtml"}}

<div class="home_callout">
<
class="home-callout">
<
img src="{{skin url='images/gratis-frakt.jpg'}}" border="0" width="470" />
</
p>
</
div>


{{block type="catalog/product_list" category_id="10" template="catalog/product/featured_list.phtml"}}

</div>

As you can see the two blocks have different category_id=” “, first have 9 and second 10.

my problem is that both blocks show the same category. So changing the category of the second block changes nothing, but changing the category of the first block changes both.

However the products are displayed differently, based on each individual template file.

This is my site: Showing the problem: http://www.amedisin.no/amedisin_nettbutikk/magento/

 
Magento Community Magento Community
Magento Community
Magento Community
 
Christian_Norway
Sr. Member
 
Total Posts:  135
Joined:  2008-06-18
 

Am I getting no answers, as people don’t know, or because you don’t understand my problem?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Back40Design
Jr. Member
 
Total Posts:  21
Joined:  2008-02-13
 

I’m working on the same issue on my company’s time. Will post when I have it figured out.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Back40Design
Jr. Member
 
Total Posts:  21
Joined:  2008-02-13
 

Actually it looks like you have it solved, care to share?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Christian_Norway
Sr. Member
 
Total Posts:  135
Joined:  2008-06-18
 

I don’t have it solved… that is my problem…

I believe what we need is to be able to change the active category for each block. But that the category_id="foo", isn’t working the second time around… So we need a way to unset() the first category and set a new active category for the second block..

 
Magento Community Magento Community
Magento Community
Magento Community
 
piotrn
Member
 
Total Posts:  33
Joined:  2008-04-27
 

The reason for the product lists to be the same, is that Mage_Catalog_Model_Layer object (which is being used as singleton) stores a all products in it’s internal property. When the property is not null, it’s being used again, even if category has changed since last time.

So to fix the problem, you will need to reset that variable. Easiest way to to that, will be to invoke:

Mage::getSingleton('catalog/layer')->setData("product_collection",NULL);
before you call
$this->getLoadedProductCollection()
inside your template.

Now, there the block model itself also uses an internal cache (lots of that) and the block is also a singleton (also lots of those),
so to make it all work, you need to add this line also:

$this->_productCollection null;

So, finally:

<?php
Mage
::getSingleton('catalog/layer')->setData("product_collection",NULL);
$this->_productCollection null;
$_productCollection=$this->getLoadedProductCollection();
?>
Put it in your list template. By default, it is in app/design/frontend/default/default/template/catalog/product/list.phtml.
This code should be placed before original code.

edited: There was a bug introduced here, the line $this->_productCollection = null; was accidentally replaced by $this->getLoadedProductCollection();

best regards
Piotr N

 
Magento Community Magento Community
Magento Community
Magento Community
 
eliteeternity
Member
 
Avatar
Total Posts:  74
Joined:  2008-07-16
 

So which file do I add this code to? And where is it located?
Sorry for the noob response, but I really appreciate your help.

 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 
eliteeternity - 25 July 2008 07:30 AM

So which file do I add this code to? And where is it located?
Sorry for the noob response, but I really appreciate your help.

app/code/core/Mage/Catalog/Block/Product/List.php

You can tell because of the block type and naming conventions used:
type="catalog/product_list" <-- relates to Catalog/[block]/Product/List.php

 
Magento Community Magento Community
Magento Community
Magento Community
 
eliteeternity
Member
 
Avatar
Total Posts:  74
Joined:  2008-07-16
 

thanks nikefido.
so I got into the List.php file and this is the code I see:

<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * @category   Mage
 * @package    Mage_Catalog
 * @copyright  Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */


/**
 * Product list
 *
 * @category   Mage
 * @package    Mage_Catalog
 * @author      Magento Core Team <core@magentocommerce.com>
 */
class Mage_Catalog_Block_Product_List extends Mage_Catalog_Block_Product_Abstract
{
    
protected $_defaultToolbarBlock 'catalog/product_list_toolbar';
    protected 
$_productCollection;

    
/**
     * Retrieve loaded category collection
     *
     * @return Mage_Eav_Model_Entity_Collection_Abstract
     */
    
protected function _getProductCollection()
    
{
        
if (is_null($this->_productCollection)) {
            $layer 
Mage::getSingleton('catalog/layer');
            if (
$this->getShowRootCategory()) {
                $this
->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
            
}

            
// if this is a product view page
            
if (Mage::registry('product')) {
                
// get collection of categories this product is associated with
                
$categories Mage::registry('product')->getCategoryCollection()
                    ->
setPage(11)
                    ->
load();
                
// if the product is associated with any category
                
if ($categories->count()) {
                    
// show products from this category
                    
$this->setCategoryId(current($categories->getIterator()));
                
}
            }

            
if ($this->getCategoryId()) {
                $category 
Mage::getModel('catalog/category')->load($this->getCategoryId());
                
$layer->setCurrentCategory($category);
            
}
            $this
->_productCollection $layer->getProductCollection();
        
}
        
return $this->_productCollection;
    
}

    
/**
     * Retrieve loaded category collection
     *
     * @return Mage_Eav_Model_Entity_Collection_Abstract
     */
    
public function getLoadedProductCollection()
    
{
        
return $this->_getProductCollection();
    
}

    
/**
     * Retrieve current view mode
     *
     * @return string
     */
    
public function getMode()
    
{
        
return $this->getChild('toolbar')->getCurrentMode();
    
}

    
/**
     * Need use as _prepareLayout - but problem in declaring collection from
     * another block (was problem with search result)
     */
    
protected function _beforeToHtml()
    
{
        
/*$toolbar = $this->getLayout()->createBlock('catalog/product_list_toolbar', microtime());
        if ($toolbarTemplate = $this->getToolbarTemplate()) {
            $toolbar->setTemplate($toolbarTemplate);
        }*/
        
$toolbar $this->getToolbarBlock();
        if (
$orders $this->getAvailableOrders()) {
            $toolbar
->setAvailableOrders($orders);
        
}
        
if ($modes $this->getModes()) {
            $toolbar
->setModes($modes);
        
}
        $toolbar
->setCollection($this->_getProductCollection());
        
$this->setChild('toolbar'$toolbar);
        
Mage::dispatchEvent('catalog_block_product_list_collection', array(
            
'collection'=>$this->_getProductCollection(),
        ));

        
$this->_getProductCollection()->load();
        
Mage::getModel('review/review')->appendSummary($this->_getProductCollection());
        return 
parent::_beforeToHtml();
    
}

    
public function getToolbarBlock()
    
{
        
if ($blockName $this->getToolbarBlockName()) {
            
if ($block $this->getLayout()->getBlock($blockName)) {
                
return $block;
            
}
        }
        $block 
$this->getLayout()->createBlock($this->_defaultToolbarBlockmicrotime());
        return 
$block;
    
}

    
/**
     * Retrieve list toolbar HTML
     *
     * @return string
     */
    
public function getToolbarHtml()
    
{
        
return $this->getChildHtml('toolbar');
    
}

    
public function setCollection($collection)
    
{
        $this
->_productCollection $collection;
        return 
$this;
    
}

    
public function addAttribute($code)
    
{
        $this
->_getProductCollection()->addAttributeToSelect($code);
        return 
$this;
    
}

    
public function getPriceBlockTemplate()
    
{
        
return $this->_getData('price_block_template');
    
}

}

I’m confused as to where to put the suggested code. I’ve played around with placing it places, but I always get an error when I go to my site, so I just reload the original file to make everything go back to normal.
Specific instruction for a slightly noob kinda guy?

 
Magento Community Magento Community
Magento Community
Magento Community
 
eliteeternity
Member
 
Avatar
Total Posts:  74
Joined:  2008-07-16
 

bump...please, this is urgent.

 
Magento Community Magento Community
Magento Community
Magento Community
 
eliteeternity
Member
 
Avatar
Total Posts:  74
Joined:  2008-07-16
 

for the love....bump.

 
Magento Community Magento Community
Magento Community
Magento Community
 
eliteeternity
Member
 
Avatar
Total Posts:  74
Joined:  2008-07-16
 

HELP!

 
Magento Community Magento Community
Magento Community
Magento Community
 
Alessandro Perrone
Jr. Member
 
Total Posts:  13
Joined:  2008-07-22
 

You have to insert that code in your template (for the default template app/design/frontend/default/defaul/template/catalog/product/list.phtml)

 
Magento Community Magento Community
Magento Community
Magento Community
 
piotrn
Member
 
Total Posts:  33
Joined:  2008-04-27
 

Yes, the code should go to template, at the top of it.
I filled a bug report and hopefully it will be fixed, so there is a chance future versions will not require this workaround.

best regards
PiotrN

 
Magento Community Magento Community
Magento Community
Magento Community
 
eliteeternity
Member
 
Avatar
Total Posts:  74
Joined:  2008-07-16
 

Thanks guys, you rule.
I was trying to put it in here: app/code/core/Mage/Catalog/Block/Product/List.php as opposed to here: app/design/frontend/default/defaul/template/catalog/product/list.phtml as that is what I was told by nikefido.

 
Magento Community Magento Community
Magento Community
Magento Community
 
eliteeternity
Member
 
Avatar
Total Posts:  74
Joined:  2008-07-16
 

Hey, since I did this, now when I search for a product on the site I get this error: Fatal error: Call to a member function getProductCollection() on a non-object in /home/skyjapan/domains/skyjapanshop.com/public_html/app/code/core/Mage/Catalog/Model/Layer.php on line 41
Do you think this is something related do what I added to the code? I don’t know why this would happen right after, but it sucks. Please respond asap.
Thanks.

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