【问题标题】:magento 2 expand layered navigationmagento 2 扩展分层导航
【发布时间】:2016-08-11 19:28:13
【问题描述】:

在 Magento 2.0 中,默认情况下分层导航全部折叠,除了第一个过滤器,对我来说是价格。如何展开所有过滤器,以便每个过滤器选项在所有过滤器类别中都可见?

我在代码中看到 aria-expanded="false" 而在 HTML 中某处有 class="filter-options-content" 和 style="display: none;"

有人知道在哪里编辑吗?

【问题讨论】:

    标签: magento magento2


    【解决方案1】:

    如果您正在使用 Luma 主题并希望这样做,请确保创建您自己的主题作为 Luma 主题的子主题。您可以在此处找到更多信息 (https://community.magento.com/t5/Theming-Layout-Design-Questions/How-to-create-a-Child-Theme-in-Magento-2/m-p/33314#M384)

    然后,将位于“vendor\magento\theme-frontend-luma\Magento_LayeredNavigation\templates\layer\view.phtml”的文件复制到您子主题的相应区域。

    您需要将 data-mage-init 属性和“active”属性更改为一种格式,以指定要按其索引打开哪些过滤器。我有 6 个过滤器,所以我希望该属性为“0 1 2 3 4 5”。

    我在第 30-45 行之间做了一些更改,如下所示:

    <?php $wrapOptions = false; ?>
    <?php 
    $filters = $block->getFilters();
    $active_filters_str = implode(' ', range(0, count($filters)-1)); 
    ?>
    <?php foreach ($filters as $filter): ?>
        <?php if ($filter->getItemsCount()): ?>
            <?php if (!$wrapOptions): ?>
                <div class="filter-options" id="narrow-by-list" data-role="content" data-mage-init='{"accordion":{"openedState": "active", "collapsible": true, "active": "<?php echo $active_filters_str ?>", "multipleCollapsible": true}}'>
            <?php  $wrapOptions = true; endif; ?>
            <div data-role="collapsible" class="filter-options-item">
                <div data-role="title" class="filter-options-title"><?php /* @escapeNotVerified */ echo __($filter->getName()) ?></div>
                <div data-role="content" class="filter-options-content"><?php /* @escapeNotVerified */ echo $block->getChildBlock('renderer')->render($filter); ?></div>
            </div>
        <?php endif; ?>
    <?php endforeach; ?>
    

    首先,确保使用“$filters = $block->getFilters();”获取变量中的所有过滤器。然后,为活动属性创建一个字符串,使用“$active_filters_str = implode(' ', range(0, count($filters)-1));”按索引列出它们然后在 mage-init 属性中的 active 属性旁边回显它。

    希望这会有所帮助:)

    【讨论】:

    • 非常有帮助.. 工作,但我想多了解一点.. 我在implode(' ', range(0, count($filters)-1)); 上做了一个 print_r,它吐出了大量数字。但它正是我想要的。如果我只是想显示第一个类别,需要做“0,1”
    【解决方案2】:

    https://magento.stackexchange.com/questions/102259/open-category-filters-by-default-in-magento-2

    打开文件:##

    供应商\magento\theme-frontend-luma\Magento_LayeredNavigation\templates\layer\view.phtml

    并将“data-mage-init”属性更改如下:

    <?php foreach ($block->getFilters() as $filter): ?>
    <?php if ($filter->getItemsCount()): ?>
    <?php if (!$wrapOptions): ?>
        <?php $collapsibleRange = implode(' ', range(0, $filter->getItemsCount())); ?>
        <strong role="heading" aria-level="2" class="block-subtitle filter-subtitle"><?php /* @escapeNotVerified */ echo __('Shopping Options') ?></strong>
        <div class="filter-options" id="narrow-by-list" data-role="content" data-mage-init='{"accordion":{"openedState": "active", "collapsible": true, "active": "<?php echo $collapsibleRange ?>", "multipleCollapsible": true}}'>
    <?php  $wrapOptions = true; endif; ?>
    <div data-role="collapsible" class="filter-options-item">
    <div data-role="title" class="filter-options-title"><?php /* @escapeNotVerified */ echo __($filter->getName()) ?></div>
    <div data-role="content" class="filter-options-content"><?php /* @escapeNotVerified */ echo $block->getChildBlock('renderer')->render($filter); ?></div>
    </div>
    <?php endif; ?>
    

    【讨论】:

    猜你喜欢
    • 2018-04-03
    • 2022-11-20
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 1970-01-01
    • 2013-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多