0

I have used below code to get the quote object from customer id

{
protected $_objectManager = null;
protected $_customerSession; 
protected $quoteFactory; 
public function __construct(
\Magento\Framework\ObjectManagerInterface $objectManager,
\Magento\Customer\Model\Session $customerSession,
\Magento\Quote\Model\QuoteFactory $quoteFactory
){
    $this->_objectManager = $objectManager;
    $this->_customerSession = $customerSession; 
    $this->quoteFactory = $quoteFactory;
}

 public function execute(){

  if($this->_customerSession->isLoggedIn()){
            $customerId = $this->_customerSession->getCustomer()->getId();
            $quoteData = $this->getQuoteCollection($customerId);
            echo $quoteData->getSubtotal();die;
        }

 }

 public function getQuoteCollection($customerId)
  {
    $quote = $this->quoteFactory->create()->getCollection()->addFieldToFilter('customer_id',$customerId);
    return $quote;
 }
}

How to get the subtotal from current logged in customer quote? I am getting fatal error after using above code.Please anyone help on this.

0

Use checkout session Magento\Checkout\Model\Session

Inject to constructor

protected $_checkoutSession; 
public function __construct(
    \Magento\Checkout\Model\Session $checkoutSession
) {
    $this->_checkoutSession = $checkoutSession;
}

public function execute()
{
    $quote = $this->_checkoutSession->getQuote();
    $subTotal = $quote->getSubtotal();
}

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.