9

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?

2 Answers 2

3

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);
1
  • @fschmengler - thanks, this seems like a better option.
    – BrynJ
    Dec 14, 2015 at 9:51
1

I have resolved this issue now, and it's actually quite simple and logical - I just needed to go to Configuration -> Catalog -> Inventory and set 'Display Out of Stock Products' to Yes.

With the above set to Yes, and the addFinalPrice() method added to the collection, I now get all my products and the correct pricing data.

1
  • This is fine if you want to show out of stock products everywhere. If it's just for this collection, see my answer below Dec 13, 2015 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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