0

I needed to get the cart total in my observer.

Here is the code I have:

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

use Psr\Log\LoggerInterface;

class DisablePayment implements ObserverInterface
{
  protected $_logger;

  public function __construct(
    \Magento\Customer\Model\Session $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!

0

This is what worked:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cartObj = $objectManager->get('\Magento\Checkout\Model\Cart');
$subTotal = $cartObj->getQuote()->getSubtotal(); //Current Cart Subtotal
$grandTotal = $cartObj->getQuote()->getGrandTotal(); //Cart Grand total

Then I just used this - $total = $grandTotal.

Good luck!

  • Using $objectManager directly is not a good approach to follow. You should use dependency injection for your requirements. – Sumit Sep 19 at 5:10
0

Since you should not use the object manager a better way would be to use the checkout session:

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

use Psr\Log\LoggerInterface;

class DisablePayment implements ObserverInterface
{
  protected $_logger;
  protected $checkoutSession;

  public function __construct(
    \Magento\Customer\Model\Session $customerSession,
    \Magento\Checkout\Model\Session $checkoutSession,
    LoggerInterface $logger
  ) {
    $this->_customerSession = $customerSession;
    $this->checkoutSession = $checkoutSession;
    $this->_logger = $logger;
  }

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

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

  public function execute( Observer $observer )
  {
    $result = $observer->getEvent()->getResult();
    $method_instance = $observer->getEvent()->getMethodInstance()->getCode();
    $quote = $this->checkoutSession->getQuote();
    $total = $quote->getGrandTotal();
    if($method_instance == 'payroll')
    {
      $customerAttribute = 300;

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

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.