2

I want to sort the attributes in layered navigation in the alphabetical order.

http://i.prntscr.com/zDuYTVDbSWqv_507ew0JbQ.png

Sort in the order as:Climate, Color, ...

I tried to override the setPositionOrder() in \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection using before.

Ref:How to sort layered navigation attributes on Alphabetic order

My magento version is 2.3

But it didn't work. How can I do that? Please help.

1

you just need to override the filter.php file inside your child theme

app/design/frontend/{Vendor}/{themename}/Magento_LayeredNavigation/templates/layer/filter.php and then add the following code

    <?php 
    $offsets = array();
    $offsets1 = array();
    $offsets2 = array();
    $offsets3 = array();
    foreach ($filterItems as $filters) {
        $checklable = $filters->getLabel();
        if(preg_match("/[a-z]/i", $checklable)){
            array_push($offsets1, $filters);
        }
        elseif(preg_match("/,/i", $checklable)){
            array_push($offsets2, $filters);
        }
        elseif(preg_match("/-/i", $checklable)){
            array_push($offsets2, $filters);
        }
        elseif(strstr($checklable, '/')){
            array_push($offsets2, $filters);
        }
        else{
            array_push($offsets3, $filters);
        }
    }
    asort($offsets1);
    asort($offsets2);
    sort($offsets3);
 
    $offsets = array_merge($offsets3,$offsets2,$offsets1);
?>
<ol class="items">
    <?php foreach ($offsets as $filterItem): ?>
        <li class="item">
            <?php //echo '===>'; ?>
            <?php if ($filterItem->getCount() > 0): ?>
                <a href="<?= $block->escapeUrl($filterItem->getUrl()) ?>">
                    <?= /* @escapeNotVerified */ $filterItem->getLabel() ?>
                    <?php if ($this->helper('\Magento\Catalog\Helper\Data')->shouldDisplayProductCountOnLayer()): ?>
                        <span class="count"><?= /* @escapeNotVerified */ $filterItem->getCount() ?><span class="filter-count-label">
                            <?php if ($filterItem->getCount() == 1):?> <?= /* @escapeNotVerified */ __('item') ?><?php else:?> <?= /* @escapeNotVerified */ __('items') ?><?php endif;?></span></span>
                    <?php endif; ?>
                </a>
            <?php else:?>
                <?= /* @escapeNotVerified */ $filterItem->getLabel() ?>
                <?php if ($this->helper('\Magento\Catalog\Helper\Data')->shouldDisplayProductCountOnLayer()): ?>
                    <span class="count"><?= /* @escapeNotVerified */ $filterItem->getCount() ?><span class="filter-count-label">
                        <?php if ($filterItem->getCount() == 1):?><?= /* @escapeNotVerified */ __('item') ?><?php else:?><?= /* @escapeNotVerified */ __('items') ?><?php endif;?></span></span>
                <?php endif; ?>
            <?php endif; ?>
        </li>
    <?php endforeach ?>
</ol>

this code is working for me

0

For this,You need to override the getList() function in /vendor/magento/module-catalog/Model/Layer/Search/FilterableAttributeList.php

1
  • it's code not working it please help me how to specific attribute option filter by alphabetically
    – Rv Singh
    May 28 '19 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.