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. rnYou can easily obtain order ID with the help of existing code
getOrderId(); ?>
Now load order with the help of this particular order ID and fetch the data required
load($orderId); ?>
You can also get other details along with order ID using the code mentioned below
getLastRealOrderId();
$order = Mage::getSingleton('sales/order')->loadByIncrementId($orderId);
?>
How to fetch order details from your order IDrnThe code snippet mentioned below will help you obtain order details like order items, customer billing, payment and shipping details via order id.rnNote: 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.
create('Magento\Sales\Api\Data\OrderInterface')->load($orderid);
//fetch whole order informationrnprint_r($order->getData());
//Or fetch specific informationrnecho $order->getIncrementId();rnecho $order->getGrandTotal();rnecho $order->getSubtotal();
?>
Fetch information for order items
create('Magento\Sales\Api\Data\OrderInterface')->load($orderid);
//Loop through each item and fetch datarnforeach ($order->getAllItems() as $item)
{
//fetch whole item informationrn print_r($item->getData());
//Or fetch specific item informationrn echo $item->getId();
echo $item->getProductType();
echo $item->getQtyOrdered();
echo $item->getPrice();
}
?>
Fetch order payment details
create('Magento\Sales\Api\Data\OrderInterface')->load($orderid);
//fetch whole payment informationrnprint_r($order->getPayment()->getData());
//Or fetch specific payment informationrnecho $order->getPayment()->getAmountPaid();rnecho $order->getPayment()->getMethod();rnecho $order->getPayment()->getAdditionalInformation('method_title');
?>
Fetch order customer details
create('Magento\Sales\Api\Data\OrderInterface')->load($orderid);
//fetch customer informationrnecho $order->getCustomerId();rnecho $order->getCustomerEmail();rnecho $order->getCustomerFirstname();rnecho $order->getCustomerLastname();
?>
Fetch order shipping & billing details
create('Magento\Sales\Api\Data\OrderInterface')->load($orderid);
//fetch whole billing informationrnprint_r($order->getBillingAddress()->getData());
//Or fetch specific billing informationrnecho $order->getBillingAddress()->getCity();rnecho $order->getBillingAddress()->getRegionId();rnecho $order->getBillingAddress()->getCountryId();
//fetch whole shipping informationrnprint_r($order->getShippingAddress()->getData());
//Or fetch specific shipping informationrnecho $order->getShippingAddress()->getCity();rnecho $order->getShippingAddress()->getRegionId();rnecho $order->getShippingAddress()->getCountryId();
?>
How to show order details on success page after the order is placed rnCreate the file app/code/Vendor/Module/etc/di.xml and add the following:
Create the file app/code/Vendor/Module/Block/Success.php and add the following:
_checkoutSession->getLastRealOrder();
}
}
Override templaternCreate the file app/code/Vendor/Module/view/frontend/layout/checkout_onepage_success.xml and add the following:
Create the file app/code/Vendor/Module/view/frontend/templates/checkout/success.phtml and add the following:
getOrderId()):?>
getCanViewOrder()) :?>
%s', $block->escapeHtml($block->getViewOrderUrl()), $block->escapeHtml($block->getOrderId()))) ?>
%1.', $block->escapeHtml($block->getOrderId())) ?>
getOrder()->getTotalQtyOrdered()) ?>
getAdditionalInfoHtml() ?>
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 changern$session->clearQuote();rntorn// $session->clearQuote();rnThis will not clear your quote upon opening the page.
Modify your success pagern rnAdding details to the purchased items is quite complicated so use the following code to load your order objectrn$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
Now you can access the items purchased by following the codern$orderItems = $order->getAllItems();
$purchasedSkus = array();rnforeach($orderItems as $orderItem) {
$purchasedSkus[] = $orderItem->getSku();
}
Preview success pagernThe 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.
rnThe 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.rnYou 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?