Magento 2 creating custom console command to change the current theme

Magento 2 Creating custom console command to switch/change the frontend theme

In the Last tutorial we have learned how to create a new custom console command in Magento2. This Article is a followup of our previous article, those who want to check the previous article please click the link below.
http://mydons.com/magento-2-create-custom-console-command-with-sample-usecase

Registering the New Console Command

In Order to create a new console command it needs to be registered in the di.xml.
In Addition to the existing command which lists the installed themes in the site, we need to add the new command.

The di.xml will look as shown below.

Workflow of the new console command

The new console command will accept the theme code as an argument, the corresponding THEME
will be assigned to the frontend, if the theme is not found a failure message will be displayed.

Creating a subclass for registering new command

The Subclass for settheme is created with the below name
Mydons\ThemeConsoleCmd\Command\SetTheme

Defining the console command details

As per the convention our new subclass will contain two methods configure and execute.
The configure method will contain the command name,description,argument etc.

Command Execution

The execute method accepts the theme_code as an Input and loads the theme.
The Loaded theme is assigned to the default store.

Implemented Code

Once the above change were done and cache clean we can see the help for the command function

Help for the Custom Command

Change Luma Theme to Blank Theme via command

Leave a Comment

Your email address will not be published. Required fields are marked *

8 − two =