How to create a Theme Plugin for Magento
Tips about getting started with Magento and creating a simple theme plugin.
Magento is one of the most powerful, actively developed open source webshop engines around. It has rich built-in functionality and a wide variety of plugins and themes available online. Because it’s based on PHP, it can even be deployed to cheap hosting services.
These were the main reasons I chose it for a recent project, despite the fact that I rarely code in PHP, and I know other platforms much better.
Also, it has a huge community, and amazing documentation, so finding resources online is really easy.
In this post, I collected some tips about how to get started with Magento and create a simple site with a custom theme.
Many service providers maintain packages and tools for it, so you can install Magento easily with cPanel in a few clicks.
However, if you decide to roll your own server, it’s easy to install Magento yourself.
Create a theme
The documentation mentions that while it’s possible to directly change the design files in the Magento installation, it might complicate upgrades in the future.
Instead, the design changes should be contained in a separate Theme plugin.
To create a custom Theme, first create a folder in the following location:
app/design/frontend/<Vendor>/<Theme-Name>. In the example below I use advancedweb as the Vendor and my-first-theme for theme name.
Then create the following 3 files that contain the necessary information to declare and register your theme:
theme.xml you declare the theme, and specify its parent:
composer.json is required to distribute your Plugin as a Composer package. The official guide suggests that this file is optional, but I could not get my theme working without it.
It lists what packages are required for your plugin to work and points Magento to the
registration.php, that has contains some initialization code needed by Magento to load the theme:
When these files are in place, the Theme can be applied to the whole store in the
Stores / Configuration panel.
The theme can be applied to single pages or shop items too. This is handy when you have to add small design changes for just one page. For this, simply create a new child theme that contains the necessary changes, and use that in the page.
Before adding anything to the theme it’s important to note that Magento in production mode aggressively caches the static view files to provide better performance. This is really inconvenient during design development, so make sure to run Magento in development mode.
Otherwise, you have to flush Magento Cache after even simplest of CSS changes in the
System / Cache Management admin panel. (Also flush
Add Custom Styles and Scripts
Now the theme doesn’t do anything special yet, it looks just like the default one:
So let’s add some custom styling rules to make it a bit more unique.
First, create a
web/css folder and a
custom.css file in the theme folder. For this simple example, this file will contain all our style modifications.
Then create a
default_head_blocks.xml file in the
Magento_Theme/layout folder. In this file, you can specify the CSS files to be loaded with each page.
The same goes for the script files, just add them to your theme and register it in the XML configuration above. Make sure to copy this file from the parent theme you’re using to avoid accidentally leaving something out.
Set your Magento application to developer mode if you are modifying the theme files, or just clear Magento Caches in the
System / Cache Management administration panel after you are done with the modifications to see the result.
Custom Logo, Favicon, and Footer
The logo and the favicon can be uploaded on the
Stores / Configuration / Design admin page. It’s worth checking out the parameters you can tune, especially in the
HTML head section.
The footer content can be defined on this page as well. You can simply change the copyright text, or inject custom HTML code.
The localization options are also found in the Store configuration panel. In the General page, you can set the Country, State and Locale options. The options regarding prices can be found under the separate Currency Setup category.
You can ship your custom localization files with the theme by adding CSV files to the
i18n folder in your theme. The name of the CSV file should be in Magento’s locale format, like en_US.csv, and it has to contain two columns.
The first column is the localization key, while the second one is the localized value in the given language. What’s interesting in Magento is, that the localization key is not some arbitrary keyword, but the original English version of a given text, like “Shopping Options”. This makes it very easy to translate or customize most labels in the webshop.
Wrapping it up
As we’ve seen in this post, it’s quite easy to create a custom theme for Magento, making it possible to tune the look and feel and client-side scripts. With a Theme Plugin, you can easily create unique sites for your visitors.