Magento 2

Magento 2: How to Display Categories in Alphabetical order Programmatically in Frontend

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.

Introduction

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 

app\code\VENDOR\EXTENSION\etc\di.xml

And add this code

<preference for="Magento\Catalog\Model\ResourceModel\Category" 
type="VENDOR\EXTENSION\Model\Category"/>

Step 2: Create another file Category.php in the following path

 app\code\VENDOR\EXTENSION\Model\Category.php

And simply add this code

<?php

declare(strict_types=1);
namespace VENDOR\EXTENSION\Model;

class Category extends \Magento\Catalog\Model\ResourceModel\Category
{
   public function getChildrenCategories($category)
   {
       $collection = $category->getCollection();
       /* @var $collection \Magento\Catalog\Model\ResourceModel\Category\Collection */       $collection->addAttributeToSelect('url_key')
                  ->addAttributeToSelect('name')
                  ->addAttributeToSelect('all_children')
                  ->addAttributeToSelect('is_anchor')
                  ->addAttributeToFilter('is_active',1)
                  ->addIdFilter($category->getChildren())
                  ->setOrder('name',\Magento\Framework\DB\Select::SQL_ASC)
                  ->joinUrlRewrite();
       return $collection;
   }
}
?>

That’s the way.

Conclusion

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 ?

Click to rate this post!
[Total: 8 Average: 4.5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

  • This is not working at all. main categories and Subcategories are not showing alphabetically.

    • Confirm that you are implementing the solution properly and there is no conflict with other extensions or theme files.

  • 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.

    • The blog is written earlier when the older Magento version has the options.
      Now you can try to replace the line :
      ->setOrder('name',\Magento\Framework\DB\Select::SQL_ASC)
      with
      ->setOrder('name','ASC')

Recent Posts

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

2 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

2 days ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

3 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

5 days ago

HYVÄ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We have brought exciting news for Magento store owners. Hyvä Themes recently released 1.3.6 and…

5 days ago

How Modern E-Commerce Platforms Leverage Docker & Kubernetes for Scalability

Your e-commerce platform is surging - orders are rolling in, traffic spikes are becoming the…

6 days ago