Debugging with fn_print_r();
You can use the
fn_print_r(); function to debug your PHP code. This function displays the information about the variables you specify in it.
In a PHP Controller
Let’s display the information about the administrators under Customers → Administrators. The URL of this page is http://example.com/admin.php?dispatch=profiles.manage&user_type=A.
According to the dispatch parameter in the URL, we should look for the controller called profiles.php. It is located in the app/controllers/backend directory of the store. We’re looking for a section beginning with
$mode == 'manage'.
Let’s find the following code:
If we add
fn_print_r($users); in the next line, we’ll see the information from the
In a Smarty Template
You can use
fn_print_r(); in .tpl files (templates), but the syntax is different:
Let’s modify the template of the Customers → Administrators page to display the information about administrators.
The file we’re looking for is manage.tpl. It is located in the design/backend/templates/view/profiles directory. Add the following code at the very beginning of the file:
It will achieve the same result as adding
fn_print_r($users); to the PHP controller.
Unlike PHP, Smarty and SQL query errors, AJAX errors don’t show up, even with error notifications and the development mode on. The only evidence of the error is that something doesn’t work as intended.
To see the error message, you have to disable AJAX. Inspect the code of the buggy element in your browser and remove/modify any occurrence of
cm-ajax—for example, change it to
Suppose the Add to Cart button on a product page in the customer area doesn’t work for some reason. If you use the code inspector, you’ll see that this button is a part of a form. Change
cm-ajax1 in the highlighted area.
If you click the Add to Cart button after that, you will see an error page with the name of the file and the number of the line where the error was encountered.