Theme Concept in Magento 2

Theme-Concept-in-Magento-2

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 purchases for our goodwill. Magento is one of the best open-source platforms for eCommerce. This gives users great opportunities to control the look and functionality of the storefront. This provides many tools and features like Marketing, Search Engine Optimization ( SEO ), Catalog Management, etc. The best part is Theme Concept in Magento 2.

Magento 2 is a better and enhanced version of Magento.

Let’s Continue to customize our storefront:

What is the Theme in Magento?

The theme in Magento 2 is responsible for the look and feel of the frontend as well as the 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 provides us predefined theme i.e,“ Luma” and “Blank”. Magento gives us the option to create a new theme for our store or we can also inherit one from Luma and Blank.


How to create a theme in Magento?

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

For creating a new theme based on “Magento Blank” or “Magento Luma”, we have to create a folder for a theme. Let’s discuss 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/
 │   │   │   ├── ...
 │   │   │   ├── ...

Within the above folder, we will create the needed file:

  1. theme.xml
  2. registration.php

These are required files 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 the 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 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/

The structure of the theme will be like this:

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 the upgrade command, the theme will register in our database. You can check your theme under “theme table” in the 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. If you have any queries, you can ask me directly over email at aryansrivastavadesssigner@gmail.com or contact me here.

If you like this article, you can buy me a cup of coffee Buy me a Coffee.

Reference Links :

  1. https://devdocs.magento.com


About Aryan Srivastava

I’m a Passionate Magento 2 Frontend Developer. I have 2.5+ years of experience in Frontend. I cherish taking up complex problems and turning them into the best layout in the easiest and proper way. I love to work on Magento 2 e-Commerce Solutions.

View all posts by Aryan Srivastava →

Leave a Reply

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