php Magento 在分层导航中获取选定的过滤器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11862967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:18:00  来源:igfitidea点击:

Magento Get Selected Filter In Layered Navigation

phpmagento

提问by blakcaps

In Magento, if "color" attribute is chosen in layered navigation, values of the "color" are automatically disappears and results are getting displayed.How to retrieve the name of the selected filter?

在 Magento 中,如果在分层导航中选择“颜色”属性,“颜色”的值会自动消失并显示结果。如何检索所选过滤器的名称?

回答by Ivan Chepurnyi

All the applied filters are stored in layer state object. You can easily retrieve them by using the following snippet:

所有应用的过滤器都存储在图层状态对象中。您可以使用以下代码段轻松检索它们:

$appliedFilters = Mage::getSingleton('catalog/layer')->getState()->getFilters();

It will return you an array of filter item objects. You can retrieve name and applied value of a single filter item by doing something like this:

它将返回一组过滤器项目对象。您可以通过执行以下操作来检索单个过滤器项的名称和应用值:

foreach ($appliedFilters as $item) {
    $item->getName(); // Name of the filter
    $item->getLabel(); // Currently selected value
    $item->getFilter()->getRequestVar(); // Filter code (usually attribute code, except category filter, where it equals "cat")
}

回答by Rehan Mobin

You can get filter's attribute code or id through this code:

您可以通过此代码获取过滤器的属性代码或 id:

$appliedFilters = Mage::getSingleton('catalog/layer')->getState()->getFilters();
foreach ($appliedFilters as $item) {
    echo $item->getFilter()->getAttributeModel()->getAttributeId();
    echo $item->getFilter()->getAttributeModel()->getAttributeCode();
}