Theme Concept in Magento 2

Aryan Srivastava
Nerd For Tech
Published in
4 min readMay 29, 2021

E-commerce plays a very important role in our life especially in this Covid-19 Pandemic. As we all know, it’s not safe going out for purchasing essential items for us. In that case, many vendors are offering online purchase for our goodwill.

Magento is one of the best open source platform for eCommerce. This gives users a great opportunities to control the look and functionality of storefront. This provides many tools and features like Marketing , Search Engine Optimization ( SEO ), Catalog Management, etc.

Magento 2 is a better and enhanced version of Magento.

Let’s Continue to customize our storefront :

What is Theme in Magento 2 ?

Theme in Magento 2 is responsible for the look and feel of the frontend as well as backend. They use a combination of PHP, XML, HTML, CSS and JavaScript to achieve the desired look. Themes override or extend existing PHP, HTML, CSS and JavaScript while providing extra functionality.

Magento 2 provide us predefined theme i.e,“ Luma” and “Blank”. Magento gives us option to create a new theme for our store or we can also inherit one from Luma and Blank.

How to create theme in Magento 2 ?

We are going to learn how to create new theme based on “Magento Luma”.

For creating new theme based on “Magento Blank” or “Magento Luma”, we have to create folder for theme. Let’s discuss about folder structure:

app/design/frontend/
├── <Vendor>/
│ │ ├──...<theme>/
│ │ │ ├── ...
│ │ │ ├── ...

Here,

<vendor> is the name of the vendor or company who is providing that particular theme.

<theme> is the name of the theme.

For example :

<vendor> => Tutorial

<theme> => Simple

Now, folder will be :

app/design/frontend/
├── Tutorial/
│ │ ├──...Simple/
│ │ │ ├── ...
│ │ │ ├── ...

Inside above folder we will create required file :

  1. theme.xml
  2. registration.php

These are required file for creating a theme and there is also one optional file i.e, composer.json file.

Declare theme in theme.xml file :

<theme xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/theme.xsd">
<title>Tutorial Simple</title>
<parent>Magento/luma</parent>
</theme>

Here,

<title> is the name of theme

<parent> is parent theme .

and also we can add theme preview image in theme.xml file.

<theme xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/theme.xsd">
<title>Tutorial Simple</title>
<parent>Magento/luma</parent>
<media>
<preview_image>media/preview.jpg</preview_image>
</media>
</theme>

Register theme in Magento System :

To register our theme in the system, we have to add a registration.php file in theme directory with the following code :

<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

use \Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::THEME, 'frontend/Tutorial/Simple, __DIR__);

Create composer.json ( optional ) :

Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

{
"name": "Tutorial/Simple",
"description": "N/A",
"config": {
"sort-packages": true
},
"require": {
"php": "~7.2.0||~7.3.0",
"magento/framework": "*",
"magento/theme-frontend-blank": "*"
},
"type": "magento2-theme",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"autoload": {
"files": [
"registration.php"
]
}
}

Configure images of storefront :

Product image sizes and other properties used on the storefront are configured in a view.xml configuration file. It is required for a theme, but is optional if exists in the parent theme.

Configure all storefront product image sizes in the view.xml file. For example, you can make the category grid view product images square by specifying a size of 250 x 250 pixels:

<image id="category_page_grid" type="small_image">
<width>250</width>
<height>250</height>
</image>

For more detail, click here

Create directories for static files :

Theme will likely contain several types of static files:

  • Styles
  • Fonts
  • JavaScript
  • Images
app/design/frontend/Tutorial/Simple/
├── web/
│ ├── css/
│ │ ├── source/
│ ├── fonts/
│ ├── images/
│ ├── js/

Structure of theme will be like :

app/design/frontend/Tutorial/
├── Simple/
│ ├── etc/
│ │ ├── view.xml
│ ├── web/
│ │ ├── css/
│ │ ├── source/
│ | ├── fonts/
│ | ├── images/
│ | ├── js/
│ ├── registration.php
│ ├── theme.xml
│ ├── composer.json

Once above steps are completed, we need to execute setup: upgrade command.

$ php bin/magento setup:upgrade

After executing upgrade command, theme will register in our database. You can check your theme under “theme table” in database.

In above steps we have successfully created theme for our storefront but we have to configure theme for storefront from Magento Admin:

Login to Magento Admin > Content > Design > Configuration > click on edit > Default Theme > Under applied theme dropdown select your theme > Save Configuration

After that, we need to clear magento cache

$ php bin/magento cache:flush

Now, we have successfully applied new created theme on our storefront.

Hope this article helped you to create a theme.

Next Tutorial topic is Layout Overview in Magento 2

If you like this article, you can buy me a cup of coffee https://www.buymeacoffee.com/aryansrivastava

Follow me on :

Linkedin Twiter

--

--

Aryan Srivastava
Nerd For Tech

Senior Software Engineer @Codilar Technologies | 2x Adobe Certified, Magento2, React.js