3

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.

3

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);
| improve this answer | |
1

Try FieldToFilter instead of AttributeToFilter

$searchedProducts = $this->_productCollectionFactory->create();    
$searchedProducts = $searchedProducts->addAttributeToSelect('*')
                        ->addFieldToFilter('final_price', array(
                                array('gt' => $pricefrom, 'lt' => $priceto),
                            )
                        );
| improve this answer | |

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.