¿Cómo comparar dos campos en una consulta de Magento?

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

  $collection = $this->_addProductAttributesAndPrices($collection)
 ->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')

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'))

Gracias por su ayuda!

preguntado el 08 de noviembre de 11 a las 17:11

the second parameter 'price' is being treated as string -

That's right, that's why I'm asking, I don't know the right way to compare those values -

2 Respuestas

Puedes probar este, thanks for Zyava!

On my case:

->addAttributeToFilter('price', array ('gt' => new Zend_Db_Expr('final_price') ) )

contestado el 23 de mayo de 17 a las 15:05

A lesser efficient solution would be to iterate through the collection,

$products = array();
$collection = Mage::getResourceModel('catalog/product_collection');
foreach($collection as $col) {
    $product = Mage::getModel('catalog/product')->load($col->getId());
    if( $product->getPrice() > $product->getSpecialPrice() ) {
        $products[] = $product;

you will have an array of products in the end...

respondido 08 nov., 11:22

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.