0

I am working on shipping module where i need to fetch quote subtotal in both area frontend as well as admin order create.

I am using Magento\Checkout\Model\Session to get the quote subtotal but when i try to check in admin no value return for quote.

Also tried Magento\Quote\Model\Quote method getBaseSubTotal return nothing.

Any other solution where we can use same code for both area.

3 Answers 3

1

In General, Checkout Session is not initiated for adminhtml area so it won't be possible without customization.

In My opinion, you should set some value in quote table when quote is created or initiated every time and that is similar for adminhtml and frontend area.

For example. save IP address in quote cart is created is create and fetch Quote by current IP match with Quote IP and it will work from admin too.

Hope this approach works!!!

1
  1. For Admin quote session Magento\Backend\Model\Session\Quote class is responsible for getting backend quote order processes

  2. For Frontend :

You can get by Factory method

protected $quoteFactory;
 
public function __construct(
    \Magento\Quote\Model\QuoteFactory $quoteFactory,
    ....
) {
    $this->quoteFactory = $quoteFactory;
    ....
}

// create a quote object

$quote = $this->quoteFactory->create()->load($quoteId);

OR You can get by Repository method

protected $quoteRepository;
 
public function __construct(
    \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
    ....
) {
    $this->quoteRepository = $quoteRepository;
    ....
}

//    get a quote object by its id

$this->quoteRepository->get($quoteId);

Please refer this : https://mage2-blog.com/magento-2-get-quote-by-id/

1
  • Yes we can write code based on the area code initialized but i would like to know about core code how it work? Magento also using different code for getting quote on both area or single class responsible?
    – M2 Dev
    Dec 14, 2022 at 14:59
1

You can use the Magento\Quote\Model\QuoteRepository class to get the quote subtotal in both the front and backend. This class is used to retrieve quotes from the database and will have the quote subtotal included.

You can call this class in the front end using the \Magento\Framework\App\ObjectManager.

In the backend, you can call it using the Magento\Quote\Model\QuoteRepositoryFactory.

Example:

//Frontend 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$quoteRepository = $objectManager->create('Magento\Quote\Model\QuoteRepository');
$quote = $quoteRepository->get($quoteId);
$subtotal = $quote->getBaseSubtotal();

//Backend 
$quoteRepositoryFactory = $this->_objectManager->create('Magento\Quote\Model\QuoteRepositoryFactory');
$quoteRepository = $quoteRepositoryFactory->create();
$quote = $quoteRepository->get($quoteId);
$subtotal = $quote->getBaseSubtotal();

Your Answer

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

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