How To

How to Move Category Position Programmatically in Magento 2?

Hello Magento Friends,

In today’s instructional guide, I will explain How to Move the Category Position Programmatically in Magento 2?

A Magento 2 store contains various categories to classify the products based on their type. The category comprises various subcategories and products. You can quickly move subcategories in Magento 2 by drag-and-drop from the Admin Panel. Another way to move the category position is by executing it programmatically.

Let’s see how to move the category position from one parent category to another parent category along with its products programmatically in Magento 2.

Steps to Move Category Position Programmatically in Magento 2:

Category position before running the script

 

Step 1: Create a file in your Magento root directory at the below path. 

magento_root_directory\Changecategoryposition.php

Now add the code as follows

<?php
use Magento\Framework\AppInterface;
try
{
    require_once __DIR__ . '/app/bootstrap.php';
} 
catch (\Exception $e) 
{
    echo 'Autoload error: ' . $e->getMessage();
    exit(1);
}
try
{
 $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
 $objectManager = $bootstrap->getObjectManager();
 $appState = $objectManager->get('\Magento\Framework\App\State');
 $appState->setAreaCode('frontend');
 
       $category = $objectManager->get('\Magento\Catalog\Api\CategoryManagementInterface');

 
 $categoryId = 5; //id of The category to be moved
        $parentId = 6; //id of parent category where the new category to move
        $afterId = 7; //id of category after which you want to move
        
        $CategoryMoveSuccess = $category->move($categoryId, $parentId, $afterId); 
        
        if($CategoryMoveSuccess)
        {
            echo "Successfully Move Category Position";
        }  
        else
        {
            echo "Not Move Category Position"; 
        }
}
catch(\Exception $e)
{
 echo "Error : ".$e->getMessage();
}

?>

Step 2: After the above step, you will need to run the below-given URL.

https://yourdomain.com/Changecategoryposition.php 

Category position after running the script

Conclusion:

This way, you can change the category position in Magento 2 programmatically. Alternatively, you can integrate Duplicate Categories Extension for Magento 2 to create a copy of the category.

Share your doubts with me in the comment box. Stay in touch with us for more Magento 2 solutions.

Happy Coding!

Click to rate this post!
[Total: 2 Average: 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.🏏

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

3 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

5 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

5 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

6 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

7 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

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

1 week ago