Magento 2 Certification: Customize Sales Operations

Dec 11, 2018402Andrey Matveev
Magento 2 Certification: Customize Sales Operations

Sales operations are extremely sensitive topic for online shop onwners – if you make a mistake in configurations, you may lose money and customers. That is why, in today’s article we will take a look at how to customize sales operations in Magento 2, and answer the following questions:

How to modify order processing and integrate it with a third-party ERP system
How to modify order processing flow
How to customize invoices
Refund functionality in Magento 2

How to modify order processing and integrate it with a third-party ERP system

To modify order processing, you can use a plugin or observer.

You can create a plugin for one of the functions (placeOrder or submitQuote) of the Magento\Quote\Model\QuoteManagement class or create an observer for one of the following events:

  • sales_model_service_quote_submit_before;
  • sales_model_service_quote_submit_success;
  • sales_model_service_quote_submit_failure;
  • checkout_submit_before;
  • checkout_submit_all_after.

This allows to integrate your logic into the order creation process, for example, sending data to a third-party ERP system.

How to modify order processing flow

Magento 2 has a system of statuses and states that affect processing of orders. The difference between status and state is that state is the actual position in the order processing flow, that’s why states affect the possible actions in the order processing. For example, in the processing state you can create an invoice or shipping, and the order status does not allow to affect the actions (the logic of third-party modules is an exception).

A state can have multiple statuses, which allows to describe the order process more flexibly. The status-state connection is stored in the sales_order_status_state table.

To change statuses and states programmatically, you can use the setStatus and setState methods. To add a new entry to the order history, you can use the addStatusToHistory method.

  • $order->setState(\Magento\Sales\Model\Order::STATE_PROCESSING);
  • $order->setStatus(‘processing’);
  • $order->addStatusToHistory($order->getStatus(), ‘Custom Message’);
  • $order->save().

How to customize invoices

To create an invoice, use the \Magento\Sales\Model\Service\InvoiceService class and its prepareInvoice method, which in turn uses \Magento\Sales\Model\Order\Invoice. In order to add your logic to the invoice creation process, you can use one of the following events:

  • sales_order_invoice_pay,
  • sales_order_invoice_cancel,
  • sales_order_invoice_register.

Invoice can have one of the three states:

  • STATE_OPEN
  • STATE_PAID
  • STATE_CANCELED

There are 2 types of invoice — online and offline invoice. Online invoice calls the capture method for payment, which in turn can send a request to the payment system. Offline invoice changes the payment information only at the Magento side.

Refund functionality in Magento 2

Credit Memos are responsible for the refund in Magento 2. They allow you to return the whole order or a part of it. Refund can also be offline and online (depending on the order type). The difference between online and offline refunds is that offline refund is executed on the Magento side and does not send any requests to the payment system, while online refund sends a request to the payment system.

Here is an example of the _void method in \Magento\Sales\Model\Order\Payment:

This is all to the question of sales operation customization. If you have questions or comments, feel free to leave them down below.

 



Post a new comment

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