How Do I Resolve Common Magento 2 Errors?
Magento 2 is a great eCommerce platform with many extensions and custom hooks to add your desired functionality. Sometimes adding that custom functionality doesn't go as smoothly as you'd like though. Here are a few ideas to resolve some common issues we've run into with Magento 2:
- Fatal PHP errors – "Reset Magento"
- We've run into fatal PHP errors quite a bit, but that's not a bad thing! It usually means there's a connection missing from your codebase to your database. Majority of the time, the error screen will instruct you on what steps to take to resolve. But for those times when things just seem out of sync, you can try running this sequence of Magento CLI commands as a "reset" script of sorts:
- SSH into your environment. Then run these commands from the site root:
- php bin/magento setup:upgrade
- php bin/magento setup:di:compile
- php bin/magento indexer:reindex
- php bin/magento cache:flush
- We've run into fatal PHP errors quite a bit, but that's not a bad thing! It usually means there's a connection missing from your codebase to your database. Majority of the time, the error screen will instruct you on what steps to take to resolve. But for those times when things just seem out of sync, you can try running this sequence of Magento CLI commands as a "reset" script of sorts:
- Custom theme styles aren't showing up on the front-end
- Modes: If you are doing some custom styles and development, the first thing you'll want to do is enable "developer mode." This will help with the caching settings and make your life a little easier for adding those custom features. Once you set developer mode, make sure to clear the cache. You can read all about Magento modes and how to enable or disable them in this Magento Dev Docs: Magento Modes article.
- Static Files: Once you have developer mode enabled things should start going smoother for static files, but there will still be times where your new code isn't showing up on the front-end even if you have developer mode enabled. I've found a good fix to make sure you have the most current and up to date files for your theme is to clear the pub/static directory. This directory is where Magento compiles your static resources to load the front end. You can clear all the contents of pub/static besides the .htaccess file, you NEED this file for the page to load. Once you clear the other contents of pub/static, clear your Magento cache and reload the page!
- My template or layout file updates aren't showing on the front-end
- When you make edits to any ".phtml" or ".xml" file, you have to clear your Magento cache for the updates to appear. It is a very tedious and annoying tidbit about Magento along with other heavily cached systems like Drupal, but its the reality of the system. So be sure to always check your cache if things aren't showing up as you'd expect.
- I would HIGHLY recommend looking into getting a preprocessor setup for your theme like Gulp. You can then add tasks for clearing the Magento cache using dependencies like gulp-shell to run any CLI command. This has saved our team so many headaches.
- Removing Magento sample data
- You can set up a fresh install of Magento 2 with sample data to get things moving quickly, but at some point you'll need to remove all the sample data and use your store's real data. You can read about removing sample data in this Magento Dev Docs: Removing Sample Data article here. Before you remove the sample data, I HIGHLY recommend taking a backup of your site, both files and database, as these commands can make some rather large changes.
- Once you've removed the sample data, I would recommend running the "Reset Magento" commands listed above in step 2. It will help ensure everything is synced up after a large change.
- You may also notice that your theme is no longer displaying correctly, don't panic. This has happened to us too. Just log in to your admin, go to "Content > Design: Configuration" and make sure your custom theme is still selected.