magento 2扩展分层导航

时间:2016-08-11 19:28:13

标签: magento magento2

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

我在代码中看到了aria-expanded =" false"并且在HTML的某个地方有class =" filter-options-content" with style =" display:none;"

任何人都知道在哪里编辑这个?

2 个答案:

答案 0 :(得分:5)

如果您正在使用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-&gt; getFilters();”获取变量中的所有过滤器。然后,为活动属性创建一个字符串,使用“$ active_filters_str = implode('',range(0,count($ filters)-1))”按索引列出它们;“然后在mage-init属性中的active属性旁边回显它。

希望这会有所帮助:)

答案 1 :(得分:3)

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

OPEN FILE:##

供应商\的magento \主题前端-亮度\ Magento_LayeredNavigation \模板\层\ view.phtml

并更改&#39; data-mage-init&#39;属性如下:

<?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; ?>