Theme concept in Magento 2
Electronic commerce plays a very important role in our life especially in this Covid-19 pandemic. As we all know, it is not safe to go out to buy essentials for us. In this case, many sellers offer online shopping for our goodwill.
Magento is one of the best open source platforms for e-commerce. This gives users great opportunities to control the appearance and functionality of the storefront. This provides a lot of tools and features like marketing, search engine optimization (SEO), catalog management, etc.
Magento 2 is an improved and improved version of Magento.
Let’s continue to personalize our showcase:
What is the theme in Magento 2?
The theme in Magento 2 is responsible for the appearance of the frontend as well as the backend. They use a combination of PHP, XML, HTML, CSS, and JavaScript to achieve the look you want. Themes replace or extend existing PHP, HTML, CSS, and JavaScript while providing additional functionality.
Magento 2 provides us with a predefined theme, namely “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 2?
We will learn how to create a new theme based on “Magento Luma”.
To create a new theme based on “Magento Blank” or “Magento Luma”, we need to create a folder for the theme. Let’s talk about the folder structure:
app/design/frontend/
├── <Vendor>/
│ │ ├──...<theme>/
│ │ │ ├── ...
│ │ │ ├── ...
<vendor> is the name of the vendor or company that offers that particular theme.
<theme> is the name of the theme.
For example :
<provider> => Tutorial
<theme> => Simple
Now the folder will be:
app/design/frontend/
├── Tutorial/
│ │ ├──...Simple/
│ │ │ ├── ...
│ │ │ ├── ...
- theme.xml
- registration.php
Declare the theme in the 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>
<title> is the name of the theme
<parent> is the parent theme.
and we can also add a preview image of the theme to the 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>
To save our theme in the system, we need to add a registration.php
file in the 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__);
Composer is a PHP dependency management tool. It allows you to declare the libraries that 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"
]
}
}
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 it exists in the parent theme.
Configure all storefront product image sizes in the view.xml
file. For example, you can square the product images in the category grid view by specifying a size of 250 x 250 pixels:
<image id="category_page_grid" type="small_image">
<width>250</width>
<height>250</height>
</image>
Create directories for static files:
The theme will likely contain several types of static files:
-
- fashions
- Fonts
- Javascript
- Pictures
app/design/frontend/Tutorial/Simple/
├── web/
│ ├── css/
│ │ ├── source/
│ ├── fonts/
│ ├── images/
│ ├── js/
app/design/frontend/Tutorial/
├── Simple/
│ ├── etc/
│ │ ├── view.xml
│ ├── web/
│ │ ├── css/
│ │ ├── source/
│ | ├── fonts/
│ | ├── images/
│ | ├── js/
│ ├── registration.php
│ ├── theme.xml
│ ├── composer.json
$ php bin/magento setup:upgrade
Log in to Magento Admin> Content> Design> Configuration> click Edit> Default Theme> Under the drop-down list of the applied theme, select your theme> Save configuration
After that we have to clear the magento cache
$ php bin/magento cache:flush