I'm using Magento 2.4.1 and I want to retrieve the current quote in collectRates function in my Carrier model, but sometimes it will prompt an infinite loop error like this:

enter image description here

this is how i retrieve the current quote in My Carrier Model

class Standard extends \Magento\Shipping\Model\Carrier\AbstractCarrier implements
   public function collectRates(RateRequest $request)
     $quote = $this->checkoutSession->getQuote();
     $subTotal = $quote->getData('subtotal_with_discount');
     $ddate = $quote->getShippingDeliveryDate(); // this is a custom column i made in quote table

The reason to get quotes is that I need to retrieve the quote attribute value to do some calculation in the collect rates function

  • Good to see that you have resolved your issue :) – Shoaib Munir Jun 21 at 10:49

You can not directly get quote in collect rate.

The $this->checkoutSession->getQuote() will call your collectRates function at the end, so you will end up getting infinite loop.

the $request variable will also give you almost all the data you need, like

  • $request->getAllItems()
  • $request->getOrderSubtotal();
  • $request->getDestCity();
  • etc

So there is no direct way to get quote. This is not the end, you have an alternative, may be it will work for you. Try this code:

     * Do not use checkoutSession->getQuote()!!! it will cause infinite loop for
     * quotes with trigger_recollect = 1, see Quote::_afterLoad()
    $items = $request->getAllItems();
    if (empty($items)) {
        return false;

    /** @var \Magento\Quote\Model\Quote\Item $firstItem */
    $firstItem = reset($items);
    if (!$firstItem) {
        return false;

    $quote = $firstItem->getQuote();
    if (!($quote instanceof \Magento\Quote\Model\Quote)) {
        return false;

Hope this will help you.


Warning : Do not use $this->checkoutSession->getQuote(); cause it recursively calls your get collect method and you will have infinite loop.

You can also get current quote in collect rate method by load cart model, it always gives you quote of current session.

    use  Magento\Checkout\Model\Cart;
        protected $cart;
        public function __construct(
                 Cart $cart
             ) {
                 $this->cart= $cart;
    $quote= $this->cart->getQuote();
    $subTotal = $quote->getData('subtotal_with_discount');
    $ddate = $quote->getShippingDeliveryDate(); // this is a custom column i made in quote table

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.