Hello Magento Techs ?,
Greetings of the day! Welcome to the MageComp Technical blog series. Today I am here with the most commendable topic, Magento 2: How to Display Categories in Alphabetical order Programatically in Frontend. Catch over our earlier released blog, How to Set Cart Page Items Custom Notice Message in Magento 2.
Products are added to different categories according to their type. Large Magento stores have lots of product categories. Supposing the categories are disorganized. The customer will find it difficult to locate the right product. As a result, the user will leave the site which decreases the business epitome. If these categories are displayed in alphabetical order, it becomes easy for the customers to explore the products. Quick search, quick shopping, and quick returns.
In Magento, sorting product categories from backend is possible with drag and drop. But if you are running with more than 200 categories, drag and drop can be a deadening job. Thus, the store with lots of categories needs to sort in frontend. The following code can help out with How to Display Categories in Alphabetical order Programatically in Frontend. Let’s get started ?
Steps to Display Categories in Alphabetical order Programatically in Frontend
Step 1: Create one file called di.xml in the following path
And add this code
Step 2: Create another file Category.php in the following path
And simply add this code
class Category extends \Magento\Catalog\Model\ResourceModel\Category
public function getChildrenCategories($category)
$collection = $category->getCollection();
/* @var $collection \Magento\Catalog\Model\ResourceModel\Category\Collection */
That’s the way.
I hope, now you are well aware of How to Display Categories in Alphabetical order Programatically in Frontend for your Magento 2 store. Any issues feel free to let me know in the comment section below. I would like to solve it for you. Also, share the article with your friends to help them with the same. Stay in the know!
Happy Coding ?
Tried everything step by step and still subcategories are sorted only by ID as it was before
Thanks for the info but actually didn’t work. Followed the exact instructions and the categories are still not in alphabetical order. Perhaps a step is missing here. Thanks anways.