Magento Forum

   
getLoadedProductCollection / _getProductCollection
 
Charles
Member
 
Avatar
Total Posts:  73
Joined:  2008-03-27
Belgium
 

Hi,

just wanted to know how to restrict some of the products that are loaded with getLoadedProductCollection (or _getProductCollection) from the category/list.php, the purpose being that I’d want only to return all the products that have a specific attributesetid

// 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()
                    ->
addAttributeToFilter('attributesetid',27)
                    ->
setPage(11)
                    ->
load();
                
// if the product is associated with any category
                
if ($categories->count()) {
                    
// show products from this category
                    
$this->setCategoryId(current($categories->getIterator()));
                
}
            }

As you can see, I’ve already tried with ->addAttributeToFilter, alas no luck.

Thanks,

Charles

 
Magento Community Magento Community
Magento Community
Magento Community
 
Charles
Member
 
Avatar
Total Posts:  73
Joined:  2008-03-27
Belgium
 

got is solved.
Needed to make the modification in catalog/model/category.php

 
Magento Community Magento Community
Magento Community
Magento Community
 
Charles
Member
 
Avatar
Total Posts:  73
Joined:  2008-03-27
Belgium
 

Not completely out of the woods yet.

For instance, let’s assume that the products I want to select are those with attributesetid 27, the code looks like

class LVP_Catalog_Model_Category extends Mage_Catalog_Model_Category
{
    
    
public function getProductCollection()
    
{
        $collection 
Mage::getResourceModel('catalog/product_collection')
            ->
addCategoryFilter($this)
            ->
addAttributeToFilter('attribute_set_id'27);
        return 
$collection;
    
}

}
(as you can see, I just extended the Mage_Catalof_Model_Category instead of hacking in the code)

Now, in catalog/product/list.phtml you get

<?php $_productCollection=$this->getLoadedProductCollection() ?>

Which relates to the code in category/list.php

protected function _getProductCollection()
    
{
        
if (is_null($this->_productCollection)) {
            $layer 
Mage::getSingleton('catalog/layer');
            
/* @var $layer Mage_Catalog_Model_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()));
                
}
            }

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

            if (
$sortField $this->getSortBy()) {
                $sortOrder 
'asc';
                if (
strtolower($this->getSortOrder()) == 'desc'{
                    $sortOrder 
'desc';
                
}
                $this
->_productCollection->addAttributeToSort($sortField$sortOrder);
            
}

            
if ($origCategory{
                $layer
->setCurrentCategory($origCategory);
            
}
        }
        
return $this->_productCollection;
    
}

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

So, I guess, if I want to make the code more generic, I need to tweak the line in

$this->_productCollection $layer->getProductCollection();

which I did, into

$this->_productCollection $layer->getProductTypeCollection('attribute_set_id'27);

and changed the code in the extended category.

However, there I got a problem as it stucks. No product selected and certainly some error as the html blocks at the exact
place of where I called the getChildHtml(’product_list’).

Why?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Charles
Member
 
Avatar
Total Posts:  73
Joined:  2008-03-27
Belgium
 

up

up

 
Magento Community Magento Community
Magento Community
Magento Community
 
Charles
Member
 
Avatar
Total Posts:  73
Joined:  2008-03-27
Belgium
 

Well, I’ve been able to solve it eventually.

In catalog/block/product/list.php, locate the _getProductLocation()

change the line

$this->_productCollection $layer->getProductCollection();

into

$this->_productCollection $layer->getProductCollection()
->->
addAttributeToFilter('your_attribute''your_value')

I’ve even been able to elaborate on that and select the products on some criteria.

For instance, in my code, you’ll find:

if(stristr($this->helper('core/url')->getCurrentUrl(), "region")){
                $this
->_productCollection $layer->getProductCollection()
                    ->
addAttributeToFilter('attribute_set_id'27);
            
else {
                $this
->_productCollection $layer->getProductCollection()
                    ->
addAttributeToFilter('attribute_set_id'26);
            
}

 
Magento Community Magento Community
Magento Community
Magento Community
 
Quiddity
Jr. Member
 
Total Posts:  22
Joined:  2008-06-13
 

Charles,

I wanted to see if you can help. Currently in my product page (template/catalog/product/list.phtml) i’m not displaying “out of stock” products and this seems to be working fine. But the toolbar total still counts those those out of stock items, which in terms displays multiple blank pages if i have several product out.

How can i modify this number to only count those that are in stock. this way it will display properly.

Thx in advance

 
Magento Community Magento Community
Magento Community
Magento Community
 
Charles
Member
 
Avatar
Total Posts:  73
Joined:  2008-03-27
Belgium
 

Quiddity,
I’m trying to solve that one for myself as I need a toolbar displaying two numbers (to reflect the count on 2 product types).
I’ll get back to you when I have a solution. I must say, in my project it’s rather a NTH instead of Must have.
Anyway, if you want to give it a go, you’d better look into the toolbar.phtml and associated toolbar.php file
Gr,
Charles

 
Magento Community Magento Community
Magento Community
Magento Community
 
webfish
Jr. Member
 
Avatar
Total Posts:  4
Joined:  2009-03-01
 

Just wanted to share my knowledge

This is to filter the products based on the attribute_set_id

change the function getProductCollection( ) like this which is located in code\core\Mage\Catalog\Model\Category.php

public function getProductCollection()
    
{

        $attribute_set_id 
NULL;

        
//check for the product page
        
if(Mage::registry('product'))
            
$attribute_set_id=Mage::registry('product')->getAttributeSetId();

        if(
$attribute_set_id == NULL){
          $collection 
Mage::getResourceModel('catalog/product_collection')
                                ->
addCategoryFilter($this);
        
else {
          $collection 
Mage::getResourceModel('catalog/product_collection')

                              ->
addAttributeToFilter('attribute_set_id',$attribute_set_id);


        
}
        
return $collection;
    
}

 
Magento Community Magento Community
Magento Community
Magento Community
 
amartinez
Guru
 
Avatar
Total Posts:  347
Joined:  2008-11-14
Spain
 

Hi,

We have some problems with the update of the products (you can see our discussion here: http://www.magentocommerce.com/boards/viewthread/35865/P0/)

Our script works fine with the products creation, but when try to modify it, receive a warning (perhaps not relationed with the problem) and the product don’t update his data:

WarningInvalid argument supplied for foreach()  in /var/www/magentok/app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 937

Can you help us?

Regards!

 
Magento Community Magento Community
Magento Community
Magento Community
 
holylander
Jr. Member
 
Total Posts:  13
Joined:  2012-07-11
 

I tell you what I did:

$_productCollection=$this->getLoadedProductCollection();

//get Current category
$cat_id Mage::getModel('catalog/layer')->getCurrentCategory()->getId();

//get the filtered collection of products based on special price active for today and current category
$_productCollection Mage::getResourceModel('catalog/product_collection')
    ->
addAttributeToSelect(array('name''price''small_image''status','special_price'), 'inner')
    ->
addCategoryFilter(Mage::getModel('catalog/category')->load($cat_id));

$_helper $this->helper('catalog/output');

this way I managed to list the products I was interested.

Hope that it helps!

 
Magento Community Magento Community
Magento Community
Magento Community
 
emcl21
Jr. Member
 
Total Posts:  1
Joined:  2013-01-23
 

class LVP_Catalog_Model_Category extends Mage_Catalog_Model_Category
{

public function getProductCollection()
{
$collection = Mage::getResourceModel(’catalog/product_collection’)
->addCategoryFilter($this)
->addAttributeToFilter(’attribute_set_id’, 27);
return $collection;
}

}

this code list of selected products create it?

Thanks

Toner Compatible

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