Magento 2 change category name programmatically

In this article, you going to see how to change the category name programmatically in Magento 2.We going to see in detail step by step.

Let’s start with the topic in detail step by step:Magento 2 change category name

To change the category name we going use event in Magento 2.

Before going to start with event in Magento 2, first we going to check some important point.

Point is how to create event in Magento 2.

Below are the steps for over-viewing of creating event in Magento 2.

1: Dispatch event
2: Create an event file: events.xml
3: Create Observer class
4: Flush cache and check all the result

Before start with following steps first understand catching and handling all the process of event.

Magento use all area of the definition to manage the store.

We will have a frontend and admin area.

With the help of configuration file we can achieve every points, they can be put in 3 places.

Create and place Under etc/ folder is the configuration file. Further which can be used in both admin and frontend.

Then another step is to place the under etc/frontend folder will be used for the frontend area.

Similarly for admin section place under etc/adminhtml folder will be used for the admin area.

Next important point regarding event configuration file.

So You can create events configuration file for each area as it is given in the below section like this:

Admin area: app/code/Techone/Testevent/etc/adminhtml/events.xml
Frontend area: app/code/Techone/Testevent/etc/frontend/events.xml
Global area: app/code/Techone/Testevent/etc/events.xml

Magento 2 Change shipping address programmatically

Step 1: Dispatch event

To achieve the change of category name for that we have to dispatch an Magento 2 event list which allow other module can change category name.

First of all create the module and declare the controller.

With the help of a controller, you can dispatch a custom event.

As the module is executed controller dispatch and check with the category id and set the category name in an observer and after that save the category.

For that we going use the controller as below:

Create File: app/code/Techone/Testevent/Controller/Index/Test.php
<?php
namespace Techone\Testevent\Controller\Index;
class Test extends \Magento\Framework\App\Action\Action
{
	public function execute()
	{
		$CategoryNameDisplay = new \Magento\Framework\DataObject(array('text' => 'Categoryname'));
		$this->_eventManager->dispatch('techone_testevent_change_categoryname', ['categoryname_text' => $CategoryNameDisplay]);
		echo $CategoryNameDisplay->getText();
		exit;
	}
}

Dispatch method will receive two following arguments: event name and another argument is an array data.

Using this example you come to know how we add the data object to the event and call it back to display the text.

Step 2: Create an event file: events.xml

Next step is to Create File: app/code/Techone/Testevent/etc/frontend/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="techone_testevent_change_categoryname">
        <observer name="mp_display_text" instance="Techone\Testevent\Observer\ChangeCategoryname" />
    </event>
</config>

In the above file, under config section you see, we define an event section with the name is the event name which was dispatch above in controller code section.

The class which will execute this event will be defined in the observer element by instance attribute.

The name of the observer is used to identify this with other observers of this event.

Step 3: Create Observer class

Now next step is to create a class to execute above event.

Then create File: app/code/Techone/Testevent/Observer/ChangeCategoryname.php

<?php

namespace Techone\Testevent\Observer;

class ChangeCategoryname implements \Magento\Framework\Event\ObserverInterface
{	
	/**
     * @var \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory
     */
    protected $_categoryCollectionFactory;

    /**
     * @var \Magento\Catalog\Api\CategoryRepositoryInterface
     */
    protected $_repository;

    public function __construct(
       \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
       \Magento\Catalog\Api\CategoryRepositoryInterface $repository
    ) {
        $this->_categoryCollectionFactory = $categoryCollectionFactory;
        $this->_repository = $repository;
    }

	public function execute(\Magento\Framework\Event\Observer $observer)
	{
		$categoryname = $observer->getData('categoryname_text');
		$categoryCollection = $this->_categoryCollectionFactory->create();
                $categoryCollection->addAttributeToSelect('*');
                $categoryCollection->addAttributeToFilter('name', array('eq' => 'test'));
                $currentCategory = $categoryCollection->getFirstItem();
		$currentCategory->setName($categoryname);
                $currentCategory->setStoreId(0);
                $currentCategory->setIsActive(true);
                $this->_repository->save($currentCategory);
		$displayText->setText('Execute event successfully.');
		return $this;
	}
}

This class will implement the ObserverInterface and declare the execute method.

Step 4: Flush cache and check the result

Let’s flush cache and check with the result.

I hope you liked my this article. If you have any queries or any question regarding this, Feel free to comment on Me.

2 thoughts on “Magento 2 change category name programmatically”

Leave a Comment