0

I have the situation to filter product who is having Stock Availability as 'out of stock' in product listing page. Need to show only in product detail page. I tried with following event in my custom module

<events>
    <catalog_block_product_list_collection>
      <observers>
        <test_instockonly_list>
          <type>singleton</type>
          <class>test/observer</class>
          <method>addInStockOnlyFilter</method>
        </test_instockonly_list>
      </observers>
    </catalog_block_product_list_collection>
</events> 

and method 'addInStockOnlyFilter' in observer filter to add additional query to filter out of stock item

/**
   * Observes the catalog_block_product_list_collection event
   */
  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,
          'website_id' => Mage::app()->getWebsite()->getWebsiteId(),
        ))
    ;
  }

After clear cache and run the listing page. Still i can see out of stock item from product detail page.

What was i missed here. Kindly advice

  • I am not 100% what you are trying to do. Are you 1. Trying to not display out of stock items in catalog list pages, or 2. Prevent out of stock item from being accessable in the products details view page. Can you clarify. – ProxiBlue Oct 8 '14 at 9:05
  • @ProxiBlue Thanks,I am trying to avoid show out of stock item in listing page and need to access from product detail page – mymotherland Oct 8 '14 at 9:08
1

I think, from your question that you want to remove (not display) products that are out of stock in the category listing page.

Magento can do this out the box, via admin setting in system->configuration->inventory

enter image description here

If you have flat catalog enabled (and if not, you should), you would need to re-index for that change to take effect.

If, your intention is to not allow out of stock items to be viewed in the detailed page, you can simply use the view.phtml file to prevent that.

copy the file /app/design/frontend/base/default/template/catalog/product/view.phtml to your theme, and at the top, simply place a conditional to check of the product is in stock.

So you will have:

<?php $_product = $this->getProduct(); ?>
<?php if($_product->isSalable()): ?>

<THE REST OF THE TEMPLATE CODE>

<?php else; ?>

<DISPLAY A MESSAGE THAT THE PRODUCT IS NOT AVAILABLE>

<?php endif; ?>
0

If you want to do this by Observer then try the below code:

$ReadAdapter= Mage::getSingleton('core/resource')->getConnection('core_read');


         $joinCondition =$ReadAdapter()
            ->quoteInto('e.entity_id = stock_status_index.product_id'
                . ' AND stock_status_index.website_id = ?', $websiteId
            );

        $joinCondition .=$ReadAdapter()->quoteInto(
            ' AND stock_status_index.stock_id = ?',
            Mage_CatalogInventory_Model_Stock::DEFAULT_STOCK_ID
        );

        $collection->getSelect()
            ->join(
                array('stock_status_index' => $ReadAdapter->getTableName('cataloginventory/stock_status')),
                $joinCondition,
                array()
            )
            ->where('stock_status_index.stock_status=?', Mage_CatalogInventory_Model_Stock_Status::STATUS_IN_STOCK);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.