如何在集合中比较Magento中的2个不同字段

Jos*_*ton 3 php magento

我正在尝试将Magento集合中的产品价格与其销售价格进行比较。具体而言,尝试查找所有销售价格大于或等于其基本价格的产品。

从逻辑上讲,我试图将字段名称放入addAttributeToFilter调用中:

->addAttributeToFilter('special_price', array('gteq' => 'special_price'));
Run Code Online (Sandbox Code Playgroud)

但这没有用。是否有一个很好的方法可以执行此操作,而无需我获得具有销售价格的每个产品来手动检查销售价格是否大于或等于其基本价格?

这很奇怪,说明有时候Magento的收藏有多么奇怪。事实证明,仅仅因为您->addAttributeToSelect('price')加入了呼叫链,并不意味着您会以一种简洁的方式获得价格。因此,我要做的是放入条件查询中,以便可以在查询的其他部分中访问它。我选择了->addAttributeToFilter('price', array('gteq' => 0))。我不得不以特殊的价格做同样的事情。

然后,当涉及放置where条件时,我仍然无法使用简单的名称来访问价格和特价,因此我不得不使用它们的表值,所以我的where语句最终被 $products->getSelect()->where('_table_special_price.value >= _table_price.value');

最后,这就是我的整个链的样子(那里有一些自定义属性):

$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('id')
->addAttributeToSelect('name')
->addAttributeToSelect('price')
->addAttributeToSelect('special_price')
->addAttributeToFilter('price', array('gteq' => 0))
->addAttributeToFilter('special_price', array('gteq' => 0))
->addAttributeToFilter('visibility', 4)
->addAttributeToFilter('discontinued', array('neq' => 1))
->addAttributeToFilter('status', 1)
;
Run Code Online (Sandbox Code Playgroud)

$ products-> getSelect()-> where(' _table_special_pricevalue> = _table_pricevalue');

Kal*_*esh 5

尝试这个:

->addAttributeToFilter('special_price', array ('gteq' => new Zend_Db_Expr('price') ) );
Run Code Online (Sandbox Code Playgroud)

如果它不起作用,则可以执行以下操作:

->getSelect()->where("special_price >= price");
Run Code Online (Sandbox Code Playgroud)