How to get order data by order id programmatically in magento 2?

Magento  2 Fetch order details by Order ID. With the help of Magento\Sales\Api\OrderRepositoryInterface, You can retrieve order-related data programmatically.

Order Entity generated after the Customer Placed Order successfully from the Storefront or Admin Can Place an order from the Magento Back Panel.

Get Order Data By Order Id to retrieve order information using the OrderRepositoryInterface way. Continue reading “How to get order data by order id programmatically in magento 2?”

How to Get Payment Method Title/Code from order in Magento 2?

You can get the payment method title from the Order in Magento 2 by the order repository interface.

Load an Order by API OrderRepositoryInterface with Order ID, Get Payment Object, and fetch Payment-related stuff from the Payment Data.

You can get the payment method title of an order by below code snippets below,

<?php
namespace Rbj\Order\Model;

use Magento\Sales\Api\OrderRepositoryInterface;

class Data
{
    public function __construct(
        private readonly OrderRepositoryInterface $orderRepository
    ) {
    }

    public function getPaymentData()
    {
        $orderId = 1;
        $order = $this->orderRepository->get($orderId);
        $payment = $order->getPayment();
        $method = $payment->getMethodInstance();
        echo $method->getTitle(); // Cash On Delivery
        echo $method->getCode(); // cashondelivery
    }

}

Using the above method you can get any payment method title or code.

How to override magento 2 order/items/renderer/default.phtml?

We can override default.phtml file using below way,
Create sales_order_item_renderers.xml in your module layout folder,
File path will be in your module,
app/code/Rbj/Training/view/frontend/layout/sales_order_item_renderers.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="sales.order.items.renderers">
            <block class="Magento\Sales\Block\Order\Item\Renderer\DefaultRenderer"
                   name="sales.order.items.renderer.default.configurable" as="configurable"
                   template="Rbj_Training::order/items/renderer/default.phtml"/>
            <block class="Magento\Sales\Block\Order\Item\Renderer\DefaultRenderer"
                   name="sales.order.items.renderer.default.simple" as="simple"
                   template="Rbj_Training::order/items/renderer/default.phtml"/>
            <block class="Magento\Downloadable\Block\Sales\Order\Item\Renderer\Downloadable"
                   name="sales.order.items.renderer.downloadable" as="downloadable"
                   template="Rbj_Training::order/items/renderer/default.phtml"/>
            <block class="Magento\Bundle\Block\Sales\Order\Items\Renderer"
                   name="sales.order.items.renderers.bundle" as="bundle"
                   template="Rbj_Training::order/items/renderer/bundle/renderer.phtml"/>
            <block class="Magento\GroupedProduct\Block\Order\Item\Renderer\Grouped"
                   name="sales.order.items.renderers.grouped" as="grouped"
                   template="Rbj_Training::order/items/renderer/default.phtml"/>
        </referenceBlock>
    </body>
</page>

Replace Rbj_Training with your actual module name.

Clear Cache and check your file will be overridden.