如何在 Magento 中以编程方式从搜索结果中排除产品)
How to exclude product(s) from search result programatically in Magento
我试图从填充搜索结果中排除产品。
它似乎在我的本地主机上运行良好,但在客户端开发服务器上运行不正常。
我正在观察事件
catalog_block_product_list_collection
而在观察者方法中,最后是这样的代码:
$observer->getCollection()
->addFieldToFilter('entity_id', array('nin' => array_keys($_excludeProducts)))
->clear()
->load();
它也适用于目录和搜索结果列表,但目前不适用于客户端开发服务器上的搜索结果列表。
非常感谢任何 guidance/help。
编辑:调试此方法给我一个空集合,但数据仍然从某处填充。
我 运行 在尝试使用此事件过滤此集合时遇到了类似的问题。
您是否在两种环境中设置了相同的平面类别和平面产品?在我的例子中,我的代码只适用于关闭平面表,因为我正在使用连接其他 EAV 属性。
在你的情况下,如果你使用的是平面,我认为你只需要做 addAttributeToFilter()
代替。
就我而言,这是我的观察者的样子:
function onCategoryCollectionLoad($observer) {
$collection = $observer->getEvent()->getCategoryCollection();
$customerGroupId = (int) Mage::getSingleton('customer/session')->getCustomerGroupId();
// hidden_from_groups is an EAV attribute; I couldn't figure out how to make it work with flat because it has a backend_model
$collection->addAttributeToSelect('hidden_from_groups');
$collection->addExpressionAttributeToSelect('should_be_hidden', "COALESCE(FIND_IN_SET($customerGroupId, {{attribute}}), 0)", 'hidden_from_groups');
// should_be_hidden is not a real db field (nor EAV attribute); it only exists because of the addExpressionAttributeToSelect() above.
$collection->addFieldToFilter('should_be_hidden', array('lt' => 1));
// I don't call $collection->load(); that will get called further down the line.
}
我改变了方法并使用了另一个事件:catalog_product_collection_load_before
找到了用更少的代码实现该方法的更好方法。 #优化
$excludeIds = array(2,3,4); //$excludeIds mixed
$observer->getCollection()
->addIdFilter($excludeIds, true); //exclude = true
该事件还有助于保持工具栏上正确的项目计数,因为它是在集合加载之前分派的。
我试图从填充搜索结果中排除产品。
它似乎在我的本地主机上运行良好,但在客户端开发服务器上运行不正常。
我正在观察事件
catalog_block_product_list_collection
而在观察者方法中,最后是这样的代码:
$observer->getCollection()
->addFieldToFilter('entity_id', array('nin' => array_keys($_excludeProducts)))
->clear()
->load();
它也适用于目录和搜索结果列表,但目前不适用于客户端开发服务器上的搜索结果列表。 非常感谢任何 guidance/help。
编辑:调试此方法给我一个空集合,但数据仍然从某处填充。
我 运行 在尝试使用此事件过滤此集合时遇到了类似的问题。
您是否在两种环境中设置了相同的平面类别和平面产品?在我的例子中,我的代码只适用于关闭平面表,因为我正在使用连接其他 EAV 属性。
在你的情况下,如果你使用的是平面,我认为你只需要做 addAttributeToFilter()
代替。
就我而言,这是我的观察者的样子:
function onCategoryCollectionLoad($observer) {
$collection = $observer->getEvent()->getCategoryCollection();
$customerGroupId = (int) Mage::getSingleton('customer/session')->getCustomerGroupId();
// hidden_from_groups is an EAV attribute; I couldn't figure out how to make it work with flat because it has a backend_model
$collection->addAttributeToSelect('hidden_from_groups');
$collection->addExpressionAttributeToSelect('should_be_hidden', "COALESCE(FIND_IN_SET($customerGroupId, {{attribute}}), 0)", 'hidden_from_groups');
// should_be_hidden is not a real db field (nor EAV attribute); it only exists because of the addExpressionAttributeToSelect() above.
$collection->addFieldToFilter('should_be_hidden', array('lt' => 1));
// I don't call $collection->load(); that will get called further down the line.
}
我改变了方法并使用了另一个事件:catalog_product_collection_load_before
找到了用更少的代码实现该方法的更好方法。 #优化
$excludeIds = array(2,3,4); //$excludeIds mixed
$observer->getCollection()
->addIdFilter($excludeIds, true); //exclude = true
该事件还有助于保持工具栏上正确的项目计数,因为它是在集合加载之前分派的。