Font Awesome Icons without CDN in Magento2

How to ... in Magento

I recently worked on a custom module where one of the requirements was to use Font Awesome for custom forms and notifications / alerts.

Most of the people in the Magento community suggest to load Font Awesome via CDN, but this can cause ” blank squares ” and eventually slow down the front-end on slower connections.

So instead using a CDN I decided to add those libraries in Magento which will be merged with the static view files. Here is how it works.

1. Copy files

Go to Host Font Awesome Yourself and download the entire library. The zip file should contain css, js and less files as well as webfonts.

Fontawesome Less Files

Copy all less files to the following folder.

Next, copy all files from the webfonts folder to the below path.

Fontawesome Font Files

Make sure you only copy the less files ( no CSS files ).

2. Update Layout XML

Don’t load the library globally if you only need it at one place. In my case, I added those library to my custom controller only. For example:

3. Template

Once you have re-compiled your code, you should be able to use Front Awesome in your template. For example:

Font Awesome Example

Btw. the same works for the adminhtml area in ./view/adminhtml/.

Automatically Update The Year In The Footer

Magento Footer Copyright

In most of the projects that I am working on, it is still common practice to have the current year of the copyright text stored in System > Configuration > General > Design > Footer.

That means, every year somebody has to change the year manually. This is very time consuming, especially if you have to manage ten, twenty or more clients. Here is a quick example of how you can make it dynamic.

1. Set a custom string such as {YEAR}

Edit your footer in System > Configuration > General > Design > Footer and place the {YEAR} somewhere.

2. Replace the {YEAR}

Edit your footer.phtml in your package and replace getCopyright as shown below. For the year I am using Mage::getModel(‘core/date’) to have the correct timezone included.

File: ./app/design/frontend/{package}/default/template/page/html/footer.phtml

 

Don’t mess your theme files

Today I am explaining, how you can implement 3rd party services such as Google Analytics, Facebook Pixel or other tracking services in Magento. The reason why I am doing it is because for some reason it is still popular to put JavaScript code directly in theme files, such as page.phtml, success.phtml or footer.phtml. It might be a quick solution, but in most cases very difficult to maintain. With Magento you can easily create blocks which allows you to place the tracking codes by using layout handles without modifying any theme file.

Here is one example that you can easily adapt for other purposes.

1. Create a custom .phtml file

Go to your template folder ./app/design/frontend/{package}/default/template/ and create a new folder custom. Inside the custom folder create a file my_code.phtml and put all your JavaScript code in there.

2. Create or edit your local.xml file

Create or edit your existing local.xml file which should be located in ./app/design/frontend/{package}/default/layout/local.xml.

Inside of the <default> handle you have to place the following <reference>.

With the attribute name before_body_end and after_body_start you can define the position inside of the <body>. The <default> layout handle is present in every page request and will add your JavaScript code on all pages. You can use other layout handles such as <checkout_onepage_success> to add your JavaScript code only on the checkout success page.

That’s it. If you clear the cache, you should see the content of my_code.phtml in your HTML code.

3. Pass data to your block

You may need some custom data in your block, such as Google Analytics Account ID or other values for a proper tracking. Those data, you can add to your block with setData.

Use getData to receive the values.