[Magento] Hiding out of stock products in advanced search results

Data dodania wpisu: 17-09-2010

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.
 
However his Magento modules (hiding out of stock products and hiding out of stock products in layered navigation) do not support an advanced search results.
 
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

I have created same thing as mentioned above but nothing is happening..could you please assist me for the same....

appcodelocalEndryuAdvancedInStockOnlyetcconfig.xml
appcodelocalEndryuAdvancedInStockOnlyCatalogSearchModelAdvanced.php
appetcModulesEndryu_AdvancedInStockOnly.xml

Search is not overwriting...please help
@immy->"awesome post. is it possible to make this optional for the end-user, so that they can check or uncheck a box depending on whether they want to see out of stock products in their search results or not?" I second this, you ever get this working????
awesome post. is it possible to make this optional for the end-user, so that they can check or uncheck a box depending on whether they want to see out of stock products in their search results or not?
This snippet supports only Simple Products (Configurable option's stock statuses are not validated).
is this working for configurable products too?
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.
Yes thank you...It's not easy to find current information on this...
Thanks for the code, ill try to modify advanced search too.
Good going keep-it up.Great information.Thanks for such a great blog.
Comments closed...

DesignEnd on Facebook

Inspiration