Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two fields in a Magento query?

I'm getting all my active special products using this code that I've found somewhere:

  $collection = $this->_addProductAttributesAndPrices($collection)
 ->addStoreFilter()
 ->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))
 ->addAttributeToFilter('special_to_date', array('or'=> array(
    0 => array('date' => true, 'from' => $todayDate),
    1 => array('is' => new Zend_Db_Expr('null')))
 ), 'left')
 ->setPageSize($this->get_prod_count())
 ->setCurPage($this->get_cur_page());

Now I want to get only the products that have a special price <= price, however I still can't realize how to do it.

I've been reading this page: http://www.magentocommerce.com/wiki/5_-_modules_and_development/catalog/using_collections_in_magento

and I tried this without success:

     ->addAttributeToFilter('special_price', array('lt' => 'price'))

Thanks for your help!

like image 888
Shaz Avatar asked Feb 22 '23 11:02

Shaz


1 Answers

You can try this, thanks for Zyava!

On my case:

 ->addAttributeToFilter('price', ['gt' => new Zend_Db_Expr('final_price')])
like image 50
Katapofatico Avatar answered Mar 05 '23 23:03

Katapofatico