6

I am using this code to filter products by min and max price

updated code

     $collection = Mage::getResourceModel('catalog/product_collection')
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('status', 1)
            ->addAttributeToFilter(array(array('attribute'=>'visibility', 'eq'=>"4")));
     }
        if(sizeof($years)>0){
            $collection->addAttributeToFilter('year', array('in' => $years));
        }
        if(!empty($minprice) && !empty($maxprice)){
                $collection->addFieldToFilter('price',array('gteq'=>$minprice))
                           ->addFieldToFilter('price',array('lteq'=>$maxprice));
        }

But if products have special price it works wrong. I need to use final price here. How can I do this?


Answer that I found while exploring magento native layered navigation

$collection->addFinalPrice();
$select = $collection->getSelect();
$select->where('price_index.final_price >= ' . $minprice);
$select->where('price_index.final_price < ' . $maxprice);
6

Well the field for "final price" has code final_price in Magento Eav product table.

First you have to add final price to the collection as below:

$collection = Mage::getResourceModel('catalog/product_collection')->addAttributeToSelect('*');
$collection->addFinalPrice();

Then you can filter the collection by final_price as below:

$collection->addFieldToFilter('final_price',array('gteq'=>$minprice))
           ->addFieldToFilter('final_price',array('lteq'=>$maxprice));
  • I am getting an error Fatal error: Call to a member function getBackend() on boolean in /app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 816. In this line there is this code: $attrInstance = $this->getAttribute($attribute); $attrBackendStatic = $attrInstance->getBackend()->isStatic(); – Anna Dec 22 '15 at 12:34
  • Have you upgraded Magento recently to version ce 1.9 ? Which subversion ? 1.9.2.2 ? – Vicky Dev Dec 22 '15 at 12:35
  • version is 1.9.1.1 – Anna Dec 22 '15 at 12:35
  • @Jeremy It seems one of your attributes you're passing in the collection for filtering is broken, can you update your question to include the whole collection query you are using ? – Vicky Dev Dec 22 '15 at 12:40
  • code was updated – Anna Dec 22 '15 at 12:55
2

The safest way to add price filters is using the existing methods that take advantage of the price index.

Example:

// join the price index
$collection->addFinalPrice();

// add filters
$collection->addPriceDataFieldFilter('%s <= %s', ['final_price', $minPrice]);
$collection->addPriceDataFieldFilter('%s >= %s', ['final_price', $maxPrice]);

Alternatively, look at the layered navigation to see how price filters are implemented there.

  • Would this be possible to add "easily" here? github.com/OpenMage/magento-lts (I'd like to help - but can assume adding it to the grid + adding the frontend filters would already come a long way). thx Sean – snh_nl Jul 3 '17 at 11:52
  • This works perfectly for frontend, but how can I use that price filter in backend – Haris Mar 9 '18 at 19:33

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.