We have a running Adobe Commerce instance that need to communicate with an external ERP for order fulfillment reasons. With every new order, some of its details have to be asynchronously transmitted to the ERP, using the Message Queue features provided by Adobe Commerce. Order transmission must be reliable.
- On every new order, the Message Queue should receive a new message containing the order ID, the email address of the user who placed the order, and the amount of items in the cart, and the same information should be logged using the default logger
- Orders information in the Message Queue should be periodically processed and transmitted to the ERP
- Orders that are successfully transmitted to the ERP must have their status changed from “new” to “processing”
- Every transmission attempt must be logged to a database table. The following information must be recorded: order ID, timestamp, return code from the ERP
- I used Mock API for ERP syncing.
- The database table containing the transmission logs should never be empty. On table creation, a single line should be created, with order ID 0, current timestamp at creation time, and return code 999
- An html page reachable at /erp_sync/items/status should show the last 10 transmission attempts. This should not be part of the admin panel!
- There should be a CLI command that show a list of the last 10 successful transmission attempts, or the last 10 failed transmission attempts, based on an argument passed to it
ERP Order fullfillment Module core business logic persists in magento2/app/code/Custom
vendor folder.
-
Plugin/OrderManagement.php
is our after plugin for the methodOrderManagementInterface::place
, as it fires after the order has been persisted to the database and call the Queue Publisher to add order in a queue to be consumed later. -
Model/Queue/Consumer.php
is our queue handler, which containes all the business logic to:- Sync the order to ERP.
- Store Response to
erp_transmission_log
table - Update order status from
new/pending
toprocessing
-
Queue configuration files
etc/communication.xml
to configure our topicetc/queue_publisher.xml
defines our publisher, along with the connection type and exchange for the topic.etc/queue_topology.xml
is to configure our exchange, we are not using RabbitMQ so we set connection todb
etc/queue_consumer.xml
this file is responsible to process/handel the queue periodically
-
Configure declarative schema for ERP Transmission Log table and CRUD
etc/db_schema.xml
file defineserp_transmission_log
Model/ERPTransmissionLog.php
Model/ResourceModel/ERPTransmissionLog.php
Model/ResourceModel/ERPTransmissionLog/Collection.php
Helper/AsyncOrderFullFillment.php
contains the helper functions like- Update order status on successfull transmission
- Call ERP Transmit Mock Service function
- Get Transmission Log collection
ERPMockAPIService.php
use to communicate external system (ERP)
- Clone Repository
docker-compose up -d
-
docker exec -it magento2-order-async_apache_1 bash
-
composer install
-
php bin/magento setup:install \ --db-host magento2-order-async_db_1 --db-name magento2 --db-user magento2 --db-password magento2 --admin-user admin --timezone 'Europe/Rome' --currency EUR --use-rewrites 1 --cleanup-database \ --backend-frontname admin --admin-firstname AdminFirstName --admin-lastname AdminLastName --admin-email '[email protected]' --admin-password 'click123' --base-url 'http://magento2.docker/' --language en_US \ --session-save=redis --session-save-redis-host=sessions --session-save-redis-port=6379 --session-save-redis-db=0 --session-save-redis-password='' \ --cache-backend=redis --cache-backend-redis-server=cache --cache-backend-redis-port=6379 --cache-backend-redis-db=0 \ --page-cache=redis --page-cache-redis-server=cache --page-cache-redis-port=6379 --page-cache-redis-db=1 \ --search-engine=elasticsearch7 --elasticsearch-host=elasticsearch
-
Set Permissions
-
find . -type d -exec chmod 755 {} \;
-
find . -type f -exec chmod 644 {} \;
-
find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} \;
-
find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} \;
-
chown -R :www-data .
-
chmod u+x bin/magento
-
php bin/magento s:up && bin/magento s:d:c && bin/magento s:s:d -f en_US && bin/magento c:f
-
Run this command on the root of project directory not in docker container
docker inspect magento2-order-async_apache_1
get IPAddress from Networks.
- Set IP with domain
magento2.docker
in/etc/hosts
file viasudo /etc/hosts
-
-
Now try to run(http://magento2.docker/) for storefront and (http://magento2.docker/admin) for backoffice
-
Admin Username:
admin
-
Admin Password:
click123
-
To access the DB run
docker inspect magento2-order-async_db_1
and get IPAddress from Network, and connect with any MySql Client with the provided details
-
Host:
IPADDRESS
-
Database:
magento2
-
Username:
magento2
-
Password:
magento2
-
Port:
3306
-
Place an order and view the transmission logs in
erp_transmission_log
table
- Before hitting the
Place Order
button you can also open thesystem.log
ordebug.log
file in CLI to view the queue information logging into log files.tail -f var/log/debug.log
ortail -f var/log/system.log
in docker container.
-
-
Transmission attempts page URL (http://magento2.docker/erp_sync/items/status)
-
Transmission CLI command will in docker container, first you need ssh into container via
docker exec -it magento2-order-async_apache_1 bash
then runbin/magento erp:tranmission --success=1
for sucessfull case andbin/magento erp:tranmission --success=0
for failed case