Magento Forum

   
How to use JoinField to add a filter to the Product Collection? 
 
Bobby
Jr. Member
 
Avatar
Total Posts:  7
Joined:  2007-11-13
 

Hi,

I would like to add a filter to the products collection created in app/code/core/Mage/Catalog/Model/Layer.php. Indeed, I want to hide the products and configurable products whose quantity is 0.

Thus, I changed the getProductCollection function :

public function getProductCollection()
    
{
        $collection 
$this->getData('product_collection');
        if (
is_null($collection)) {
            $collection 
$this->getCurrentCategory()->getProductCollection()
                ->
addCategoryFilter($this->getCurrentCategory())
                ->
addStockFilter(); //products with no stock won't appear
            
            
$this->prepareProductCollection($collection);
            
$this->setData('product_collection'$collection);
        
}

        
return $collection;
    
}

This is my addStockFilter function (in app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Product/Collection.php) :

public function addStockFilter()
    
{
        $this
->joinField('inventory_in_stock''cataloginventory/stock_item',
                                
'is_in_stock''product_id=entity_id''{{table}}.is_in_stock=1');
        
        
$this->joinField('''catalog_product_super_link''product_id''parent_id=entity_id')
        ->
joinField('product_super_link''cataloginventory/stock_item''qty''product_id={{table}}.product_id''qty > 0');
        
        return 
$this;   
    
}

The first joinField checks the is_in_stock field of the products, and displays only those which have 1. It is working with simple products.

But with configurable products, if all the associated simple products are not in stock, the is_in_stock field of the configurable product remains at 1, so it is displayed in the list…

So I tried to write a second joinField which gets back all the entity_id of the simple products associated to the configurable product (in catalog_product_super_link table, and then I checks if they have a quantity > 0 (in cataloginventory_stock_item table). But it is not working, I have some problems with the use of the joinField function…

Has anybody an idea?

Thanks.

 
Magento Community Magento Community
Magento Community
Magento Community
 
kohril
Member
 
Total Posts:  44
Joined:  2007-12-23
Lille, northernFrance
 

Take care about changing core code !

Maybe you should rewrite the class and then overload the methods you need to customize in your own module

 
Magento Community Magento Community
Magento Community
Magento Community
 
Bobby
Jr. Member
 
Avatar
Total Posts:  7
Joined:  2007-11-13
 

Ok, Kohril, I’ll do it…

I finally managed to find the answer. That is the function which worked (if anybody is interested) :

public function addStockFilter()
    
{
        $this
->joinField('inventory_in_stock''cataloginventory/stock_item',
                                
'is_in_stock''product_id=entity_id''{{table}}.is_in_stock=1');
        
        
$this->joinField('children_ids''catalog/product_super_link''product_id''parent_id=entity_id');
        
$this->joinField('stock_children''cataloginventory/stock_item''qty''product_id=children_ids''{{table}}.qty > 0');
        
$this->groupByAttribute('entity_id');
        
        return 
$this;   
    
}

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