0

How can I get customer information on success page (template I think is "success.phtml"). I want to set it to something link "Dear Mr. Hans Mustermann ..."

in success.phtml I found this code

$block->getOrderId()

I have tried $block->getCustomerName() or $block->getCustomerGender() but it does not work.

0

You can use below code to get ordered customer details.

$lid = $block->getOrderId();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($lid);
$billingAddress = $order->getBillingAddress();
echo $billingAddress->getFirstname().' '.$billingAddress->getLastname();

Hope this will be helpful.

  • My suggestion would be set the customer name in session and get that in success.phtml rather than loading the whole order. – Kingshuk Deb Apr 13 '17 at 7:13
1

For you are requirement, you need to create a plugin for class

Magento\Checkout\Block\Onepage\Success

In this, you need to pass order object to template file

And using after method of prepareBlockData(), you can do that.

Plugin

<?php

namespace My\Module\Plugin;

class AppendValueAtSuccesspage
{

     public function __construct(
        \Magento\Sales\Model\OrderFactory $orderFactory
    ) {
        $this->orderFactory = $orderFactory;

    }

    public function afterPrepareBlockData(\Magento\Checkout\Block\Onepage\Success $subject, $result)
    {
        $orderIncrementId = $result->getOrderId();
        $order = $this->orderFactory->create()->loadByIncrementId($orderIncrementId);
        $result->setOrderObject($order);
        return $result;
    }
}
?>

di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Checkout\Block\Onepage\Success">
        <plugin name="addorderToTemplate" type="My\Module\Plugin\AppendValueAtSuccesspage" />
    </type>
</config>

success.phtml you can get Order object from getOrderObject()

<?php $_order = $block->getOrderObject() ?>
<?php echo __(' %1', $block->escapeHtml($_order->getCustomerFirstname() ? $_order->getCustomerName() : $_order->getBillingAddress()->getName())) ?>
0
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$customerSession = $objectManager->get('Magento\Customer\Model\Session');

$customerName = '';

if($customerSession->isLoggedIn()) {

   $customerName = $customerSession->getCustomer()->getName();
}
0

Currently, I cannot find a way with Plugin. I will try with Observer Event.

app/code/Vendor/Checkout/etc/frontend/events.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_onepage_controller_success_action">
        <observer name="add_customer_data_order_success"
                  instance="Vendor\Checkout\Observer\AddCustomerDataToOrderSuccess" />
    </event>
</config>

app/code/Vendor/Checkout/Observer/AddCustomerDataToOrderSuccess.php

<?php

namespace Vendor\Checkout\Observer;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;

class AddCustomerDataToOrderSuccess implements ObserverInterface
{
    /**
     * @var \Magento\Framework\View\LayoutInterface
     */
    protected $layout;
    /**
     * @var \Magento\Checkout\Model\Session
     */
    protected $checkoutSession;
    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $customerSession;

    public function __construct(
        \Magento\Framework\View\LayoutInterface $layout,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Customer\Model\Session $customerSession
    )
    {
        $this->layout = $layout;
        $this->checkoutSession = $checkoutSession;
        $this->customerSession = $customerSession;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {

        $checkoutBlock = $this->layout->getBlock('checkout.success');
        if($checkoutBlock) {
            $customer = [];
            $order = $this->checkoutSession->getLastRealOrder();
            $customer['prefix'] = $order->getCustomerPrefix();
            $customer['name'] = $order->getCustomerName();

            $checkoutBlock->setData('customer', $customer);
        }

    }
}

We can use boths of \Magento\Checkout\Model\Session and \Magento\Customer\Model\Session to get customer data.

In the success.phtml in your custom theme, we can get the customer data via $block->getData('customer');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.