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

Filter out Products
 
illo_sdrawkcab
Member
 
Total Posts:  63
Joined:  2008-10-23
 

Hello Magento Community,
ich want to filter out some products by id in my list view.

I tried to use this code:

$storeId    Mage::app()->getStore()->getId();
            
$product    Mage::getModel('catalog/product');
            
            
$products   $product->setStoreId($storeId)->getCollection();
            
            
                
$this->_productCollection $product->setStoreId($storeId)->getCollection()->addAttributeToFilter('entity_id', array('in'=>array(1,4))); 
                
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
                
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
                
$products->setPageSize($this->_productCollection->getSize())->setCurPage(1);
                
$this->setProductCollection($products);

but somehow it shows me only the skelett of two items ( what would be right if name,price, description… fields where filled)

Can somebody help me ? Whats wrong with my code ?

My intention is to filter products from selected categorys. this is all working, but the Collection Object is not updated so Pagebrowser shows more pages than needed.

Greets Olli

 
Magento Community Magento Community
Magento Community
Magento Community
 
illo_sdrawkcab
Member
 
Total Posts:  63
Joined:  2008-10-23
 

So i finally found out how to filter my products but as always follows the next problem.

If i filter the Ids hard-coded all works fine. But if i go through the actual search productCollection magento shows on each page all products.
for better understanding:

CatalogSearch/Block/Result.php

public function _getProductCollection()
    
{
        
if (is_null($this->_productCollection)) {
            $this
->_productCollection $this->_getQuery()->getResultCollection()->addAttributeToFilter('entity_id', array('in'=>array(6,7,5)))
                ->
addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes());

            
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($this->_productCollection);
            
Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($this->_productCollection);
            return 
$this->_productCollection;
        
}

        
return $this->_productCollection;
    
}
this is my filter with ids 6,7,5 to show. works.

public function getSelectedProducts($_categories,$_productCollections{        
        
if(!isset($cntProducts)) {
            $cntProducts 
0;
            
$_listetProducts ''
            
$_listetProducts1 '';
            
$productArray '';
        
}
        $this
->_cats $_categories;
        
        
$getRecursiveCategoriesArray array_unique(explode(',',substr($_categories,1)));
        foreach(
$getRecursiveCategoriesArray as $recCatId{
            
             $cur_category 
Mage::getModel('catalog/category')->load($recCatId);
             
$_productsC Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($cur_category);
             
$_productsC $_productsC->load(); 
             foreach ( 
$_productsC as $keyLi=>$product { $_listetProducts1 .= $keyLi.',';
                foreach (
$_productCollections as $keyOr=>$_product{  
                    
if($keyLi == $keyOr && strstr($_listetProducts,$keyOr.',')==false {
                        
                        $cntProducts
++;
                        
$productArray[$cntProducts] $keyOr;
                        
$_listetProducts .= $keyOr.',';
                        
$_selectedProducts[$keyOr] $_product;
                    
}
                }
             }
        }
        
return $productArray;
        
    
}
    
    
public function getRecursiveCategories($_category$level=2
       
{
        
if(!isset($recursiveCategories)) {
            $recursiveCategories 
=  '';
        
}
            $recursiveCategories 
.= ','.$_category->getId();
            foreach (
$_category->getChildren() as $keyCh=>$_categoryChild{
                $recursiveCategories 
.= ','.$keyCh;
                if(
$_categoryChild->hasChildren()) {                        
                    $recursiveCategories 
.= $this->getRecursiveCategories($_categoryChild$level+1);
                

            }
            
        
return $recursiveCategories;            
    
}
    
    
public function catchSelectedCategories() {
        
if(!isset($getRecursiveCategories)) {
            $getRecursiveCategories 
'';
        
}
        
foreach($_GET as $key=>$searchCrits):
            if(
$searchCrits=='on'):
                foreach( 
$storeCats $this->helper('catalog/category')->getStoreCategories() as $_category):
                    if(
$_category->getId() == $key):
                        
$getRecursiveCategories .= $this->getRecursiveCategories($_category);
                    endif;
                endforeach;
            endif;
        endforeach;
        
        return 
$getRecursiveCategories;
        
}
This is my selection for picking out all products dependent from search-term and selected category.
The problem is this line:
foreach ($_productCollections as $keyOr=>$_product{

$_productCollections contains the origin collection dependet from search-term. But as i call this he shows all articles on one page.

Seems that the page-limit get lost but the page browser shows the right number of pages.
Can someone help me? Is there another way to get ids from origin search?

 
Magento Community Magento Community
Magento Community
Magento Community
 
illo_sdrawkcab
Member
 
Total Posts:  63
Joined:  2008-10-23
 

Found a solution: just called getProductCollection () from the Mage_CatalogSearch_Model_Advanced Class with Mage::getModel.

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