Common Magento 2 Issues and How To Fix Them

Magento 2 issues

Magento 2 has been released almost 2 years ago. It went through numerous improvements since then and became a fitting successor of Magento 1. Despite this, there’s still a number of issues and questions you may face while working with Magento 2. Here are the most common ones:


1. How to install Magento 2

2. How to change to developer mode in Magento 2

3. How to change the backend language in Magento 2

4. How to enable or disable a module in Magento 2

5. How to update Magento 2

6. How to install sample data in Magento 2 using Composer

7. Add to cart is not working in Magento 2

8. How to delete product attribute in Magento 2

9. How to delete/edit footer links in Magento 2

10. How to setup cron in Magento 2

11. How to configure Magento 2 with a new domain

12. How to reset Magento 2 Admin Password

13. How to enable SEO-friendly URLs in Magento 2

14. How to create Contact Us Page in Magento 2

15. How to redirect Magento 2 to open through WWW

16. How to remove a block from layout in Magento 2

17. How to set up backend session timeout


How to install Magento 2

This question deserves the separate article. In short, there are three ways to install Magento 2:

  • From archive file
  • Via Composer
  • Via GitHub

I recommend checking the article from Inchoo and the official guide on Magento 2 installation.

How to change to developer mode in Magento 2

To switch between production and developer modes you need to use a command:

magento deploy:mode:set {mode}

where {mode} is required and can be either developer or production.

You can also add a –skip-compilation parameter to skip code compilation phase when switching to production mode. So the command would be:

magento deploy:mode:set {mode} [-s|--skip-compilation]

If you want to change from production to developer mode, you need to:

  • “Clear generated classes and Object Manager entities like proxies to prevent unexpected errors”
  • delete the content of the var/generation and var/di directories by using the command:
rm –rf <your Magento install dir>/var/di/* <your Magento install dir>/var/generation/*

Make sure to use an Asterisk in the end. Otherwise you may delete the whole directory with admin rights and .htaccess files, which may lead to future errors when generating a class.

  • use the command: magento deploy:mode:set developer

If done correctly, you’ll see the message: Switched to developer mode.

How to change the backend language in Magento 2

To change the language of admin backend, go to Account settings and select the language from the Interface Locale field.

magento 2 backend interface language

If you want to create a Store Views in multiple languages, you’ll need to download and install the Language Pack. Check this guide from Magestore for more information.

How to enable or disable a module in Magento 2

First, you need to log in to the Magento server as a user who has a permission to add changes to the Magento file system.

To enable or disable Magento 2 modules, use the following commands:

magento module:enable [-c|--clear-static-content] [-f|--force] [--all] <module-list> 
magento module:disable [-c|--clear-static-content] [-f|--force] [--all] <module-list>

Where

  • < module-list > is a list of modules to enable/disable (separated with spaces). If the module name has special characters, enclose the module name in single or double quotes.
  • –all to enable or disable all modules.
  • -f or –force to enable or disable modules despite dependencies. If you try to enable/disable a module with dependencies without using –f, the system will display a warning.
  • -c or –clear-static-content cleans generated static view files.

Failure to clear static view files might result in issues when there are multiple files with the same name and you don’t clear all of them. If you have files with the same name and don’t clear them, not including the –c attribute, may cause issues.

The simple command to disable a module looks like this:

magento module:disable Module_Name

How to update Magento 2

There are two ways to update Magento 2 version.

a) Go to Magento 2 admin panel. Navigate to System-Web Setup Wizard.

magento 2 update

Click System Configuration and enter your authentication keys.

magento 2 authentication keys

Then click System Upgrade. The system will check for the available updates and display them if any. The further process is quite straight forward. Check the detailed guide here.

b) You can update Magento 2 using Composer. To do so, use the following commands:

composer require magento/product-community-edition 2.1.6 --no-update
composer update
rm -rf var/di var/generation
php bin/magento cache:flush
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento indexer:reindex

You can change the needed version in the first line. You can use this method to update the Enterprise edition as well:

composer require magento/product-enterprise-edition 2.1.6 --no-update

Then you can check your Magento 2 version with the command:

php bin/magento --version

How to install sample data in Magento 2 using Composer

First, you need to switch to a developer mode (#2 on this list).

To install sample data, use the following command:

php <your Magento install dir>/bin/magento sampledata:deploy

If you are about to install sample data after installing Magento 2 (which is very likely), run the following command after downloading the sample data packages:

php <your Magento install dir>/bin/magento setup:upgrade

Add to cart is not working in Magento 2

There is a ton of reasons why Add to Cart button may not work in Magento 2, including conflicts with 3rd party plugins and infinite redirection loops.

One of the most common reported issues is caused by a form_key mismatch. When you click Add to Cart button, you get redirected to the checkout with no products added.

To fix this, you can replace root URL to 127.0.0.1.
UPDATE core_config_data
SET value = REPLACE(value, '<old root URL or domain>', '<new root URL or domain>')
WHERE path = 'design/head/includes';

So if you run in local use the following:

UPDATE core_config_data
SET value = REPLACE(value, 'http://localhost/', 'http://127.0.0.1/')
WHERE path = 'design/head/includes';
Then clean the cache.
php <your Magento install dir>/bin/magento cache:clean

Also, there is a quick fix for this issue. Go to vendor/magento/module-checkout/Controller/Cart/Add.php execute() method and comment the first IF condition.

//if (!$this->_formKeyValidator->validate($this->getRequest())) {
 
        //return $this->resultRedirectFactory->create()->setPath('*/*/');
 
//}

We don’t recommend this quick fix as a permanent solution.

How to delete product attribute in Magento 2

To delete product attribute, navigate to Stores – Product and select the attribute you want to delete.

delete product attribute

Now click the Delete Attribute button.

delete product attribute 2

About us and Customer Service links can be edited from Content – Blocks – footer_links_block.

edit footer links magento 2

edit footer links magento 2 2

edit footer links magento 2 3

To edit/delete other footer links, you need to edit default.xml file.

If you want to remove all footer links, add the following line after the tag:

<referenceBlock name="footer_links" remove="true">

To remove individual links, use the same code but instead of footer_links use the name of a link block.

List of names:

  • Privacy and Cookie Policy: privacy-policy-link
  • Search Terms: search-term-popular-link
  • Contact Us: contact-us-link
  • Advanced Search: catalog-search-advanced-link
  • Orders and Returns: sales-guest-form-link

To add new links, you can use the code:

<referenceBlock name="footer_links">
        <block class="Magento\Framework\View\Element\Html\Link\Current" name="your-link-name">
            <arguments>
                <argument name="label" xsi:type="string">Title</argument>
                <argument name="path" xsi:type="string">path-of-your-page</argument>
            </arguments>
        </block>
    </referenceBlock>

How to setup cron in Magento 2

To setup a cron job you must have root privileges and use the following command:

crontab -u <Magento file system owner user name> -e

e.g. crontab -u < magento_mavenecommerce > -e

Then enter the following:

*/1 * * * * * <path to php e.g. /usr/bin/php> <path to Magento installation directory>/bin/magento cron:run
*/1 * * * * * <path to php e.g. /usr/bin/php> <path to Magento installation directory>/update/cron.php
*/1 * * * * * <path to php e.g. /usr/bin/php> <path to Magento installation directory>/bin/magento setup:cron:run

After doing this (using your paths) default Magento 2 cron jobs are ready.

How to configure Magento 2 with a new domain

To change the domain name associate with your Magento 2 store, navigate to Stores-Configuration.

magento 2 change domain

Now go to General-Web, find the Base URL section and change the domain.

magento 2 change domain 2

Do the same steps for Secure Base URL.

magento 2 change domain 3

Now Save Config and flush the cache.

How to reset Magento 2 Admin Password

There are several ways to change/reset Admin Password in Magento 2.

a) Navigate to phpMyAdmin and find admin_user table in the corresponding database. Now insert the following MySQL query into SQL tab:

UPDATE admin_user SET password = CONCAT(SHA2('xxYourNewPassword', 256), ':xx:1') WHERE username = 'admin';

The “xx” characters is a cryptographic salt. These can be any numbers you like but they should be the same in both parts of the query.

b) You can easily recover your password using “Forgot your password?” functionality.

rassword recovery magento 2

c) If you have SSH access to your server, you can use Magento CLI to create a new Magento 2 admin user and then change the password of your original user. To do so, use the command:

php bin/magento  admin:user:create --admin-user="[admin]" --admin-password="[newpassword]" --admin-email="[admin@example.com]"  --admin-firstname="[example]" --admin-lastname="[example]"

How to enable SEO-friendly URLs in Magento 2

To enable SEO-friendly URLs in Magento 2 you need to follow the same process as with Magento 1. Navigate to Stores-Configuration and under the Web tab change Use Web Server Rewrites to Yes.

magento 2 seo friendly urls

How to create Contact Us Page in Magento 2

Navigate to Stores-Configuration and open a Contacts tab (under the General section). Make sure that Enable Contact Us is set to Yes.

Expand the Email Options tab and edit the fields according to your needs:

  • Send Emails To is the email address the system sends contact form submissions to.
  • Email Sender will appear as a sender of a Contact us message.
  • Email Template the name of an email template you want to use.
magento 2 contact us form

Then click Save Config.

To customize the contact form, navigate to Content-Blocks, find Contact Us Info block and select Edit.

magento 2 customize contact us form

Here you can edit the content of your Contact us page.

magento 2 customize contact us form 2

How to redirect Magento 2 to open through WWW

To make your Magento 2 store open through WWW you need to edit .htaccess file (is located in Magento 2 root). Find the RewriteEngine line and insert the following code below:

RewriteCond %{HTTP_HOST} ^yourdomain.com$ [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]

Now navigate to Stores-Configuration-General-Web and change the base URL to a WWW version and save config.

magento 2 www redirect

How to remove a block from layout in Magento 2

While in Magento 1 you’d use < remove ="block_id" />, in Magento 2 you need to use < referenceBlock name="block_name" remove="true"/>. So it will look like this:

<?xml version="1.0"?>
 
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 
    xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
 
<body>
 
    <referenceBlock name="block_name" remove="true" />
  
</body>
 
</page>

How to set up backend session timeout

Go to Stores-Configuration-General-Web, find Default Cookie Settings and change the Cookie Lifetime to whatever you want.

session timeout magento 2

Also, change this setting in php.ini – session.gc_maxlifetime and restart the server. If you don’t do this, changes in Magento 2 admin won’t take any effect.

Enjoyed this post? Spread it to your friends!

Oleg Yemchuk

Oleg Yemchuk

 

Oleg Yemchuk is a SEO Manager at MavenEcommerce sharing office space with Magento business experts in NYC and software developers worldwide. Oleg is SEO expert by day, and geek by night. Favorite pastime: traveling in TARDIS.