丑陋的答案:为了对价格字段应用限制,您需要恢复到(几乎)普通的旧 SQL。
首先是一点背景。
产品集合使用价格索引表 catalog_product_index_price 中预先计算的值。
当在集合上调用 addPriceData() 时,使用表别名 price_index 加入索引表。
假设我们有一个产品集合和一个包含价格下限的变量,初始化如下:
$lowerPriceLimit = 200;
/** @var $products Mage_Catalog_Model_Resource_Product_Collection */
$products = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('name');
您可能希望针对final_price 字段进行测试,因为这是购买产品时将使用的值(与price、special_price 或其他相比)。
这是一个如何添加条件的示例:
// Join the price_index table
$products->addPriceData();
// Apply price limit
$products->getSelect()->where('price_index.final_price >= ?', $lowerPriceLimit);
这是另一种选择。如果你想多用一点Magento 方式(即使用更复杂的方法),请使用:
$customerGroupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
$websiteId = Mage::app()->getWebsite()->getId();
$products->joinField(
'filter_price', // field alias
'catalog/product_index_price', // table
'final_price', // real field name
'entity_id=entity_id', // primary condition
array( // additional conditions
'website_id' => $websiteId,
'customer_group_id' => $customerGroupId,
'final_price' => array('gteq' => $lowerPriceLimit)
)
);
如果您还以第二种方式使用addPriceData(),您最终会在价格指数上使用双内连接,但它仍然可以工作...
都是相当低级的,但从好的方面来说,至少这仍然是相当符合标准的 SQL,所以它也应该是相当向上兼容的。
也许您可以将此与 Sylvain 的答案结合起来,使其成为分层导航价格范围过滤器的一部分。