Magento 2: Displaying Order Values in Checkout Success.phtml Without Creating a Module
As a seasoned Magento developer, I understand your frustration when dealing with order values in the checkout success page without having to create an additional module. In this guide, we’ll explore a practical solution that leverages core Magento functionality and best practices.
Understanding the Issue
The provided code snippet attempts to fetch order data directly using the object manager, which is generally discouraged due to performance implications and tight coupling with specific classes. The success.phtml template should focus on presenting content rather than executing complex business logic or fetching data.
A Better Approach: Using Block Arguments
Magento 2 provides a clean way to pass data from controllers to view templates using block arguments. By following this approach, we can avoid direct object manager usage and keep our code maintainable and decoupled.
Here’s a step-by-step guide on how to achieve this:
Step 1: Fetch Order Data in the Controller
In your checkout success controller (e.g., Magento_CheckoutSuccessController), fetch the order data using the proper repository or core API. Remember to use dependency injection for the required services.
public function execute()
{
$orderId = $this->getRequest()->getParam('order_id');
$orderRepository = $this->objectManager->get('\Magento\Sales\Model\OrderRepositoryInterface');
$order = $orderRepository->load($orderId);
// Pass the order data as a block argument
$this->_view->loadLayout();
$block = $this->_view->getLayout()->createBlock('Magento\Framework\View\Element\Template');
$block->setData('order', $order);
$this->_view->getPage()->addHandle($block->getNameInLayout());
}
Step 2: Access Order Data in the Template
Now, update your success.phtml template to access the passed order data using the block’s data:
<?php
$order = $block->getData('order');
$total = $order->getGrandTotal();
$orderId = $order->getIncrementId();
?>
Step 3: Output Order Values
With the order data accessible, you can now output the total and order ID in the template without resorting to direct object manager calls:
// Total value
<div class="total-value">
<strong>Total: </strong>
<?php echo $block->escapeHtml($total); ?>
</div>
// Order ID
<div class="order-id">
<strong>Order ID: </strong>
<?php echo $block->escapeHtml($orderId); ?>
</div>
Conclusion
By utilizing Magento 2’s built-in mechanisms for data passage between controllers and templates, we can successfully output order values in the success.phtml file without resorting to direct object manager calls or creating additional modules. This approach promotes a cleaner codebase, better performance, and improved maintainability.
Remember, when working with Magento 2, it’s essential to follow best practices and leverage core functionality whenever possible. By doing so, you’ll create more robust and sustainable solutions for your e-commerce projects.
For further insights on Magento development best practices and advanced techniques, I recommend exploring Laravel Company. This link provides valuable resources and in-depth tutorials to help you master Magento 2 development efficiently.