Skip to content

syedaunabbas/magento2-order-async

Repository files navigation

About

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.

Demo Screenshots

  • 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

Order Success Page

Order add in queue

  • Orders information in the Message Queue should be periodically processed and transmitted to the ERP

Order transmit to ERP

  • Orders that are successfully transmitted to the ERP must have their status changed from “new” to “processing”

Order status updated

  • Every transmission attempt must be logged to a database table. The following information must be recorded: order ID, timestamp, return code from the ERP

Custom Log table updated

Mock API Data

Mock API Data

Mock API Schema

  • 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

Custom Log table default properties

Note: Transmission log table generated by db_schema.xml.

  • 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!

Trasmission log via URL

  • 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 Transmission CLI Command

ERP Transmission CLI Success Case

ERP Transmission CLI Error Case

Code Documentation

ERP Order fullfillment Module core business logic persists in magento2/app/code/Custom vendor folder.

Code Business Logic

Asynchronous Orders Processing

  • Plugin/OrderManagement.php is our after plugin for the method OrderManagementInterface::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 to processing
  • Queue configuration files

    • etc/communication.xml to configure our topic
    • etc/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 to db
    • 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 defines erp_transmission_log

ERP Transmission Model,ResourceModel & Collection

  • Model/ERPTransmissionLog.php
  • Model/ResourceModel/ERPTransmissionLog.php
  • Model/ResourceModel/ERPTransmissionLog/Collection.php

AsyncOrderFullFillment Helper

  • Helper/AsyncOrderFullFillment.php contains the helper functions like
    • Update order status on successfull transmission
    • Call ERP Transmit Mock Service function
    • Get Transmission Log collection

ERP Mock Service

  • ERPMockAPIService.php use to communicate external system (ERP)

Run Application via Docker

Requirements

Setup

  • Clone Repository

Start all docker containers

docker-compose up -d

Configurations via CLI

  • 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.

    Apache Local IP

    • Set IP with domain magento2.docker in /etc/hosts file via sudo /etc/hosts

Run Application

  • 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

    DB Local IP

    • Host: IPADDRESS

    • Database: magento2

    • Username: magento2

    • Password: magento2

    • Port: 3306

    • Place an order and view the transmission logs in erp_transmission_log table

    Log Table

    • Before hitting the Place Order button you can also open the system.log or debug.log file in CLI to view the queue information logging into log files. tail -f var/log/debug.log or tail -f var/log/system.log in docker container.

    Log Table

  • 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 run bin/magento erp:tranmission --success=1 for sucessfull case and bin/magento erp:tranmission --success=0 for failed case

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published