简体   繁体   English

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

[英]Magento Get Selected Filter In Layered Navigation

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

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")
}

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();
}

在 Magento 2 中: $this->getLayer()->getState()->getData("filters")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM