The Integration of Magento 2 with ERP Systems

Oct 9, 2018149Pavel Polyanovskiy
The Integration of Magento 2 with ERP Systems

In this article, we will take a look at developing the integration of Magento 2 with any external ERP system. Magento 2.2.4 compatible syntax will be used to show the examples of code. The protocol of the interaction with ERP will be REST / JSON API, a two-way data exchange.

Before starting to work on the integration, it is necessary to determine the functionality and set requirements for the system.

Let us define what we need:

  • always up-to-date data from both the website and ERP;
  • reliability of the exchange (data must be validated; in case of failure, the exchange must be repeated);
  • monitoring system — logging and notification in case of errors.

In the context of this article, we omit the explanations to the components that are not directly related to the topic at hand.

Let’s start implementing the integration of Magento 2 with ERP. The first step is to create a new module. To do this, create the following directory structure:

app/code/BelVG/ERPIntegration/

app/code/BelVG/ERPIntegration/etc

app/code/BelVG/ERPIntegration/Setup

app/code/BelVG/ERPIntegration/Helper

app/code/BelVG/ERPIntegration/Controller

app/code/BelVG/ERPIntegration/Observer

Create an app/code/BelVG/ERPIntegration/registration.php file:

Create an app/code/BelVG/ERPIntegration/etc/module.xml file:

Implement the installer of the module. It must add new attributes to entities, such as category, product, customer, order, and others that are exchanged. This attribute must be unique, as it will play the role of a common identifier for the site and ERP. We make it a string type, so as not to depend on the implementation of ERP.

Create an  app/code/BelVG/ERPIntegration/Setup/InstallData.php file (here is an example of creating an attribute for a product).

We implement the component responsible for exchanging data with ERP. It should have a closed logic of sending requests to simplify working with the code. In the example below, we will use the CURL component for REST requests to the ERP API.

To do this, create an app/code/BelVG/ERPIntegration/Helper/Requester.php file:

In order to send to ERP the data that has been created or changed on the site, you must set the capture of the save events on the entities that the Site ➜ ERP exchange is carried out.

To implement this functionality, create an observer for the product_save_after event.

Create an app/code/BelVG/ERPIntegration/Observers/ProductSaveAfterObserver.php file. It also has logging implemented in it.

Also, create an app/code/BelVG/ERPIntegration/etc/events.xml file, where we assign this observer to the desired event.

To implement a relevance check and a scheduled data exchange, you can use CRON similarly to the method of event handling.

You can also implement external API integrations for direct calls from ERP if the data has been changed on the ERP side and requires immediate synchronization. The API of both the ERP and the Magento 2 module should be closed by authorization, or in some other way (for example, by an IP filter).

It remains to implement the error notifications. Let us enable email notifications. This component is an example, it can be used in a catch block along with logging:

When completed, the module should be installed. To do this, execute the following commands in the Magento console (from the Magento root directory):

php bin/magento setup:upgrade
php bin/magento setup:di:compile

That is how you integrate Magento with ERP systems. Thank you for reading, hope everything was clear. Leave your comments and questions below and I will answer them ASAP.



Post a new comment

top
BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *