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
echo $order->getIncrementId();
echo $order->getGrandTotal();
echo $order->getSubtotal();
?>
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
foreach ($order->getAllItems() as $item)
{
//fetch whole item information
print_r($item->getData());
//Or fetch specific item information
echo $item->getId();
echo $item->getProductType();
echo $item->getQtyOrdered();
echo $item->getPrice();
}
?>
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
echo $order->getPayment()->getAmountPaid();
echo $order->getPayment()->getMethod();
echo $order->getPayment()->getAdditionalInformation('method_title');
?>
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
echo $order->getCustomerId();
echo $order->getCustomerEmail();
echo $order->getCustomerFirstname();
echo $order->getCustomerLastname();
?>
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
echo $order->getBillingAddress()->getCity();
echo $order->getBillingAddress()->getRegionId();
echo $order->getBillingAddress()->getCountryId();
//fetch whole shipping information
print_r($order->getShippingAddress()->getData());
//Or fetch specific shipping information
echo $order->getShippingAddress()->getCity();
echo $order->getShippingAddress()->getRegionId();
echo $order->getShippingAddress()->getCountryId();
?>
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"?>
Create the file app/code/Vendor/Module/Block/Success.php and add the following:
<?php
namespace Vendor\Module\Block;
class Success extends \Magento\Checkout\Block\Onepage\Success {
public function getOrder() {
return $this->_checkoutSession->getLastRealOrder();
}
}
Override template
Create the file app/code/Vendor/Module/view/frontend/layout/checkout_onepage_success.xml and add the following:
<?xml version="1.0"?>
Create the file app/code/Vendor/Module/view/frontend/templates/checkout/success.phtml and add the following:
<?php /** @var $block \Vendor\Module\Block\Success */ ?>
<?php if ($block->getOrderId()):?>
<?php if ($block->getCanViewOrder()) :?>
<?php echo __('Your order number is: %1.', sprintf('%s', $block->escapeHtml($block->getViewOrderUrl()), $block->escapeHtml($block->getOrderId()))) ?>
<?php else: ?>
<?php echo __('Your order # is: %1.', $block->escapeHtml($block->getOrderId())) ?>
<?php endif; ?>
<?php echo __('You ordered %1 items.', (int) $block->getOrder()->getTotalQtyOrdered()) ?>
<?php /* @escapeNotVerified */ echo __('We\'ll email you an order confirmation with details and tracking info.') ?>
<?php endif; ?>
<?php echo $block->getAdditionalInfoHtml() ?>
getUrl() ?>"><?php /* @escapeNotVerified */ echo __('Continue Shopping') ?>
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();
to
// $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);
Now you can access the items purchased by following the code
$orderItems = $order->getAllItems();
$purchasedSkus = array();
foreach($orderItems as $orderItem) {
$purchasedSkus[] = $orderItem->getSku();
}
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.
Related Post :- How to get the items from the order into the Magento 2 success page?
With each and every specialization, Viha Digital Commerce is a most trusted ecommerce CMS development and ecommerce marketing solution company.
Post new comment
Please Register or Login to post new comment.