How to Display Order ID on Your Checkout Success Page?
It is important to use the checkout success page effectively for the customers. You can use tricks that help your customers to remain on the page so that you get the opportunity to increase your sales.
You can easily obtain order ID with the help of existing code
<? php $orderId = $this->getOrderId ( ); ?>
Now load order with the help of this particular order ID and fetch the data required
<? php $order = Mage:: getModel ( 'sales/order') ->load( $orderId); ?>
You can also get other details along with order ID using the code mentioned below
<? php
$orderId = Mage:: getSingleton ( 'checkout/session') ->getLastRealOrderId ( );
$order = Mage:: getSingleton ( 'sales/order') ->loadByIncrementId ( $orderId);
?>
How to fetch order details from your order ID
The code snippet mentioned below will help you obtain order details like order items, customer billing, payment and shipping details via order id.
Note: For demonstration purpose Objectmanager . Codexblog is being used which is otherwise is not recommended for direct use. You should use a particular constructor method direct an object.
<? php
$orderid = 2;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance( );
$order = $objectManager->create('Magento\Sales\Api\Data\OrderInterface')->load($orderid);
//fetch whole order information
print_r( $order->getData ( ));
//Or fetch specific information
?>
Fetch information for order items
<? php
$orderid = 2;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance( );
$order = $objectManager->create('Magento\Sales\Api\Data\OrderInterface')->load($orderid);
//Loop through each item and fetch data
{
//fetch whole item information
print_r( $item->getData ( ));
//Or fetch specific item information
}
?>
Fetch order payment details
<? php
$orderid = 2;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance( );
$order = $objectManager->create('Magento\Sales\Api\Data\OrderInterface')->load($orderid);
//fetch whole payment information
print_r( $order->getPayment ( ) ->getData ( ));
//Or fetch specific payment information
?>
Fetch order customer details
<? php
$orderid = 2;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance( );
$order = $objectManager->create('Magento\Sales\Api\Data\OrderInterface')->load($orderid);
//fetch customer information
?>
Fetch order shipping & billing details
<? php
$orderid = 2;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance( );
$order = $objectManager->create('Magento\Sales\Api\Data\OrderInterface')->load($orderid);
//fetch whole billing information
print_r( $order->getBillingAddress ( ) ->getData ( ));
//Or fetch specific billing information
//fetch whole shipping information
print_r( $order->getShippingAddress ( ) ->getData ( ));
//Or fetch specific shipping information
?>
How to show order details on success page after the order is placed
Create the file app/code/Vendor/Module/etc /di.xml and add the following:
<? xml version="1.0"? >
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi : noNamespaceSchemaLocation =".. /.. /.. /.. /.. /vendor/magento /framework/ObjectManager/etc/config. xsd ">
<preference for="Magento\Checkout\Block\Onepage\Success" type="Vendor\Module\Block\Success"/>
</config >
Create the file app/code/Vendor/Module/Block/Success.php and add the following:
<? php
}
}
Override template
Create the file app/code/Vendor/Module/view/frontend /layout/checkout_onepage_success.xml and add the following:
<? xml version="1.0"? >
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi : noNamespaceSchemaLocation ="urn: magento : framework: View/Layout/etc /page_configuration. xsd ">
<body >
<referenceBlock name="checkout. success" template="Your_Module:: checkout/success. phtml "/>
</body >
</page >
Create the file app/code/Vendor/Module/view/frontend /templates/checkout/success. phtml and add the following:
<? php /** @var $block \Vendor\Module\Block\Success */ ? >
<div class="checkout-success">
<? php if ($block->getOrderId ( )):? >
<? php if ($block->getCanViewOrder ( )) :?>
<p><? php echo __( 'Your order number is: %1.', sprintf ( '<a href ="%s" class="order-number"><strong>%s</strong></a>', $block->escapeHtml ( $block->getViewOrderUrl ( )), $block->escapeHtml ( $block->getOrderId ( )))) ? ></p>
<? php else: ?>
<p><? php echo __( 'Your order # is: <span>%1</span>.', $block->escapeHtml ( $block->getOrderId ( ))) ? ></p>
<? php endif ; ?>
<! -- BEGIN VENDOR_MODULE CUSTOM -->
<p><? php echo __( 'You ordered %1 items.', (int ) $block->getOrder ( ) ->getTotalQtyOrdered ( )) ? ></p>
<! -- END VENDOR_MODULE CUSTOM -->
<p><? php /* @escapeNotVerified */ echo __('We\'ll email you an order confirmation with details and tracking info.') ? ></p>
<? php endif ; ?>
<? php echo $block->getAdditionalInfoHtml ( ) ? >
<div class="actions-toolbar">
<div class="primary">
<a class="action primary continue " href ="<? php /* @escapeNotVerified */ echo $block->getUrl ( ) ? >"><span><?php /* @escapeNotVerified */ echo __( 'Continue Shopping') ? ></span ></a>
</div >
</div >
</div >
To refresh the checkout success page multiple times – follow the code given below
Head to file app/code/Magento/Checkout/Controller/Onepage/Success. phtml and change
$session->clearQuote ( );
// $session->clearQuote ( );
This will not clear your quote upon opening the page.
Modify your success page
Adding details to the purchased items is quite complicated so use the following code to load your order object
$orderId = Mage:: getSingleton ( 'checkout/session') ->getLastRealOrderId ( );
$order = Mage:: getModel ( 'sales/order') ->loadByIncrementId ( $orderId);
$order = Mage:
Now you can access the items purchased by following the code
$orderItems = $order->getAllItems ( );
$purchasedSkus = array( );
foreach ( $orderItems as $orderItem) {
$purchasedSkus[ ] = $orderItem->getSku ( );
}
$purchasedSkus = array
$purchasedSkus
}
Preview success page
The problem exists when you try to modify any of the details on the page and you would certainly wish to preview what the end results would be like. Well, when an order is placed and you head to the success page, refreshing the page would end the session and divert you to shopping cart empty page. In short when you change the success template you just need to create new order altogether always to preview the end result.
The above codes and snippets will help you display order ID on checkout success page and also carry out many more tasks on checkout success page .
You need to have proper knowledge of php programming so that you can make necessary changes to the code to get the final result. You can try these codes and check the final result. The codes mentioned above can help you make the changes to your checkout success page and you can easily achieve your goal.
Comments
Post a Comment