【发布时间】:2021-01-18 17:14:44
【问题描述】:
在Magento中,如果在分层导航中选择“颜色”属性,“颜色”的值会自动消失并显示结果。如何检索所选过滤器的名称?
【问题讨论】:
在Magento中,如果在分层导航中选择“颜色”属性,“颜色”的值会自动消失并显示结果。如何检索所选过滤器的名称?
【问题讨论】:
所有应用的过滤器都存储在图层状态对象中。您可以使用以下 sn-p 轻松检索它们:
$appliedFilters = Mage::getSingleton('catalog/layer')->getState()->getFilters();
它将返回一个过滤器项目对象数组。您可以通过执行以下操作来检索单个过滤器项的名称和应用值:
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")
}
【讨论】:
您可以通过此代码获取过滤器的属性代码或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")
【讨论】: