Trying to find a solution for the order of the attributes in layered navigation on the category list page.
Now I have to sort them in the attributes from backend, and it is not a good way with many entries.
How can I change the dropdown for the attributes to be sorted in 1-9
and a-z
? (not from backend)
Now it can be
B
P
8
A
When it should be
8
A
B
P
Thank you !
Best Answer
I will not give you the final solution but I will direct you to a place where you change the default behaviour.
So, if the attribute does not have a custom source it uses default source class which is
\Magento\Eav\Model\Entity\Attribute\Source\Table
. All of the attribute options are returned in a functiongetAllOptions($withEmpty, $defaultValues)
. If you check it, you will find that in this function anattributeOptionCollection
is created on which a functionsetPositionOrder($dir, $sortAlpha)
is called. In this function we have hardcoded sort order of the options collection -$this->setOrder('main_table.sort_order', $dir);
I can give you quick 2 options to make it work as you want it:
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::setPositionOrder
and change the$sortAlpha
variable totrue
- I am not sure how the sorting will behave, but it should be alphabeticaly\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::setPositionOrder
and change the sorting the way you wantThere are of course more options, you can also make plugins for
\Magento\Eav\Model\Entity\Attribute\Source\Table::getAllOptions
as well as you can override it (which I do not reccomend). Let me know which option you selected and good luck!