Question

I want to get the order details from

module-checkout/Block/Onepage/Success.php.

I got only the increment id to my custom template using the above block file.

How can I get the entire order details?

No correct solution

OTHER TIPS

<?php
$lid = $this->getOrderId();
echo  "Order ID:".$lid."<br/>";

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Model\Order')->load($lid);


$totall = $order->getGrandTotal();
echo "Order Total:".$totall."<br/>";

$shippingAddress = $order->getShippingAddress();

 echo "Telephone No:".$shippingAddress->getTelephone()."<br/>"; 

echo "postcode".$shippingAddress->getPostcode()."<br/>"; 

$items = $order->getAllItems();


foreach($items as $i):
   $_product = 
$objectManager->create('Magento\Catalog\Model\Product')->load($i->getProductId())->getSku();

echo "product sku:".$_product."<br/>";
endforeach;


?>

Reference link Magento 2 Get order details on success page?

The order details in the order success page can be displayed as follows,

Step 1:

Create a Block file for overriding the file Magento\Checkout\Block\Onepage\Success as follows,

<?php

namespace VendorName\ModuleName\Block\Checkout\Onepage;

use Magento\Catalog\Helper\Image;
use Magento\Catalog\Model\ProductRepository;
use Magento\Checkout\Block\Onepage\Success as mageSuccess;
use Magento\Checkout\Model\Session;
use Magento\Directory\Model\CountryFactory;
use Magento\Framework\App\Http\Context as HttpContext;
use Magento\Framework\View\Element\Template\Context;
use Magento\Sales\Model\Order\Config as OrderConfig;
use Magento\Sales\Model\OrderFactory;

/**
 * Class Success
 * @package VendorName\ModuleName\Block\Checkout\Onepage
 */
class Success extends mageSuccess
{
    /**
     * @var OrderFactory
     */
    private $orderFactory;

    /**
     * Success constructor.
     * @param Context $context
     * @param Session $checkoutSession
     * @param OrderConfig $orderConfig
     * @param HttpContext $httpContext
     * @param OrderFactory $orderFactory
     * @param array $data
     */
    public function __construct(
        Context $context,
        Session $checkoutSession,
        OrderConfig $orderConfig,
        HttpContext $httpContext,
        OrderFactory $orderFactory,
        array $data = []
    ) {
        $this->orderFactory = $orderFactory;
        parent::__construct($context, $checkoutSession, $orderConfig, $httpContext, $data);
    }

    /**
     * @return \Magento\Sales\Model\Order
     */
    public function getOrderId()
    {
        return $this->_checkoutSession->getLastRealOrder()->getIncrementId();
    }

    /**
     * @return object
     */
    public function getOrderDetails()
    {
        $incrementId = $this->_checkoutSession->getLastRealOrder()->getIncrementId();
        $order_information = $this->orderFactory->create()->loadByIncrementId($incrementId);

        return $order_information;
    }
}

Step 2:

Get the values of the order details in the success.phtml file as follows,

<div class="checkout-success">
    <div class="page-title-wrapper">
        <h1 class="page-title">
            <span class="base" data-ui-id="page-title-wrapper">Thank you</span></h1>
    </div>

    <?php if ($block->getOrderId()): ?>
    <h1 class="page-title">
        <span class="base">Thank you </span></h1>
    <p id="success_msg">An email confirmation was automatically sent to the address provided during checkout and
        subsequent alerts with tracking numbers will be sent as your item(s) are shipped.</p>
    <?php
    $_order = $block->getOrderDetails();

    /* Get Shipping address */
    $shipping_addresses = $_order->getShippingAddress()->getData();

    $address = $_order->getShippingAddress()->getStreet();
    /* Get Street address 1 and Street address 2 */
    $address1 = '';
    if (array_key_exists(1, $address)) {
        $address1 = ', ' . $address[1];
    }
    $shipping_addresses['street'] = $_order->getShippingAddress() ? $address[0] . $address1 : '&nbsp;';

    /* Get the billing address */
    $billing_addresses = $_order->getBillingAddress()->getData();
    $addressBilling = $_order->getBillingAddress()->getStreet();
    $address1Billing = '';
    /* Get Street address 1 and Street address 2 */
    if (array_key_exists(1, $addressBilling)) {
        $address1Billing = ', ' . $addressBilling[1];
    }
    $billing_addresses['street'] = $_order->getBillingAddress() ? $addressBilling[0] . $address1Billing : '&nbsp;';
    ?>
    <div class="success-detail">
        <div class="checkout-succ-order">
            <span class="order-id">Order #<?php echo $block->getOrderId(); ?></span>
            <span class="order-pring"><a href="" onclick="window.print();">Print</a></span>
        </div>
        <div class="box box-order-shipping-address">
            <strong class="box-title"><span>Shipping Address</span></strong>
            <div class="box-content">
                <?php
                if (!empty($shipping_addresses)) {
                    ?>
                    <p><?php echo $shipping_addresses['firstname'] . ' ' . $shipping_addresses['lastname']; ?></p>

                    <?php if ($shipping_addresses['company']): ?>
                        <p><?php echo $shipping_addresses['company']; ?></p>
                    <?php endif; ?>

                    <p><?php echo $shipping_addresses['street']; ?></p>
                    <p><?php echo $shipping_addresses['city']; ?>, <?php echo $shipping_addresses['region']; ?>
                        , <?php echo $shipping_addresses['postcode']; ?></p>
                    <p><?php echo 'Tel: ' . $shipping_addresses['telephone']; ?></p>
                <?php } ?>
            </div>
        </div>

        <div class="box box-order-billing-address">
            <strong class="box-title"><span>Billing Address</span></strong>
            <div class="box-content">
                <?php
                if (!empty($billing_addresses)) {
                    ?>
                    <p><?php echo $billing_addresses['firstname'] . ' ' . $billing_addresses['lastname']; ?></p>
                    <?php if ($billing_addresses['company']): ?>
                        <p><?php echo $billing_addresses['company']; ?></p>
                    <?php endif; ?>
                    <p><?php echo $billing_addresses['street']; ?></p>
                    <p><?php echo $billing_addresses['city']; ?>, <?php echo $billing_addresses['region']; ?>
                        , <?php echo $billing_addresses['postcode']; ?></p>
                    <p><?php echo 'Tel: ' . $billing_addresses['telephone']; ?></p>
                <?php } ?>
            </div>
        </div>

        <div class="box box-order-shipping-address">
            <strong><?php /* @escapeNotVerified */
                echo __('Shipping Method') ?></strong>
            <?php if ($_order->getShippingDescription()): ?>
                <p><?php echo $block->escapeHtml($_order->getShippingDescription()) ?></p>
            <?php else: ?>
                <p> <?php /* @escapeNotVerified */
                    echo __('No shipping information available'); ?></p>
            <?php endif; ?>
        </div>
        <div class="box box-order-billing-method">
            <strong class="box-title">
                <span>Payment Method</span>
            </strong>
            <div class="box-content">
                <?php endif; ?>
                <div class="box-content">
                    <?php print_r($_order->getPayment()->getMethodInstance()->getTitle()); ?>
                </div>

                <div class="order_information">
                    <?php $order_items = $_order->getAllItems();
                    ?>
                </div>
            </div>
        </div>
    </div>
</div>

Step 3:

Create di.xml file to set the preference for the class,

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Checkout\Block\Onepage\Success" type="VendorName\ModuleName\Block\Checkout\Onepage\Success"/>
</config>

Step 4:

Create the layout file to set the template created in the step 2 as follows,

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="checkout.success">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">VendorName_ModuleName::success.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>

You can use $block->getRealOrderId() to get the order id at successpage.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top