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();
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.

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

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.