Magento 2 Cookbook - Sample Chapter
Magento 2 Cookbook - Sample Chapter
Magento 2 Cookbook - Sample Chapter
ee
Magento 2 Cookbook
This merchant and developer guide is packed with recipes that cover all aspects of Magento 2.
The recipes start with simple how-to's then delve into more advanced topics as the book progresses.
By the end of this book, you'll be competent with all the development phases of Magento 2 and its
most common elements.
or NGINX
Transfer your Magento 1 database to
Magento 2 Cookbook
Magento 2 is an open source e-commerce platform that has the functionality to function from small to
large online stores.
Sa
pl
e
frontend apps
and problems
real-world problems
Magento 2
$ 44.99 US
28.99 UK
Ray Bogman
Vladimir Kerkhoff
problems efficiently
Magento 2 Cookbook
Over 50 practical recipes that will help you realize the full potential
of Magento in order to build a professional online store
Foreword by Guido Jansen, Magento Community Manager
P U B L I S H I N G
P U B L I S H I N G
Ray Bogman
Vladimir Kerkhoff
Preface
Magento is one of the most successful open source e-commerce platforms out there today.
It is powerful enough to build small or enterprise businesses.
The first stable version of Magento was released in 2008. Since the beginning, lots of
merchants have chosen Magento to be their platform with great success.
The current global ecosystem of Magento includes 240+ merchants, 300+ partners, and
4500+ certified developers, and this number is growing on a daily basis.
Over the last few years, e-commerce has changed a lot, so Magento announced a new
platform that will support this. This new era of commerce is called Magento 2.
Magento 2 offers enhanced performance and scalability. The new platform is built from
scratch to enable lots of new features. It supports both B2C and B2B businesses, and will
serve the best omnichannel shopping experiences to their shoppers.
This book will provide you with the necessary insights to get a better understanding on what is
needed to build such a powerful commerce platform.
The book is divided into several recipes, which show you which steps to take to complete a
specific action. In each recipe, we have a section that explains how everything works.
It will cover installing a Magento 2 website, configuring your categories and products,
performance tuning, creating a theme, developing a module, and much more.
At the end of this book, you will gain the knowledge to start building a success website.
Preface
Chapter 2, Magento 2 System Tools, explains how to install Magento 2 via the command
shell. Magento released a new powerful tool to manage and install sample data, reindex
your database, back up your site, or flush your caches, which are just a few of the options.
Chapter 3, Enabling Performance in Magento 2, explains how to configure different types of
caching options. In Magento 2, the Full Page Cache (FPC) can be handled by Varnish to give
your store a performance boost. There are also external services that you can use as a cache.
Chapter 4, Creating Catalogs and Categories, shows you one of the major elements of a
Magento store before creating products. Creating the correct product type including attributes
is an important step in setting up a Magento store.
Chapter 5, Managing Your Store, covers setting up the correct tax rules, configuring an
inventory, and creating customer groups.
Chapter 6, Creating a Magento 2 Theme, discusses the Magento 2 blank theme and how to
use the fallback to create seasonal variations. It also explains how the new theme is set up
and where files are stored.
Chapter 7, Creating Magento 2 Extensions the Basics, contains the basic functions required
to use extensions in a Magento 2 installation. It contains a brief introduction to new methods
introduced in the Magento 2 framework and examples on how to create basic functions.
Chapter 8, Creating Magento 2 Extensions Advanced, explains how to use advanced
features in extensions for Magento 2. It also includes how to add unit/functional tests
as this is a new requirement for extensions listed on the new Magento Connect.
Magento 2
System Tools
In this chapter, we will cover the basic tasks related to managing the system tools of
Magento 2. You will learn the following recipes:
Introduction
This chapter explains how to install and manage Magento 2 on a production-like environment.
We will be installing a new Magento 2 instance via the shell command with and without
sample data. Besides the setup, managing Magento 2 is different from the current Magento
version. We will be using a lot of tools from the command line so basic shell knowledge is
advised. The command-line tool in the /bin directory is similar to the current Swiss army
knife tool in the current Magento version known as n98-magerun.
Using bin/magento and Composer is one of the new key features in Magento 2 that will rock
your world.
43
Usage:
command [options] [arguments]
Options:
--help (-h)
--quiet (-q)
Description
list
admin
admin:user:create
admin:user:unlock
cache
cache:clean
cache:disable
cache:enable
cache:flush
cache:status
catalog
catalog:images:resize
44
Chapter 2
Commands
Description
cron
cron:run
customer
customer:hash:upgrade
deploy
deploy:mode:set
deploy:mode:show
dev
dev:source-theme:deploy
dev:tests:run
dev:urn-catalog:generate
dev:xml:convert
i18n
i18n:collect-phrases
i18n:pack
i18n:uninstall
indexer
indexer:info
indexer:reindex
indexer:set-mode
indexer:show-mode
indexer:status
maintenance
maintenance:allow-ips
maintenance:disable
maintenance:enable
maintenance:status
module
module:disable
module:enable
module:status
module:uninstall
sampledata
sampledata:deploy
Description
This removes all sample data from composer.
json
This resets sample data modules for reinstallation
theme
theme:uninstall
info
info:adminuri
info:backups:list
info:currency:list
info:dependencies:showframework
info:dependencies:showmodules
info:dependencies:showmodules-circular
info:language:list
info:timezone:list
setup
setup:backup
setup:config:set
setup:cron:run
setup:db-data:upgrade
setup:db-schema:upgrade
setup:db:status
setup:di:compile
This generates the DI configuration and all nonexisting interceptors and factories
setup:di:compile-multitenant
setup:install
setup:performance:generatefixtures
setup:rollback
46
Chapter 2
Commands
setup:static-content:deploy
Description
setup:store-config:set
setup:uninstall
setup:upgrade
Throughout this chapter, you can pick your own preferred hosting setup as we set up in
Chapter 1, Installing Magento 2 on Apache and NGINX. We will be using an NGINX-based
setup. The Apache setup is pretty straightforward; when needed, we will address specified
configuration settings when they occur.
with sample data, and later, I will show you how to install it at the end in case you already
have a preinstalled version.
Getting ready
For this recipe, we will use a Droplet created in Chapter 1, Installing Magento 2 on Apache
and NGINX, at DigitalOcean, https://www.digitalocean.com/. We will be using NGINX,
PHP-FPM, and a Composer-based setup. No other prerequisites are required.
How to do it...
For the first step, you can either create a new Droplet or rebuild a clean Droplet based on a
Ubuntu or RedHat DigitalOcean image.
47
The steps to install Magento 2 sample data via GUI are as follows:
1. Preferably, pick the new snapshot or the already created one and press Rebuild from
Image. The rebuild will take around 60 seconds.
2. Now log in to your new or current Droplet. We will be referring to a new build Droplet
throughout this recipe.
48
Chapter 2
3. Now let's download the latest Magento 2 version including sample data. Go to
https://www.magentocommerce.com/download, pick Full Release with
Sample Data (ZIP with sample data), and unpack this in your web directory.
We refer to /var/www/html in this recipe.
If you are using a ZIP package, make sure that you install the unzip package first,
running the following command on the shell:
apt-get install unzip
find . -type d -exec chmod 770 {} \; && find . -type f -exec chmod
660 {} \; && chmod u+x bin/magento
5. Now we use Composer to resolve all of our dependencies. Run the following
command from the shell:
cd /var/www/html/ && composer install
During the installation process, you will get a notice to create a GitHub OAuth token.
The download rate limit is pretty small. Copy the URL from your shell window in your
browser, log in at GitHub or create an account, and create the token. You may also
check Chapter 1, Installing Magento 2 on Apache and NGINX, for more details on
this topic.
6. Next, we will be using the setup wizard to continue the rest of the installation. In this
chapter, we will be using the shell installation method. Go to your favorite browser
and enter the following URL:
http://yourdomain.com/setup
Continue your flow until Step 4: Customize Your Store. As we have chosen a
Magento 2 package including sample data, all software modules are preinstalled
and listed in the advanced modules configurations list.
49
50
Chapter 2
7.
Now, continue the rest of the steps and install Magento 2. Installing sample data can
take some time, so don't close or refresh your browser.
The progress bar and console log will share all details regarding the current status:
Congratulations, you just finished the installation of Magento 2 including sample data. Now
go to your browser using yourdomain.com; you will see the default Magento 2 layout theme
called Luma, as follows:
51
How it works
Let's recap and find out what we did throughout this recipe. In steps 1 through 6, we created
a Magento 2 version including sample data. The process is almost the same as we did in the
previous chapter. The most important change is that we downloaded a full version including
sample data. In this build, Magento submitted all of the media files and data sample packages
that we need to complete the process. During the graphical setup, we were able to choose
which sample data packages are needed. This process could take some time to complete.
There's more
In the /var directory, you can find the following hidden file with the current state of the
installed sample data:
less /var/www/html/var/.sample-data-state.flag
Want to start all over again? Magento 2 has a magic uninstall option that cleans everything
on the fly, cache, and database. You can use the php bin/magento setup:uninstall
command on the shell, as shown here:
root@mage2cookbook:/var/www/html# php bin/magento setup:uninstall
Are you sure you want to uninstall Magento?[y/N]y
Starting Magento uninstallation:
Cache cleared successfully
Cleaning up database `magento2`
File system cleanup:
/var/www/html/pub/static/_requirejs
/var/www/html/pub/static/adminhtml
/var/www/html/pub/static/frontend
/var/www/html/var/cache
/var/www/html/var/composer_home
/var/www/html/var/di
/var/www/html/var/generation
/var/www/html/var/log
/var/www/html/var/page_cache
/var/www/html/var/tmp
/var/www/html/var/view_preprocessed
/var/www/html/app/etc/config.php
/var/www/html/app/etc/env.php
52
Chapter 2
It was very easy to script and use multiple times on any given environment.
In Magento 2, the logic stayed the same but now, it's much easier to use. We will be using a
composer.json file for our setup prerequisites.
Getting ready
For this recipe, we will use a Droplet created in Chapter 1, Installing Magento 2 on Apache
and NGINX, at DigitalOcean, https://www.digitalocean.com/. We will be using NGINX,
PHP-FPM, and a Composer-based setup. No other prerequisites are required.
53
How to do it...
For the purpose of this recipe, let's assume that we need to create a Magento 2 hosting
environment including sample data. The following steps will guide you through this:
1. In this recipe, check your /root/.composer/auth.json file if you have a Magento
of GitHub repository token, username, and password. If not, create them. Here is an
example (the username and password are dummies):
{
"http-basic": {
"repo.magento.com": {
"username": "256e8f49b66689ecf18b07bc3cc2ca2d",
"password": "cb1c7ef2e14b666d8a4e99fe40c8393a"
}
},
"github-oauth": {
"github.com": "e960f7000803e2832ce5f7a637d58a666"
}
}
You can create a Magento authentication key in the user section of the Magento
connect portal. Go to http://www.magentocommerce.com/magentoconnect/, navigate to the Developers | Secure Keys menu item, and create one.
Public Key is your username and Private Key is your password.
The GitHub token can be created under the tokens section of your GitHub account.
Go to https://github.com/settings/tokens and press Generate new token.
Copy the token in your auth.json file of your home or root directory.
2. Now, let's create a Composer project using the shell. Go to your web server
/var/www/html directory and use the following command:
composer create-project "magento/project-community-edition" /var/
www/html --prefer-dist --repository-url https://repo.magento.com/
3. Now use the following command on the shell. This will add the sample data package
to the Magento composer.json file.
php bin/magento sampledata:deploy
You may get the following error notice; ignore this once you set up the auth.json
file:
[Composer\Downloader\TransportException]
The 'https://repo.magento.com/packages.json' URL required
authentication.
You must be using the interactive console to authenticate
54
Chapter 2
4. Run the following command on the shell. This will download all sample data to your
Magento 2 environment.
composer update
5. Make sure that you have the correct file permission before you continue:
chown -R www-data:www-data /var/www/html
The easy way to continue is to visit our setup page from the browser as we did
in the Installing Magento 2 sample data via GUI recipe of this chapter using the
http://yourdomain.com/setup URL.
6. We can also use the shell to finish the setup using the following script:
bin/magento setup:install \
--db-host=localhost \
--db-name=<your-db-name> \
--db-user="<db-user>" \
--db-password="<db-password>" \
--backend-frontname=<admin-path> \
--base-url=http://yourdomain.com/ \
--admin-lastname=<your-lastname> \
--admin-firstname=<your-firstname> \
--admin-email=<your-email> \
--admin-user=<your-admin-user> \
--admin-password=<your-password> \
Congratulations, you just finished the installation of Magento 2 including sample data. Now
go to your browser using yourdomain.com, and you will see the default Magento 2 layout
theme called Luma.
How it works
Let's recap and find out what we did throughout this recipe. In steps 1 through 6, we installed
Magento 2 via the command shell. Before we could continue, we needed to create an auth.
json file that stores our Magento and GitHub tokens. Without them, we may not be able to
install the software easily due to download restrictions or dependencies.
55
There's more
Always make sure that your system PATH is exported to your system using the following
command from the shell (where /var/www/html is your Magento root folder):
export PATH=$PATH:/var/www/html/bin
Customer Grid: This indexer rebuilds the customer's grid. This is a new indexer in
Magento 2 for optimized rendering of the adminhtml backend pages.
Category Products: This indexer creates the association between categories and
products based on the associations that you set in the backend on the categories
and relates to the Product Categories indexer. The flat catalog indexer creates a flat
optimized table in the database.
Product Categories: This indexer creates the association between products and
categories based on the associations that you set in the backend on the products
and relates to the Category Products indexer. The flat product indexer creates a flat
optimized table in the database.
56
Chapter 2
Product Price: This indexer relates to the products and their advanced pricing
options based on, for example, customer group, website, and catalog discount rules.
The indexer aggregates the data in tables (catalog_product_index_price_*)
and makes the selects (sorting and filtering) much easier.
Product EAV: This indexer reorganizes the Entity Attribute Value (EAV) product
structure to the flat structure. The product EAV indexer is related to the Category
Products and Product Categories indexer and creates a flat optimized table in the
database.
Stock: This indexer rebuilds and calculates the current stock data.
Catalog Search: This indexer rebuilds the catalog product fulltext search.
Catalog Rule Product: This indexer creates and updates the created catalog price
rule set.
Catalog Product Rule: This indexer creates and updates the created shopping cart
price rule set.
57
Getting ready
For this recipe, we will use a Droplet created in Chapter 1, Installing Magento 2 on Apache
and NGINX, at DigitalOcean, https://www.digitalocean.com/. We will be using NGINX,
PHP-FPM, and a Composer-based setup. A working Magento 2 setup is required.
How to do it...
The following are the steps to implement the recipe:
1. In Magento 2, we only have the option to change the Update on Save or Update by
Schedule state:
Update on Save: Index tables are updated immediately after the dictionary data
is changed
Update by Schedule: Index tables are updated by cron job according to the
configured schedule
2. We will be using the shell during this. This will be the only way to update your indexes.
Go to your shell and run the following command from your web server directory:
php bin/magento indexer:info
Using the mode option realtime (Update on Save) or schedule (Update by Schedule)
may set the indexer.
6. One of the most commonly used commands is as follows:
php bin/magento indexer:reindex
This will reindex all the indexers. However, you can also reindex them individually
using the following command:
php bin/magento indexer:reindex customer_grid
58
Chapter 2
We can also use the following command:
php bin/magento indexer:reindex customer_grid catalog_category_
product etc
7.
Now you can rerun indexer:status to check whether all the indexers are up to date.
How it works
Let's recap and find out what we did throughout this recipe. In steps 1 through 7, you learned
how to use the bin/magento indexer.
In step 5, you learned how to check what the current status is of all the indexes. In step 4,
we saw how to switch from the realtime Update on Save to the schedule Update on Schedule
modes.
In step 6, you learned how to reindex them individually.
There's more
You can also check the current status of the indexer using MySQL. Run the following
command in the shell:
mysql -u <username> --database <dbname> -p -e "select * from indexer_
state"
Description
Magento collects configuration
from all modules, merges it, and
saves the merged result to the
cache. This cache also contains
store-specific settings stored in the
filesystem and database.
Clean or flush this cache type after
modifying configuration files.
59
Description
This is the compiled page layout
(that is, the layout components
from all components).
Clean or flush this cache type after
modifying layout files.
block_html
Collections data
collections
DDL
db_ddl
EAV
eav
60
Chapter 2
Cache types
Page cache
Description
This is the generated HTML pages.
If necessary, Magento cleans up
this cache automatically, but thirdparty developers can put any data
in any segment of the cache.
Clean or flush this cache type after
modifying code level that affects
HTML output. It's recommended
to keep this cache enabled
because caching HTML improves
performance significantly.
Reflection
reflection
Translations
translate
Integration
configuration
config_integration
Integration API
configuration
config_integration_
api
Web services
configuration
config_webservice
By default, we will be using Full Page Cache now in the community version, which is a great
improvement next to the web services (API) caches.
Depending on the current development, default, and production state, caches will be
different.
In the next recipes of this chapter, we will dive deeper into the use of different states.
Getting ready
When cleaning or flushing your cache, Magento will flush its content from either the var/
cache or var/full_page directory. In this recipe, we will refer to the bin/magento
cache:enable, bin/magento cache:disable, bin/magento cache:clean, or bin/
magento cache:flush options.
61
How to do it...
For the purpose of this recipe, let's assume that we need to manage the Magento 2 cache
setup. The following steps will guide you through this:
1. Let's first check the current status using the following command:
php bin/magento cache:status
2. Now we will check how to enable and disable caches individually or all at once. Use
the following command to disable the caches individually:
php bin/magento cache:disable config
This will disable the cache for only config. You may pick any cache type code name.
To enable the config cache back, use the following command:
php bin/magento cache:enable config
62
Chapter 2
When skipping the cache type code name behind the command, we will be able to
enable or disable the caches all at once. It will look like this when we disable the cache:
63
How it works
Let's recap and find out what we did throughout this recipe. In steps 1 through 5, you learned
how to manage the cache in Magento 2.
In step 1, you learned how to use the status option to check what the current cache status
is. In step 2, we were able to enable or disable the caches individually.
In steps 4 and 5, you learned how to flush and clean the caches individually.
There's more
You can also flush all cached items running the following command from the shell:
php bin/magento cache:flush -all
Keep in mind that cleaning or flushing your Full Page Cache can resolve in a cold (no-cache)
page, so warming up all pages is advised.
64
Chapter 2
When creating a backup, we can use one of the following options:
Option
--code
Meaning
--media
var/backups/<timestamp>_
filesystem_media.tgz
-db
var/backups/<timestamp>_
db.gz
Getting ready
When creating a backup, Magento will store it in the var/backups directory. In this recipe,
we will refer to the bin/magento setup:backup and bin/magento setup:rollback
options.
How to do it...
For the purpose of this recipe, let's assume that we need to manage the Magento 2 backup
setup. The following steps will guide you through this:
1. Let's start creating a code-only backup using the following command:
php bin/magento setup:backup --code
Once Magento sets the maintenance code flag, the web shop is offline for everybody.
Creating a code backup takes some time. The backup will be stored in var/
backups. A clean Magento 2 code backup is around 123 MB.
2. Now we will create a database-only backup using the following command:
php bin/magento setup:backup --db
A clean Magento 2 media backup with sample data is around 153 MB.
65
| Backup Type |
+---------------------------------+-------------+
| 1448480907_filesystem_code.tgz
| 1448481232_db.gz
| db
| code
| 1448481295_filesystem_media.tgz | media
+---------------------------------+-------------+
5. Rolling back a backup is very easy. You can use the following command on the shell:
php bin/magento setup:rollback [-c|--code-file="<name>"] [-m|-media-file="<name>"] [-d|--db-file="<name>"]
For example, to restore a database backup, we can use the following command:
php bin/magento setup:rollback -d 1448481232_db.gz
While confirming the action, you could get a Segmentation fault. You
can fix this using the following command. This could be related to PHP 7.
Always use the latest version:
ulimit -s 65536
You can also store this in the .bashrc file on your system.
6. Congratulations, you just rolled back a backup. Pick any type to do the same. Always
flush and clean your cache once you are done.
7.
Setting up a scheduled backup schema is a whole different ball game. We first need
to set up a cron job using the following command:
crontab -e
*/1 * * * * php /var/www/html/bin/magento cron:run
66
Chapter 2
8. Creating a daily, weekly, or monthly backup can be done in the administrator
backend. Log in and navigate to Stores | Configuration | Advanced | System |
Scheduled Backup Settings:
How it works
Let's recap and find out what we did throughout this recipe. In steps 1 through 8, you learned
how to create rollbacks and manage backups via the command line.
In step 1, we created a backup for our code base only. In step 2, we created a backup for the
database and in step 3, for all the media files.
In step 4, we listed all the created backups that are located in var/backups.
The process in step 5 is related to the rollback scenario. Depending on the backup size, rolling
back could take some time.
In steps 7 and 8, we configured a cron job to schedule our daily backup process automatically.
There's more
You can also check the current status of the cron schedule using MySQL. Run the following
command from the shell:
mysql -u <username> --database <dbname> -p -e "select * from cron_
schedule"
67
developer
production
Description
When no given mode is given, this is explicitly set and has the following
benefits:
Verbose logging
68
Chapter 2
Getting ready
Always check what web server you are using; the settings for Apache and NGINX are not the
same. In this recipe, we will be showing you how to set this on both of them.
How to do it...
For the purpose of this recipe, let's assume that we need to manage the Magento 2
production or development setup. The following steps will guide you through this:
1. In this recipe, the setup of the MAGE_SET option is different for NGINX and Apache. In
Apache, we can use either the .htaccess file or configure this in the vhost file. We will
first look into the Apache setup. While all recipes of this chapter are based on NGINX,
it's best to skip this part and continue to the next listed topic or retrieve an DigitalOcean
Droplet that we had set up in Chapter 1, Installing Magento 2 on Apache and NGINX.
Go to the .htaccess file in your web root directory and remove the # (hash or pound
sign) at the fifth line from the top:
SetEnv MAGE_MODE developer
If you are using a server-based configuration instead of the .htaccess file, use the
following then:
SetEnv MAGE_MODE "developer"
By default, it shows the default status. We now switch the status using the following:
php bin/magento deploy:mode:set production
This will trigger the maintenance mode and start creating all necessary optimized
static files needed.
4. We can also run this manually. However, first we will need to create static files in the
pub/static directory. Run the following command on the shell:
php bin/magento setup:static-content:deploy
5. If you want to skip the code compilation, use the --skip-compilation option, as
shown in the following command:
php bin/magento deploy:mode:set developer --skip-compilation
6. Remember to check your permissions and ownership of the newly created files.
Code compilation consists of caches, optimized code, optimized
dependency injection, proxies, and so on. Magento 2 needs these
files to serve an optimized code base to the client's browser.
How it works
Let's recap and find out what we did throughout this recipe. In steps 1 through 6, you learned
configuring the development and production modes in Magento 2.
70
Chapter 2
In step 1, we configured the SetEnv parameter in the .htaccess file of Apache to set the
correct mode. There is also an option to configure this in the Apache configuration file instead.
In step 2, we configured the set $MAGE_MODE parameter in NGINX to use the correct mode.
In step 3, we used the bin/magento deploy option to tell Magento to start using the
selected mode in NGINX or Apache, and create additional static files when running in
production mode or show the correct debug headers in the developer mode.
In step 4, you learned how to deploy static content in the pub/static directory when running
in production mode. This option will trigger the whole process of merging and compiling the
correct code in the public folder.
There's more
Use curl to check your HTTP response header to see what current state you are running,
as shown in the following:
curl -I http://mage2cookbook.com/
HTTP/1.1 200 OK
Server: nginx/1.9.6
X-Magento-Cache-Debug: HIT
Always check the current status in your HTTP header and Magento shell. Only setting the
web server configuration will not automatically trigger the Magento configuration and can
mislead you.
71
Getting ready
Before we can start migrating our system, we need to check the following:
Always back up your databases and old and new Magento versions.
Only use the exact version number, so that the data-migration-tool 2.0.0 corresponds
with Magento 2.0.0.
You may copy your current production database to the
new Magento 2 server and run it there.
A migration of Magento 1 to Magento 2 has the following five phases that are important to
follow in the correct order:
1. Settings: Migration of the settings is step 1. This will transfer all information from the
stores, website, and system configuration.
The command is php bin/magento migrate:settings.
2. Data: Migration of the data is step 2. This will transfer all categories, products,
customers, orders, wishlists, ratings, and so on
The command is php bin/magento migrate:data.
3. Delta: Migration of the delta is step 3. This is an important step and is used to
transfer Magento 1 data to Magento 2 where new updates occur. It will update the
most recent data of customers, orders, or other customer-related data. It is common
to use this command before going live.
The command is php bin/magento migrate:delta.
4. Media: Migration of the media files is easy; just copy all files from /media to
/pub/media.
72
Chapter 2
5. Custom modules/themes: Migration of your modules or themes is out of the scope
of the migration tool. Contact your solutions provider to check whether they have
a new version available. This also applies to any custom-made themes of theme
packages bought online.
How to do it...
For the purpose of this recipe, let's assume that we need to manage a Magento 1 to
Magento 2 migration setup. The following steps will guide you through this:
1. First, we need to run the following command to add data-migration-tool to your
current Composer setup:
composer config repositories.data-migration-tool git https://
github.com/magento/data-migration-tool-ce
composer require magento/data-migration-tool:dev-master
Description
migrate
migrate:data
migrate:delta
migrate:settings
Go to /var/www/html/vendor/magento/data-migration-tool/etc/ceto-ce and pick the correct database version mentioned in the directory. If correct,
you will see two files called config.xml.dist and map.xml.dist.
5. Copy config.xml.dist to config.xml using the cp command:
cp config.xml.dist config.xml
73
If you are using a custom prefix in your database or you wish to use your encryption
key on your Magento 2 setup, you can add this to the <options> section, as shown
in the previous code.
7.
Now we can start step 1 of the settings migration using the following command:
php bin/magento migrate:settings /var/www/magento2/vendor/magento/
data-migration-tool/etc/ce-to-ce/1.9.2.2/config.xml
As you can see, here we are using the 1.9.2.2 version. Depending on your version,
you may change this before running the command.
The output result looks like this:
[2015-12-02 20:28:56][INFO][mode: settings][stage: integrity
check][step: Settings Step]: started
100% [============================] Remaining Time: 1 sec
[2015-12-02 20:28:56][INFO][mode: settings][stage: integrity
check][step: Stores Step]: started
100% [============================] Remaining Time: 1 sec
[2015-12-02 20:28:56][INFO][mode: settings][stage: data migration]
[step: Settings Step]: started
100% [============================] Remaining Time: 1 sec
[2015-12-02 20:28:59][INFO][mode: settings][stage: data migration]
[step: Stores Step]: started
100% [============================] Remaining Time: 1 sec
[2015-12-02 20:28:59][INFO][mode: settings][stage: volume check]
[step: Stores Step]: started
100% [============================] Remaining Time: 1 sec
[2015-12-02 20:28:59][INFO][mode: settings][stage: volume check]
[step: Stores Step]: Migration completed
74
Chapter 2
8. You can check your Magento 2 system configuration backend if all updated settings
are available. If so, you can continue.
9. If step 1 is correct, we continue to migrate our data to Magento 2 using the following
command:
php bin/magento migrate:data /var/www/magento2/vendor/magento/
data-migration-tool/etc/ce-to-ce/1.9.2.2/config.xml
10. You can check your Magento 2 catalogs, products, orders, and customers if they are
all updated. If so, you can continue.
75
12. Now check the frontend and backend whether your data is available in Magento 2. If
not, check the migration.log file located in /var.
13. In this recipe, we used a default Magento 1.9.2.2 setup. After the settings and data
migration, we created a sales order in Magento 1. Now, using the delta option, we
push the data to Magento 2 using the following command:
php bin/magento migrate:delta /var/www/magento2/vendor/magento/
data-migration-tool/etc/ce-to-ce/1.9.2.2/config.xml
14. Now check your sales and customer data. Congratulations, you successfully migrated
your database from Magento 1 to Magento 2.
How it works
Let's recap and find out what we did throughout this recipe. In steps 1 through 14, you learned
how to use the Magento 2 migration tool.
In step 1, we used Composer to add an additional repository for data migration. After installing
all of the packages, they are available in the bin/magento tool. In this setup example, we
used a clean Magento 1.9.x database.
In step 4, we made sure to run on a clean Magento 2 setup. Depending on your setup, go to
vendor/magento/data-migration-tool/etc and select the correct version. Magento
2 supports the migration option for CE and EE. Once we configured the config.xml file with
the Magento 1 database information in step 6, we were ready to go.
76
Chapter 2
In step 7, we used the bin/magento migration setting to start the whole process. We started
with the setting parameter and continued with the data and delta parameters in steps
7 through 13. We must not forget the reindexing and updating of our caches before using
them. The delta parameter option can be run multiple times as it only updates the latest
information, which is helpful before going live and switching to production.
There's more
As every Magento setup is unique, migrating from Magento 1 to Magento 2 can be hard
sometimes. In some situations, you may need to change your tables in the mapping
configuration located in vendor/magento/data-migration-tool/etc/ce-toce/<version>map.xml.dist.
Resetting your setting, data, and delta migration is easy using the [-r|--reset]
parameter in your command. This allows you to rerun all migration scripts from the beginning.
Always check for the currently supported versions on the Magento GitHub Data Migration Tool
page at the following link:
https://github.com/magento/data-migration-tool-ce
There is also an alternative Data Migration Tool available
by UberTheme at https://github.com/ubertheme/
magento2_data_migration.
77
www.PacktPub.com
Stay Connected: