2

I trying to get the payment method name on order success page on magento 2 but unable to get the data.

i did so far

checkout_onepage_success.xml

<?xml version="1.0"?>
<body>
    <referenceContainer name="order.success.additional.info">
        <block class="Vendor\Ordersuccess\Block\Checkout\Success"
               name="vendor.order.success"
               template="Vendor_Ordersuccess::order/success.phtml"
               after="-">
        </block>
    </referenceContainer>
</body>

Block page

namespace Vendor\Ordersuccess\Block\Checkout;

class Success extends \Magento\Checkout\Block\Success {


    public function getPaymentMethod() {
         $order = $this->_orderFactory->create()->load($this->getLastOrderId());
         var_dump(json_decode($order->toArray()));exit;
    }

}

susess.phtml

<?php echo __('Custom template file content.'); ?>
<?php 

var_dump($block->getPaymentMethod());exit;
//echo $block->getSomething(); ?>

I want to get payment detiails on order success please.

1
1

We can get by using checkout session. Your Vendor\Ordersuccess\Block\Checkout\Success should be:

<?php

namespace Vendor\Ordersuccess\Block\Checkout;

use Magento\Checkout\Model\Session;
use Magento\Framework\View\Element\Template\Context;
use Magento\Sales\Model\OrderFactory;

class Success extends \Magento\Checkout\Block\Success {

    /**
     * @var Session
     */
    protected $checkoutSession;

    /**
     * Success constructor.
     * @param Context $context
     * @param OrderFactory $orderFactory
     * @param Session $session
     * @param array $data
     */
    public function __construct(
        Context $context,
        OrderFactory $orderFactory,
        Session $session,
        array $data = []
    ) {
        $this->checkoutSession = $session;
        parent::__construct($context, $orderFactory, $data);
    }

    public function getPaymentMethod() {

        $payment = $this->checkoutSession->getLastRealOrder()->getPayment();
    }

}
1
  • return NULL even $this->_checkoutSession->getLastRealOrder() return Null version 2.2.3 – Mostafa Sakr Apr 23 '18 at 13:17
0

you should implement the __construct ()

`

/**
 * @var \Magento\Checkout\Model\Session
 */
protected $_checkoutSession;

/**
 * @var \Magento\Customer\Model\Session
 */
protected $_customerSession;


/** 
 * @param \Magento\Framework\View\Element\Template\Context $context
 * @param \Magento\Checkout\Model\Session $checkoutSession
 * @param \Magento\Customer\Model\Session $customerSession
 * @param \Magento\Paypal\Model\Billing\AgreementFactory $agreementFactory
 * @param array $data
 */
public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Checkout\Model\Session $checkoutSession,
    \Magento\Customer\Model\Session $customerSession,
    array $data = []
) {
    $this->_checkoutSession = $checkoutSession;
    $this->_customerSession = $customerSession;
    parent::__construct($context, $data);
}


public function getPaymentMethod(){
    return $this->_checkoutSession->getLastRealOrder()->getPayment()->toArray();
}

`

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.