We’re sorry, an error has occurred while generating this email

Today, I was dealing with the following error message on one of our Magento2 production environment.

After spending 2 hours trying to find the source of the problem in our custom modules, I was able to fix the issue by disabling the core module Magento_SampleData.

What is this module for? This module gives you different sample data ( CMS, Catalog, Customers ) which is useful during development and testing phase. For your production environment it is not necessary and also not recommended to have it installed, as you can read here.

Algolia 1.6.1 or higher automatically update the product index

Algolia Indexing Operation

Algolia 1.6.1 or higher comes with a new feature that automatically updates your product index, every time you save a category. The disadvantage is that it will also update the product index, even if you just change an attribute which has nothing to do with URL rewrites or something else that can break search results. This can increase your indexing operations rapidly, therefore your monthly bills from Algolia as well.

Algolia System Configuration

To prevent unnecessary indexing, you should disable the new feature in System > Configuration > Algolia Search 1.x > Advanced > Update product on category products update and manage indexing manually with System > Index Management or simply with cron jobs.

Nginx Docker Container and getpwnam(“www-data”) problem

I am using nginx:1.10.1-alpine to run Nginx as a docker container. Today I have replaced the Nginx container and always got the status Restarting (1) Less than a second ago after starting the container.

With the command docker logs nginx I got the following error logs.

The error message basically means that the user www-data which I have defined in /etc/nginx/nginx.conf doesn’t exists in /etc/passwd. That makes sense, because nginx:1.10.1-alpine comes with a minimal setup. I my case I need the user www-data for other linked php-fpm containers. To fix the problem I just added the missing user during the container build.

Voilà.

Programmatically Create a Gift Card for Magestore Gift Card Module

This is a quick example of how you can programmatically create a code for Magestore’s Gift Card module.

1. Create the Gift Card

2. Link the Gift Card to a Customer

In order to be able to see the Gift Card in My Account > Gift Card you need to create a reference between Gift Card and a customer id. This is something you can do right after the Gift Voucher was successfully created.

Nginx and Cached CSS / JS files

Yesterday I had a pretty hard time with cached CSS / JS files on my developer environment. I supposed to review layout changes, but I always got the old CSS files. I have spent about 2 hours to find the reason. At the end the solution was to turn off the syscall sendfile in my virtual host configuration.

If you use Nginx as a reverse proxy to serve pages from an application server, you can leave sendfile turned off.

 

Reload Nginx Inside Docker Container

I am using docker for Nginx which is running with multiple virtual hosts. Sometimes I have to reload my updated nginx configurations, but I don’t want restart the container each time. Here is how you can reload your nginx without any downtime and without interrupting any connections.

1. Find your container name

Use docker ps to find your nginx container.

2. Reload Nginx

With docker exec -it {container_name} {command} you can directly access your container and execute commands. In my case the name of the container is nginx-server.

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.

Reset Administrator User Password in Magento 2

Magento 2 comes with a CLI that allows you to create a new temporary administrator user within few seconds. Change to the directory where your Magento installation ( such as app or bin folder ) is located and run the following command to create a user support with password 8cd98f00b20.

Voilà!

Now you can go to System > Permissions > All Users and update other users credentials.

Toggle Button Text with Internationalization

Life is easier with data attributes, but for some reason not everybody is using it to improve the JavaScript flexibility, especially for multilingual websites. Here is one example how you can create buttons with a dynamic text. In my example I am using alternating text for a preview and edit method.

Template “layouts/.html.twig” is not defined in “default.html.twig” at line 12.

I am using the statement include for dynamic sections in MODx.

It was working fine so far, but today I received the following exception that basically means, my snippet tried to include a file that doesn’t exist. The variable classes was empty that causes a broken website.

In my case I improved my snippet with an if condition.