Get email from user at checkout success page
-
09-10-2020 - |
Question
I am overriding the checkout success page, Vendor/Theme/Magento_Checkout/templates/success.phtml
and I want to let the user to know that a confirmation e-mail is send to his/her e-mail address.
Is there any way to show the e-mail address, that the user has filled in the checkout page? I have tried searching for methods like getEmail
but could not find anything.
Solution
Overwrite Magento\Checkout\Block\Onepage\Success
Vendor/Module/etc/frontend/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Checkout\Block\Onepage\Success" type="Vendor\Module\Block\Onepage\Success" />
</config>
Vendor/Module/Block/Onepage/Success.php
namespace Vendor\Module\Block\Onepage;
class Success extends \Magento\Checkout\Block\Onepage\Success
{
public function getEmail()
{
$order = $this->_checkoutSession->getLastRealOrder();
return $order->getCustomerEmail();
}
}
Now inside your success.phtml
<?php echo $block->getEmail(); ?>
Make sure your template is define inside checkout_onepage_success.xml following way:
<block class="Magento\Checkout\Block\Onepage\Success" name="checkout.success" template="Magento_Checkout::success.phtml" cacheable="false"/>
That means Magento_Checkout before success.phtml in template attribute.
Clear cache.
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange