There are number of improvements to the way themes are managed and set up in Magento 2. The use of the theme.xml definition file, introduced in Magento 1.9, and a new fallback system, are two of the most significant improvements. The fallback system in Magento 2 works in a similar way to Magento 1.x, but has the added advantage that you can select unlimited parent themes to inherit from / fallback to – all via the theme.xml file in your theme.

Let’s say you want to create a brand new theme based on the new Magento “Blank” theme. First, you would create a new folder in app/design/frontend called, for example WebDux/magento2theme. You would then create a theme.xml file in this directory (it is probably best to copy it from app/design/frontend/Magento/blank/theme.xml), name your theme, and choose any parent. In this case, we want Magento 2’s Blank theme.

How To Create Magento 2 Online Store Theme Guide

Creat Magento 2 Onlines Store Theme:

  • Creating a Magento theme folder
  • Declare your theme
  • Composer package
  • registration.php file
  • Creating static files, folders
  • Configure catalog product images
  • Declare Theme Logo
  • Basic layout elements
  • Layout files types and conventions.

So your theme.xml file should look something like this:

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


Magento 2 Theme Structure

Folder Structure

app/design/frontend/webdux/
├── magento2theme/
│   ├── etc/
│   │   ├── view.xml
│   ├── web/
│   │   ├── images
│   │   │   ├── logo.svg
│   ├── registration.php
│   ├── theme.xml
│   ├── composer.json

is theme vendor. e.g: webdux

is theme name. e.g: magento2theme

Ok, let’s go

Creating a Magento theme folder

Creating a folder for the theme:

  • Go to app/design/frontend
  • Creating a vendor folder app/design/frontend/ e.g: app/design/frontend/webdux
  • Create a theme folder app/design/frontend// e.g: app/design/frontend/webdux/magento2theme
app/design/frontend/
├── webdux/
│   │   ├──...magento2theme/
│   │   │   ├── ...
│   │   │   ├── ...

Declare your Magento 2 theme

Now we have folder app/design/frontend/webdux/magento2themne , now create a file named theme.xml that define basic information about theme such as: Name, parent theme (in case your theme inherits from an existing theme), preview image

<theme xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/theme.xsd">
     WebDux Magento2Theme 
     Magento/blank 
     
         media/preview.jpg 
     
 

Composer package

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.

To distribute your theme as a package, add a composer.json file to the theme directory and register the package on a packaging server.

composer.json example:

{
    "name": "webdux/magento2theme",
    "description": "N/A",
    "require": {
        "php": "~5.5.0|~5.6.0|~7.0.0",
        "magento/theme-frontend-blank": "100.0.*",
        "magento/framework": "100.0.*"
    },
    "type": "magento2-theme",
    "version": "100.0.1",
    "license": [
        "OSL-3.0",
        "AFL-3.0"
    ],
    "autoload": {
        "files": [
            "registration.php"
        ]
    }
}

registration.php file

You can add the following content to register the theme to Magento 2

Image properties are configured for each image type defined by the id and type attributes of the element:

<images module="Magento_Catalog">
	<image id="unique_image_id" type="image_type">
	100  
        100 
	

In Magento 2 default, it uses /web/images/logo.svg, in your theme, you can change to different file format such as png, jpg but you have to declare it.

Logo size should be sized 300x300px and you open file /Magento_Theme/layout/default.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    
        <referenceBlock name="logo">
            
                <argument name="logo_file" xsi:type="string">images/custom_logo.png
                <argument name="logo_img_width" xsi:type="number">300 
                <argument name="logo_img_height" xsi:type="number">300
            
        
    

Magento 2 Theme Basic layout elements

The basic components of Magento page design are blocks and containers.

A container exists for the sole purpose of assigning content structure to a page. A container has no additional content except the content of included elements. Examples of containers include the header, left column, main column, and footer.

How To Create Magento 2 Online Store Theme Guide

Layout files types and conventions

Module and theme layout files

The following terms are used to distinguish layouts provided by different application components:

  • Base layouts: Layout files provided by modules. Conventional location:
    • Page configuration and generic layout files: /view/frontend/layout
    • Page layout files: /view/frontend/page_layout
  • Theme layouts: Layout files provided by themes. Conventional location:
    • Page configuration and generic layout files: /_/layout
    • Page layout files: /_/page_layout

Create a theme extending file

Rather than copy extensive page layout or page configuration code and then modify what you want to change, in the Magento system, you only need to create an extending layout file that contains the changes you want.

To add an extending page configuration or generic layout file:


 |__/_
   |__/layout
     |--.xml
     |--.xml

For example, to customize the layout defined in/view/frontend/layout/catalog_product_view.xml, you need to add a layout files with the same name in your custom theme, like following:

/Magento_Catalog/layout/catalog_product_view.xml


 |__/_
   |__/page_layout
     |--.xml
     |--.xml

Override base layouts

Overriding is not necessary if a block has a method that cancels the effect of the originally invoked method. In this case, you can customize the layout by adding a layout file where the canceling method is invoked.

To add an overriding base layout file (to override a base layout provided by the module): Put a layout file with the same name in the following location:


  |__/
    |__/layout
      |__/override
         |__/base
           |--.xml
           |--.xml

These files override the following layouts:

  • /view/frontend/layout/.xml
  • /view/frontend/layout/.xml

Override Magento 2 theme layouts

To add an overriding theme file (to override a parent theme layout):


  |__/
    |__/layout
      |__/override
         |__/theme
            |__/
               |__/
                  |--.xml
                  |--.xml

These files override the following layouts:

  • /_/layout/.xml
  • /_/layout/.xml

Tips!

- Changing block name or alias. The name of a block should not be changed, and neither should the alias of a block remaining in the same parent element.
- Changing handle inheritance. For example, you should not change the page type parent handle.

Congrats! Now you have your first simple Magento 2 theme. You can try to create a complexible theme later.

Ref: Devdocs.magento.com, Stackoverflow.com, https://www.mageplaza.com/kb/how-to-create-magento-2-theme.html

0 Likes
33 Views

You may also like

Leave A Comment

Please enter your name. Please enter an valid email address. Please enter message.