Colina Mollenhour has done several modules to hide unavailable products on Magento pages. His modules do that on common pages or blocks, like category page, featured products, bestsellers, crossselling block, related products. The only need is to connect his modules by changing pieces of XML entities in config.xml files.
That's the reason, why I have developed an additional Magento module for that. First of all, we need to do a model to override an original Advanced Catalog Search module:
app/code/local/Endryu/AdvancedInStockOnly/CatalogSearch/Model/Advanced.php
After creating such a file, we put there lines of code given below. getProductCollection() method does override original method from core module and has an additional rule inside (->joinField(...)), which - in fact - filters out these products in collection that has out of stock status. Moreover, we add filter to the active Website ($websiteId):
<?php
class Endryu_AdvancedInStockOnly_CatalogSearch_Model_Advanced extends Mage_CatalogSearch_Model_Advanced {
/**
* filters product collection via in stock only filter
*/
public function getProductCollection(){
if (is_null($this->_productCollection)) {
$storeId = Mage::app()->getStore()->getId();
$websiteId = Mage::getModel('core/store')->load($storeId)->getWebsiteId();
$this->_productCollection = Mage::getResourceModel('catalogsearch/advanced_collection')
->addAttributeToSelect(Mage::getSingleton('catalog/config')
->getProductAttributes())
->addMinimalPrice()
->addTaxPercents()
->addStoreFilter()
->joinField('stock_status', 'cataloginventory/stock_status', 'stock_status', 'product_id=entity_id', array('stock_status' => Mage_CatalogInventory_Model_Stock_Status::STATUS_IN_STOCK, 'website_id' => $websiteId));
Mage::getSingleton('catalog/product_status')
->addVisibleFilterToCollection($this->_productCollection);
Mage::getSingleton('catalog/product_visibility')
->addVisibleInSearchFilterToCollection($this->_productCollection);
}
return $this->_productCollection;
}
}
Next, we need a configuration XML file for fresh module. So we do:
app/code/local/Endryu/AdvancedInStockOnly/etc/config.xml
and fill with:
<?xml version="1.0"?>
<config>
<global>
<models>
<catalogsearch>
<rewrite>
<advanced>Endryu_AdvancedInStockOnly_CatalogSearch_Model_Advanced</advanced>
</rewrite>
</catalogsearch>
</models>
</global>
</config>
The last thing to do is to switch the module on by creating:
app/etc/modules/Endryu_AdvancedInStockOnly.xml
with content given below:
<?xml version="1.0"?>
<config>
<modules>
<Endryu_AdvancedInStockOnly>
<active>true</active>
<codepool>local</codepool>
</Endryu_AdvancedInStockOnly>
</modules>
</config>
Have fun:)
Comments
Monumentix
10-12-2011 11:10
jimmy
08-11-2011 22:15
DesignEnd
01-09-2011 03:04
pepper
30-08-2011 15:02
E.g. if there's a shoe product that is out of stock in 37 size, will it be visible in advanced results? (if i search only for size37 shoes)
I tested your code but it doesn't seem to work in this case.
Magento
24-08-2011 11:38
Jorge
11-03-2011 11:47
ecommerce web design
21-01-2011 10:19
Leave a comment