Article Teaser
Author Image

By Andrzej

August 02, 2023

How to move category positions programmatically in Magento 2

In this blog post we will be discussing how to move category positions programmatically in Magento 2. Categories are an important part of any ecommerce website, and being able to control their positions can greatly enhance the user experience. In Magento 2 there are several methods you can use to move categories around, however doing it programmatically can save a lot of time and effort. We'll be exploring the steps you need to take to move category positions in Magento 2, so read on to find out more.

If you're using Magento 2 to build your eCommerce store, you might come across the need to programmatically adjust the position of a certain category. This can be necessary for a variety of reasons, such as rearranging the order of categories in your navigation menu or highlighting a certain category as more important than others.

Fortunately Magento 2 makes it possible to move category positions programmatically with just a few lines of code. Here's how to do it.

First, create a new PHP file in your Magento module directory, for example “app/code/MyVendor/MyModule”. You can name it anything you like but for the purposes of this example we'll call it "MoveCategory.php".

Add the following code to your new file:

Make sure to replace the $categoryId value with the ID of the category you want to move, and the $moveAfterCategoryId value with the new position you want to assign to the category (this should be an integer). Replace the $parentCategoryId value with the target parent category to which you want to move the category.

Run the PHP script from your command line or web browser, depending on your preference. With these simple steps you should now be able to programmatically move the position of any category in Magento 2. This can be a powerful tool for customizing the organization and navigation of your eCommerce store.


The post highlights a step-by-step guide on how to move category positions programmatically in Magento 2, an e-commerce platform widely used by online merchants. Magento 2 is a powerful platform that allows programmers to customize and manipulate the way categories are displayed on their online store. The importance of the topic can be seen in its ability to provide a better customer experience. When products are presented in the most logical and intuitive way, customers can effortlessly navigate and find what they are looking for, thereby increasing the likelihood of a sale. Moving category positions programmatically can be a daunting task but when developers use Magento 2 it becomes relatively straightforward. In conclusion, the blog post is essential to developers working with Magento 2 and online merchants.

Would you like to innovate your ecommerce project with Hatimeria?

Author Image

His hobbies are calisthenics, learning new technologies and hiking. In general, he likes to spend his free time productively.

Read more Andrzej's articles

Interested in something else?


Meet the team

Learn more about company and the team.

Join Us

Join us

Make an impact on your career.