Magento – Magento 2: How to filter product collection using product’s final price

magento-2.1magento-2.1.1priceproduct-collection

How to filter product collection using product's final price. As I have special price and catalog price rule applied on the products.

here is my code:-

$searchedProducts = $this->_productCollectionFactory->create();    
$searchedProducts = $searchedProducts->addAttributeToSelect('*')
                        ->addAttributeToFilter('price', array(
                                array('gt' => $pricefrom, 'lt' => $priceto),
                            )
                        );

Above code filter the product using price.
But not give any result if i am using final_price in the product.

I want to filter products form there final price range.

Best Answer

addAttributeToSelect is not working because of addAttributeToSelect

When a field is an eav Attribute

But final_price field is not an eav attribute.

At magento collection, it will add via query.

Mistake, if you have want filter the collection by final price then you should add Finals to your collection.So,you should add **addFinalPrice()**

$searchedProducts = $this->_productCollectionFactory->create();    
$searchedProducts = $searchedProducts->addAttributeToSelect('*')->addFinalPrice();

Then filter the collection via final_price,you should use collection getSelect Query

$searchedProducts->getSelect()->where("price_index.final_price > ".$pricefrom)
                  ->where("price_index.final_price < ".$priceto);
Related Topic