Table of content

Magento 2 Certified Professional Developer Guide


Section 9: Sales Operations

9.1 Demonstrate ability to customize sales operations

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

To modify the process of order processing, use

plugin(https://belvg.com/blog/designing-complex-solutions-using-plugins-in-magento-2.html) or observer.

You can create a plugin for either placeOrder or submitQuote function from Magento\Quote\Model\QuoteManagement class or create an observer for each of the 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 a custom logic into the order creation process (for example, sending the data to the third-party ERP system side).

Describe how to modify order processing flow. How would you add new states and statuses for an order? How do you change the behavior of existing states and statuses?

Magento 2 has a system of states and statuses that influences the order processing. The difference between a status and a state lies in the following. State is the actual position in order processing flow, so state influences the possible actions during the order processing. For example, you can create an invoice or ship during the state processing, and the order status does not have an effect on any of the actions (with the exception of third-party modules’ logic).

State can have several statuses which allows to describe the order process more flexibly. The connection between state and status is stored in sales_order_status_state table.

To modify states or statuses programmatically, use setStatus and setState methods. To add a new record into the order history, apply addStatusToHistory method.

$order->setState(\Magento\Sales\Model\Order::STATE_PROCESSING);

$order->setStatus('processing');

$order->addStatusToHistory($order->getStatus(), 'Custom Message');

$order->save();

You can create a new status via the admin panel (navigate to Stores -> Order Status) or via setup script (\Magento\Sales\Setup\Patch\Data\InstallOrderStatusesAndInitialSalesConfig class). Adding a new state is possible only via setup script.

Working with an order depending on its state is hardcoded in \Magento\Sales\Model\Order class. To modify their behavior, create plugins for this class methods and, if possible, other methods in Magento_Sales module.

Described how to customize invoices. How would you customize invoice generation, capturing, and management?

To create an Invoice, use \Magento\Sales\Model\Service\InvoiceService class and its method prepareInvoice that utilizes \Magento\Sales\Model\Order\Invoice. If you need to introduce a custom logic into the invoice creating process, use one of the following events: sales_order_invoice_pay, sales_order_invoice_cancel, sales_order_invoice_register or create a plugin for \Magento\Sales\Model\Service\InvoiceService::prepareInvoice method.

Invoice can have one of the following states:

  • STATE_OPEN
  • STATE_PAID
  • STATE_CANCELED

It can also have two types – online invoice and offline invoice.
Online invoice calls the capture method for payment method, which, in its turn, sends a query to the payment system. Offline invoice modifies the payment information only on Magento side.

Describe refund functionality in Magento. Which refund types are available, and how are they used?

Credit Memo is responsible in Magento 2 for the refund, allowing to return the order partially or completely. Also, refund can be offline and online (depending on the order type). The difference between offline and online lies in the following: offline refund is performed on Magento side and does not send any requests to the payment processing system, while online refund sends a query to the payment system.

Example of _void method in \Magento\Sales\Model\Order\Payment :

 protected function _void($isOnline, $amount = null, $gatewayCallback = 'void')
    {
        $order = $this->getOrder();
        $authTransaction = $this->getAuthorizationTransaction();
        $this->setTransactionId(
            $this->transactionManager->generateTransactionId($this, Transaction::TYPE_VOID, $authTransaction)
        );
        $this->setShouldCloseParentTransaction(true);

        // attempt to void
        if ($isOnline) {
            $method = $this->getMethodInstance();
            $method->setStore($order->getStoreId());
            $method->{$gatewayCallback}($this);
        }

Magento 2 Enterprise Edition also allows to perform the refund into Store Credits, with which a customer can later pay for other items in this store.

pm-tatiana
Tatiana-Buchkova

Partner With Us

Let us know more about your project. Contact us and let’s grow your business.

Get started