Magento – How to change products sort order in magento 2

javascriptmagento2.3product-listproduct-sortingsorting

In product list page I need to change the sorting order. I have a current order like:

  • Description A-Z
  • Price High To Low
  • Price Low to High
  • Description Z-A

I need to change it to.

  • Description A-Z
  • Description Z-A
  • Price High To Low
  • Price Low to High

I tried using this JS.

window.onload = () => {
        const fontMenu = document.querySelector("#sorter");
        const options = Array.from(fontMenu.querySelectorAll("option"));
        // options.reverse();
             options.forEach((item,o) => {
                 options.splice(0,3);
                console.log(options,'op')
                options.unshift(item);
                fontMenu.appendChild(item);

             });
        }

Best Answer

Well, I think there are two ways.

  1. Create your own sort option in a module using a plugin. Something like this: Magento 2 How to add custom sort by option. Look at Luca S's answer. I think this person has a general idea of how to do it.
  2. Use Position option but modified: http://magento2x.com/magento-sort-newest-products-solution/.

Hopefully this will Help You.

Related Topic