【问题标题】:Magento Get Selected Filter In Layered NavigationMagento 在分层导航中获取选定的过滤器
【发布时间】:2021-01-18 17:14:44
【问题描述】:

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

【问题讨论】:

    标签: php magento


    【解决方案1】:

    所有应用的过滤器都存储在图层状态对象中。您可以使用以下 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")
    }
    

    【讨论】:

    • $item->getFilter()->getRequestVar(); // 过滤器的属性代码
    • 如何获取属性“color”的id?
    • 很好的答案!任何人都知道如何获取过滤器的代码(或编号 id)?因此,例如在我的情况下,颜色过滤器 id 是 187,我在数据库中查找它,但是如何使用 php 得到它?
    • 有人可以更新这个以显示这个的 Magento 2 版本。谢谢!
    • @ivan chepurnyi 我们如何在 magento 2 中编写上面的 sn-p?
    【解决方案2】:

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

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

    【讨论】:

    • 如何将您的代码应用到自定义模块中。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-27
      • 1970-01-01
      • 2011-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      • 1970-01-01
      相关资源
      最近更新 更多