Try the Demo

Magento

eCommerce Software for Online Growth

Magento Forum

Our new hosted solution for small & emerging businesses
   
Page 2 of 2
Can Magento hide products (or atributes for configurable products) when they are ‘out of stock’? 
 
colinmollenhour
Member
 
Avatar
Total Posts:  49
Joined:  2009-05-01
 

Actually, I have since fixed the code to work with configurable products.. For some reason Magento updates the stock_status when a configurable product goes out of stock, but not is_in_stock.. Here is the updated version:

class Lucky_InStockOnly_Model_Observer {
  
public function addInStockOnlyFilter($observer){
    $observer
->getEvent()->getCollection()
      ->
joinField('stock_status','cataloginventory/stock_status','stock_status',
        
'product_id=entity_id', array('stock_status' => Mage_CatalogInventory_Model_Stock_Status::STATUS_IN_STOCK))
    ;
  
}
}

The blog post has been updated as well.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Nadya
Jr. Member
 
Total Posts:  11
Joined:  2009-03-10
Kiev, Ukraine
 

Thank you Colin for your code. It really helped me.
But It does not decide problem with Layered Navigation.
there are count of products with ‘in stock’ and ‘out of stock’.

I decided it by adding condition in SQL query in file app/code/core/Mage/CatalogIndex/Model/Mysql4/Attribute.php (str 71)

public function getCount($attribute$entitySelect)
    
{
...
        
$select->from(''$fields)
            ->
join(array('index'=>$this->getMainTable()), 'index.entity_id=e.entity_id', array())
            ->
where('index.store_id = ?'$this->getStoreId())
            ->
where('index.attribute_id = ?'$attribute->getId())
            ->
group('index.value');
            
        
// this join was added for count in layered navigation products only 'in stock' status
        
$select->join(array('stock'=>'cataloginventory_stock_status'), 'stock.product_id=e.entity_id AND stock_status=1', array());

        
$select $select->__toString();
...

and the same one for price filtering app/code/core/Mage/CatalogIndex/Model/Mysql4/Price.php (str. 130)

public function getCount($range$attribute$entitySelect)
    
{
...
$select->from(''$fields)
            ->
group('range')
            ->
where('price_table.website_id = ?'$this->getWebsiteId())
            ->
where('price_table.attribute_id = ?'$attribute->getId());

            
// this join was added for count in layered navigation products only 'in stock' status
            
$select->join(array('stock'=>'cataloginventory_stock_status'), 'stock.product_id=e.entity_id AND stock_status=1', array());
        
        
$result $this->_getReadAdapter()->fetchAll($select);
...

of course better to do this changes in additional module (in app/code/local/...) not in Mage (app/code/core/Mage)

if somebody knows better way, please share

 
Magento Community Magento Community
Magento Community
Magento Community
 
colinmollenhour
Member
 
Avatar
Total Posts:  49
Joined:  2009-05-01
 

Nadya,

I had noticed the product count problem myself but now that you mentioned it I decided to revisit the problem in light of your work. Rather than handle the product list and the layerd navigation counts separately I found that they can both be handled in one fell swoop by doing what I did before only moving it to somewhere earlier in the process. I moved it into the Mage_Catalog_Model_Layer->prepareProductCollection function and it does the job beautifully. This is the proper place to do it as this is where the other filters are handled as well.

I posted the new code on my blog here: http://colin.mollenhour.com/2009/07/hiding-out-of-stock-items-in-layered-navigation/

Also, my previous code (and yours) did not work properly in all cases when there were multiple websites, this fixes that issue as well.

Thanks,
Colin

 
Magento Community Magento Community
Magento Community
Magento Community
 
Nadya
Jr. Member
 
Total Posts:  11
Joined:  2009-03-10
Kiev, Ukraine
 

Colin,

you are genius.

Thank you very much.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Max Andresen
Jr. Member
 
Total Posts:  1
Joined:  2009-11-10
 

Hello. I am very new to magento. I would like to hide the quantity on my subscription products. Is this possible? Also i was looking into the Subscriptions and Recurring Payments extension and i am wondering if i can change the price depending on what periodicity i choose.

Thank You

 
Magento Community Magento Community
Magento Community
Magento Community
 
gnum
Member
 
Total Posts:  38
Joined:  2009-02-16
 

colinmollenhour, thank you for solution.

But i have a problem that i currently trying to solve, maybe someone can help me?

I have special product attribute (for example ‘hide_out_of_stock’) and trying to implement logic ‘stock_status = 1 or hide_out_of_stock = 0 ‘. How i can add this additional ‘OR’ into code?

 Signature 

Custom software development

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
Page 2 of 2
 
© Copyright 2012 Magento Inc.
Privacy Policy|Terms of Service
Magento Community Count
701238 users|898 users currently online|497296 forum posts