Skip to content

Magento 2 – Get Cart Total in Observer

I needed to get the cart total in my observer.

Here is the code I have:

use MagentoFrameworkEventObserver;
use MagentoFrameworkEventObserverInterface;

use PsrLogLoggerInterface;

class DisablePayment implements ObserverInterface
{
  protected $_logger;

  public function __construct(
    MagentoCustomerModelSession $customerSession,
    LoggerInterface $logger
  ) {
    $this->_customerSession = $customerSession;
    $this->_logger = $logger;
  }

  /**
   * @param Observer $observer
   *
   * @return void
   */

   public function getCustomAttribute(){
      if($this->_customerSession->isLoggedIn()):
        return $customerAtt = $this->_customerSession->getCustomer()->getData('credit');
      endif;
   }

  public function execute( Observer $observer )
  {
    $result = $observer->getEvent()->getResult();
    $method_instance = $observer->getEvent()->getMethodInstance()->getCode();
    $quote = $observer->getEvent()->getQuote();

    if($method_instance == 'payroll')
    {
      $customerAttribute = 300;
      $total = 400;

      if($customerAttribute < $total)
      {
        $result->setData('is_available', false);
      }
    }
  }

How can I do that?

Thanks!