Showcasing Payment Method on Magento 2 Success.phtml Page
As a senior developer working extensively with e-commerce platforms like Magento, I frequently encounter situations where clients need to display specific order information on the success page after checkout is complete. One such request is showing the payment method used for an order in the success.phtml file of a Magento 2 site.
The core issue lies in accessing and retrieving the payment method details from the order object within the context of your custom module’s template file. The default Magento Success block does not directly provide this functionality, hence requiring you to create a custom extension to achieve this.
Here’s a step-by-step guide on how to display the payment method name or code in your success.phtml file:
-
Create a custom module If you haven’t already, create a new custom module for your Magento installation. You can follow our comprehensive tutorial on How to Create a Custom Module to get started.
-
Override the Success block In your module’s
etc/di.xmlfile, add an override for the Magento Checkout Success block:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Checkout\Block\Success">
<arguments>
<argument name="context" xsi:type="array">
<item name="template" xsi:type="string">MyFirm_Ordersuccess::success.phtml</item>
</argument>
</arguments>
</type>
</config>
Replace MyFirm with your actual module’s vendor name.
- Retrieve the payment method
In your module’s
Block/Checkout/Success.phpfile, extend the core Magento Success block and add a function to retrieve the payment method:
namespace MyFirm\Ordersuccess\Block\Checkout;
class Success extends \Magento\Checkout\Block\Success
{
protected $checkoutSession;
protected $_customerSession;
public function __construct(
Context $context,
OrderFactory $orderFactory,
Session $session,
array $data = []
) {
$this->checkoutSession = $session;
parent::__construct($context, $orderFactory, $data);
}
public function getPaymentMethod()
{
if ($lastRealOrder = $this->getLastRealOrder()) {
return $lastRealOrder->getPayment()->getMethodInstance()->getTitle();
}
return null;
}
}
- Display the payment method in success.phtml
Open your module’s
view/frontend/templates/success.phtmlfile and add the following code to display the payment method:
<?= __('Your order was placed successfully!', $block->getPaymentMethod()) ?>
This will display the title of the payment method used for the last real order on the success page.
Remember that this solution assumes you have already set up your custom module and have a success.phtml template ready. If you haven’t, make sure to follow our previous guide on How to Create a Custom Template for more detailed instructions.
In conclusion, displaying the payment method in the Magento 2 success.phtml file requires creating a custom extension and overriding the core Success block. By following this step-by-step guide, you can easily retrieve and display the payment method title on your site’s checkout success page without any additional complications.
If you need further assistance or have other questions related to Magento development, feel free to explore our Magento 2 Development Services at Laravel Company. Our experienced team is always ready to help you build and optimize your e-commerce store for better performance and customer experience.