5

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.

2 Answers 2

5

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);
5
2

Try FieldToFilter instead of AttributeToFilter

$searchedProducts = $this->_productCollectionFactory->create();    
$searchedProducts = $searchedProducts->addAttributeToSelect('*')
                        ->addFieldToFilter('final_price', array(
                                array('gt' => $pricefrom, 'lt' => $priceto),
                            )
                        );
1
  • it show error: The \"final_price\" attribute name is invalid. Reset the name and try again.
    – huykon225
    Jan 7, 2021 at 10:04

Your Answer

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

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