How to Get discount amount of an Order in Magento 2?

How to Get Final discount amount of an Order in Magento 2?

An Order has a discount amount field used to store the discount amount value in the sales_order table for the specific order.

discount_amount field used to store final discount value applied to order level.

If the Customer has a discount code and they used to code for place an order time, the amount of discount will be saved to the sales order table with a discount_amount field to track discount value in the future for the order.

By Default, Value will be saved as a negative value in the sales_order table.

if the order has a total amount of discount is $50, the value will be saved in the table like, -50.0000 (Negative number) value.

You can get the discount amount value by order increment id using below code,

<?php
namespace Jesadiya\Order\Block;

use Exception;
use Psr\Log\LoggerInterface;
use Magento\Framework\View\Element\Template;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Framework\View\Element\Template\Context;

class OrderDiscount extends Template
{
    /**
     * @var SearchCriteriaBuilder
     */
    protected $searchCriteriaBuilder;

    /**
     * @var OrderRepositoryInterface
     */
    private $orderRepository;

    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * @var array
     */
    private $data;

    /**
     * @param Context $context
     * @param SearchCriteriaBuilder $searchCriteriaBuilder
     * @param LoggerInterface $logger
     * @param OrderRepositoryInterface $orderRepository
     * @param array $data
     */
    public function __construct(
        Context $context,
        SearchCriteriaBuilder $searchCriteriaBuilder,
        OrderRepositoryInterface $orderRepository,
        LoggerInterface $logger,
        array $data = []
    ) {
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->orderRepository = $orderRepository;
        $this->logger = $logger;
        parent::__construct($context,$data);
    }

    public function getDiscountAmountOrder()
    {
        $orderDiscountAmount = null;
        try {
            $incrementId = '3000007351';
            $searchCriteria = $this->searchCriteriaBuilder
                ->addFilter('increment_id', $incrementId)->create();
            $orderData = $this->orderRepository->getList($searchCriteria)->getItems();
            foreach ($orderData as $order) {
                if ($order->getDiscountAmount()) {
                    $orderDiscountAmount = abs($order->getDiscountAmount());
                }
            }
        } catch (Exception $exception) {
            $this->logger->error($exception->getMessage());
        }

        return $orderDiscountAmount;
    }
}

If the Order has a discount available, the Result will be displayed as the discount amount. We have used abs() function to convert negative to a positive value for a discount amount.

Output:
The final discount value is the float type and if a discount is not the available result will be null.

How to get Order Discount using the Order Entity Id value instead of Order Increment id?
If You have order entity id available, You don’t need to use search criteria builder to fetch order records, Using get($orderId) method of the Order Repository interface is the simplest way to fetch order record and retrieve the value of getDiscountAmount() from the order.