0

I need to get the 'Cash On Delivery' payment method if it is selected. I Cannot get the payment method name. The getMethodInstance() and getTitle() are not working. See the code I used below:

<?php

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart');
$itemsCollection = $cart->getQuote();

$it = $itemsCollection->getPayment();
$iit = $it->getMethod();

1 Answer 1

0

We need to get MethodInstance from quote:

$quote = $cart->getQuote();

$method = $quote->getMethodInstance();

if ($method) {
   $method->getCode();
}

One more, directly trying to use Object Manager is not the best practice.

7
  • Fatal error: Uncaught Error: Call to a member function getCode() on null
    – Ramesh KR
    Commented Nov 17, 2017 at 8:47
  • How to resolve this
    – Ramesh KR
    Commented Nov 17, 2017 at 8:47
  • Checking it. For example: if (null !== $payment->getCode()) Commented Nov 17, 2017 at 8:48
  • I can't understand pls elaborate, I'm using this function in onepage.php file
    – Ramesh KR
    Commented Nov 17, 2017 at 8:49
  • Check my code again. Commented Nov 17, 2017 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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