Magento 1.9 Product Collection – Incorrect Pricing Data or Not Showing Out of Stock Items

collection;magento-1.9priceproduct-collectionstock

I am retrieving a product collection based on the current category. I need this collection to contain both in and out of stock products, and I also need the pricing data to be displayed correctly.

My store is configured to store product pricing as inclusive of tax, and to show prices as both inclusive and exclusive on the store front.

The code for obtaining the collection is as follows:

$category = Mage::registry('current_category'); 
$_productCollection = Mage::getModel('catalog/product')->getCollection()
                ->addCategoryFilter($category)
                ->addAttributeToSelect(['sku', 'name', 'price'])
                ->addAttributeToFilter('status', 1)
                ->addAttributeToSort('jump_number', Varien_Data_Collection::SORT_ORDER_ASC); 

The issue with the above is that when loading the price block using the getPriceHtml() method, both inclusive and exclusive prices are displayed as the inclusive price.

After some research, I found that adding a addFinalPrice() method to my collection gives me the pricing expected – a correct exclusive and correct inclusive price.

However, if I include the addFinalPrice() method, my collection no longer contains out of stock products.

Is there a way of returning the correct pricing data and having the collection contain both saleable and out of stock products?

Best Answer

addFinalPrice() also calls applyProductLimitationFilters. This method applies several filters according to how the collection is configured, like the store filter which only shows the products that are visible in the current store.

If you want to load all products, visible or not, like without the price filter, you can remove the store filter like this, before calling addFinalPrice():

$collection->setStoreId(null);
Related Topic