How to Create a Custom Theme in Magento

How to Create a Custom Theme in Magento
Share!

Even though, Magento 2 is in development phase it has already made a lot of buzz in the e-commerce industry because of the new and improved front-end approaches, it is expected to offer. While the front-end improvements seems to make the Magento theme process a lot more efficient, many developers (especially beginners) aren’t still aware of the process of creating a custom theme in Magento 2.

Through this post, I’ll be covering step-by-step process of creating a custom Magento 2 theme. But before that let’s first have an overview of Magento Theme.

Magento Theme – An Overview

A theme helps to create a visual representation of your Magento store, using a combination of the following:

  • Custom templates
  • Layouts
  • CSS/LESS files

Magento application offers two different design themes, namely Luma (also called as a demonstration theme) and Blank (used for custom theme creation). Since we need to create a custom Magento theme, we’ll be using the Magento Blank theme.

In order to create a new theme using Blank, you’ll have to customize it. But, making changes to the default theme will make the changes overwritten, every time the new version of your default files is updated. A viable alternative to this problem is to create a new custom theme that inherits the functionality from the default or any existing theme.

Put it simply, we’ll create a custom child theme that inherits existing theme’s functionality. Doing so, will help you going through the hassle of creating a new standalone theme from the ground up. So, instead of copying extensive set of files and modifying them, you will only need to override and extend existing files within the parent theme.

Let’s now begin discussing the steps involved in creating a new Magento theme (i.e. the one one by one.

Understanding Theme Creation Process

In this section, we will be covering about creating files that would help in theme creation. In addition, we’ll also have a glimpse of the process of adding a logo to the theme or how we can configure images.

Step 1 – Create a Theme Directory

The very first step, you need to perform when creating a custom theme requires you to create your theme directory structure. For doing so, follow below mentioned key points:

  1. Access and open up your Magento install admin backend, and then move to dir>/app/design/frontend.
  1. Most of the e-stores offers products of different vendors, and so it’s important to have a separate vendor directory, where all products offered by the vendors are listed. So, your next step is to create a new directory with the name similar to your vendor name: /app/design/frontend/<Vendor>.

In case, you’re using any built-in Magento theme, then you need to refer to the directory: app/design/frontend/Magento

  1. Now that you’ve created the vendor directory, create another directory with the name similar to that of your theme under the vendor directory as shown below.

app/design/frontend/

>> <Vendor>/

>>…<theme>/

>> …

Step 2 – Understanding Theme Declaration

Once you’ve finished creating the directory structure of your theme, you’ll have to create theme.xml to define your theme layout. The xml file must contain at least the child theme name and the parent theme name. All the updates made to your theme layout will be stored in this file.

There are two tasks that your theme layout needs to perform.

  • First add or copy contents from the current theme.xml to your theme directory app/design/frontend/<Vendor>/<theme>
  • Secondly, to make your theme visible, you need to configure it. For this purpose, use the following example:

<theme xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=”../../../../../lib/internal/Magento/Framework/Config/etc/theme.xsd”>

<title>New theme</title> <!– your theme’s name –>

<parent>Magento/blank</parent> <!– the parent theme, in case your theme inherits from an existing theme –>

<media>

<preview_image>media/preview.jpg</preview_image> <!– the path to your theme’s preview image –>

</media>

</theme>

In order to make sure that your theme is recognized by the Magento application, get logged-in in to the Magento admin panel and check whether you can see the theme in the grid under Content-> Design -> Themes.

pic-2

Step 4 – Configure Images (Changing Size of the Product Images)

When uploading the product images, if its size exceeds than the ones of the default theme, you’ll have to add a view.xml file. This file contains configuration details of the product image sizes that are displayed on the storefront. Let’s now see the steps you need to follow for configuring the images:

  1. Log-in to your Magento server, however, you’ll need to access the server as a user having a certain set of permissions. You must have permissions to create directories as well as files in the directory of the Magento installation.
  1. In your theme folder, create the etc directory.
  1. Next, copy the view.xml file from your existing theme’s “etc directory” (let’s say, for example, from the Blank theme) to your custom theme’s newly created etc directory.
  1. Lastly, you need to configure all of the product image sizes used on your store front-end in view.xml. For example, you can choose to portray the product images in the product category grid view as square, by assigning it a size of 250 x 250 px. Let’s have a glimpse of how the image configuration would look like:

<var name=”category_page_grid:type”>small_image</var>

<var name=”category_page_grid:width”>250</var>

<var name=”category_page_grid:ratio”>1</var>

<var name=”category_page_grid:height”>250</var>

Step 5 – Create Directories for Your Theme’s Static Files

Just like any standard Magento theme, your theme will also contain multiple static files, including styles, JavaScript, images and other files. Make sure to store each file type in a separate “web” sub-directory within your theme folder, as follows:

app/design/<area>/<Vendor>/<theme>/

>> web/

>> css/

>> source/

>> fonts/

>> images/

>> js/

Note: All the general theme static files should be stored in the …<theme>/web/images, for example, the logo of your theme needs to be stored in …<theme>/web/images. There’s a greater possibility that your theme will include some module-specific files. These files are stored in the sub-directories like …/<theme>/<Namespace_Module>/web/css and related sub-directories. However, this is a whole new topic of discussion. So, for now we will skip this topic.

Applying and Configuring Your Theme in Magento

This section will let you know how you can apply your theme to the Magento storefront. For doing so, simply follow the below listed steps:

Step 1 – Open up your Magento install admin panel and navigate to CONTENT-> Design-> Themes. Make certain that you can see your theme in the given theme list.

Step 2 – From your admin panel visit Stores -> Configuration -> Design.

Step 3 – From the “Scope” drop-down field, choose the store view to which you would like to apply your theme.

Step 4 – Next, from the “Design” Theme tab, choose the theme you’ve just created listed in the Design Theme drop-down.

Step 5 – Lastly, click on the “Save Config” option to apply the changes you have made so far. And once it’s done, make sure to reload the pages of your storefront.

Note: In case, caching is enabled in your Magento back end, it’s important to clear the cache or else you may not be able to see the changes you’ve just applied.

Conclusion

Hope that reading this tutorial will serve as a basic guide that will help you understand the process of creating a custom theme in Magento 2.

Author Bio :

Isabella Morris is currently employed with a renowned custom Magento development company and is also engaged in writing informative articles on best tools and tricks for Magento development. Her write-ups have proved beneficial for a wider group of Magento developers across the globe.



Leave a Reply

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