override-magento-2

Developer Tutorial: How to Override a Template for Magento 2?

Right! In case you want to update some of your Magento 2 templates but rather to make changes directly to the files, adding extra data, or override a template in Magento 2 is available to you.

Override template Magento 2 could be considered as one of the most well-known tasks for Magento 2 platform developers and designers. Furthermore, it seems to become gradually easier to modify. 

What Is Override Template For Magento 2?

Of course! Before you start learning about any new subject, you should have fully understood it.

In the situation that you have already acknowledged the action “override a template in Magento 2”, you are already eligible to skip the following step to the next tutorial. 

However, there will be necessary to read this article word-by-word if the term “override” still sounds strange with you.

In this article, we have a full tutorial for you to master the “override template for Magento 2” step. Scroll down!

override-template-for-magento

>>> Press Here: Magento Default Themes to learn configuring easily.

In the technological aspect, Magento 2 override template in module is a feature that allows a subclass to provide a specific implementation of the method provided by one of its parent classes. 

To make it easier to understand, if the subclass has one or more methods that are similar to one of its parent classes, then that is the method override.

In the fundamental aspect, sometimes we need to make modifications to existing templates, but instead of making changes directly to finished template files, updating extra data called the category listing page is available as a more advanced method.

override-template-magento-2-bank

For example, the Account class represents a generic bank account. It provides a withdrawal function, which performs the necessary withdrawal process: subtract the amount withdrawn from the account balance. 

FeeBasedAccount is a type of bank account that charges a fee for each withdrawal, meaning that besides the necessary withdrawal process, there is an additional function of deducting withdrawal fees from the account balance. 

As such, FeeBasedAccount does need the content of the function provided by Account class but still has to override an extra function because such material is not enough to use. 

Condition Of The Override Function

override-function-magento

>>> Also check: Magento 2 theme builder that could be a great help.

The overriding method requires the parent class, and the subclass must be identical in three parts: 

  • Return data type.
  • Method name.
  • Parameter list.

The rules for the overriding template are:

  • It can only apply the overriding method to methods with access modifiers that are public or protected.
  • It cannot change the access modifier of its overrides (the method that is overridden and the method override must be the same access modifier).
  • It must be used with abstract or virtual keywords.
  • It cannot override a static method or non-virtual method.
  • Cannot use modifiers such as new, static, virtual for override method.
  • Cannot override the constructor

Override Template Magento 2 Setup

Please put it in your situation, owning an available created Magento 2 theme means you should create an extension equipped that will give shop owners (your customers) the ability to edit their product image. 

In this case, you will need the overridden template for Magento 2.

override-template-magento-guide

>>> Don’t forget: Magento premium themes for your likings.

Note for this article:

Terms:

  • The term “Page layouts,” “Page configurations,” and “Generic layouts” are referred to as “layout files.”
  • Layout files with instructions that override the default or parent theme files are referred to as “overriding layout files.”

There is no one particular method that should apply in all scenery:

  • If you are building a theme, use the template path method. 
  • If you are building a module, use the layout method. 

There are two methods available mentioned below. The former is used explicitly for a custom theme, and the latter is used for a custom module instead.

Adding a new template in a Magento 2 Theme

  • Add a template in your theme directory according to the template storing convention.
  • Assign your template to a block in the corresponding layout file

Remember to ensure that you have correctly created the appropriate folder hierarchy in your custom module to match the dev docs state.

Adding an override template to Magento 2 file (theme layouts)

override-template-themes-layout

1. Put a layout file with the same name in the following location:

<theme_dir>
  |__/<Namespace_Module>
    |__/layout
      |__/override
         |__/theme
            |__/<Parent_Vendor>
               |__/<parent_theme>
                  |--<layout1>.xml
                  |--<layout2>.xml

Example:

  • <theme_dir>/Magento_Checkout/layout/override/theme/Magento/luma/checkout_cart_index.xm

2. These files override the following layouts:

  • <parent_theme_dir>/<Namespace>_<Module>/layout/<layout1>.xml
  • <parent_theme_dir>/<Namespace>_<Module>/layout/<layout2>.xml

Example:

app/design/frontend/Magento/luma/Magento_Checkout/layout/checkout_cart_index.xml.

Adding an override template to Magento 2 file (base layouts)

override-template-base-layout

This means adding override a base layout provided by the module.

1. Put a layout file with the same name in the following location:

<theme_dir>
   |__/<Namespace_Module>
     |__/layout
       |__/override
          |__/base
            |--<layout1>.xml
            |--<layout2>.xml

Example:

<theme_dir>/Magento_Checkout/layout/override/base/checkout_cart_index.xml

2. These files override the following layouts:

  • <module_dir>/view/frontend/layout/<layout1>.xml
  • <module_dir>/view/frontend/layout/<layout2>.xml

Example:

Magento_Checkout/view/frontend/layout/checkout_cart_index.xml.

Possible Mistakes Warned

override-template-warning

Although the Magento 2 override template in the module layouts mechanism provides flexible customization, it can add logically irrelevant changes. 

Hence, you should not make one of these changes:

  • Changing block name or alias. 

It would help if you did not change the name of a block, and neither should the alias of a block remaining in the same parent element.

  • Changing handle inheritance. 

Also, you should not change the page type of parent handle.

Conclusion

>>> Hungry for more? Click here: Magento 2 themes!

There are still a lot of issues surrounding update jobs with override templates for Magento 2. However, we have summarized most briefly information and tried to explain it most understandably.

Read carefully and try to modify by yourself! Figure out which problems you will encounter, or will it be successful? Comment down below for your questions and experiences, eventually. 

Furthermore, if you have any suggestions for the article, please tell us, and we will update it right after.

 

About Steph Simpson

A freelance copywriter with 8-year-of-experience in her hand that has a wide range of writing profession. She likes blogging on her site.
Previous Magento Base Theme Comparison: Luma and Blank!
Next Magento Design Trends 2020 to Get Rid of The Old-fashioned

Check Also

Magento-2-Theme-Layout-For-Storefront

How To Use Magento 2 Theme Layout For Your Storefront?

In Magento 2, layout, block, and container are three paths to determine the structure of …

Leave a Reply

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