---
title: "How to Fix Checkout Infinite Loop Detected Issue in Magento 2?"
url: "https://magecomp.com/blog/fix-checkout-infinite-loop-detected-issue-magento-2/"
date: "2021-12-17T13:08:52+00:00"
modified: "2025-02-19T05:55:18+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 1453
reading_time: "8 min read"
summary: "Hello Magento Friends,"
description: "Sometimes Magento 2 shows an infinite loop detected error in the checkout page while making payment. Learn How to Fix Checkout Infinite Loop Detected Issue i..."
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to Get Tracking Information From Shipment in Magento 2"
    url: "https://magecomp.com/blog/get-tracking-information-shipment-magento-2/"
  - title: "How to Use Resource Picker using Shopify App Bridge API in Shopify Remix App?"
    url: "https://magecomp.com/blog/use-resource-picker-using-shopify-app-bridge-api/"
  - title: "Signs to Watch Out For the Need to Upgrade to Latest Magento Version"
    url: "https://magecomp.com/blog/signs-watch-need-upgrade-latest-magento-version/"
---

# How to Fix Checkout Infinite Loop Detected Issue in Magento 2?

_Published: December 17, 2021_  
_Author: Dhiren Vasoya_  

![How to Fix Checkout Infinite Loop Detected Issue in Magento 2](https://magecomp.com/blog/wp-content/uploads/2021/12/How-to-Fix-Checkout-Infinite-Loop-Detected-Issue-in-Magento-2-1024x512.webp)

**Hello Magento Friends,**

In today’s blog, I will give you a solution for How to Fix Checkout Infinite Loop Detected Issue in Magento 2.
[![Hire Magento Programmer](https://magecomp.com/blog/wp-content/uploads/2025/02/Hire-Magento-Programmer-CTA-4.webp)](https://magecomp.com/services/hire-magento-programmer/)

In Magento 2 when a customer adds an item to the cart, they are redirected to the checkout page. There they can select their desired payment method and enter the payment details. Once the card details are entered and they click on Place Order, it shows an infinite loop detected error as shown below.
[![checkout infinite loop error](https://magecomp.com/blog/wp-content/uploads/2021/12/checkout-infinite-loop-error.png)](https://magecomp.com/blog/wp-content/uploads/2021/12/checkout-infinite-loop-error.png)

To get rid of this error, let’s look at the solution for Checkout Infinite Loop Detected Issue in Magento 2.
## Steps to Fix Checkout Infinite Loop Detected Issue in Magento 2:
**Step 1:** Create **di.xml** as given below path
**app\code\Vendor\Extension\etc\di.xml**

Now, add the below code
```
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <preference for="MagentoCheckoutModelSession" type="VendorExtensionPreferenceCheckoutModelSession" />
</config>
```

[![Magento Malware Removal](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-Malware-Removal-CTA-3.webp)](https://magecomp.com/magento-malware-removal.html)

**Step 2:** Create **Session.php** at given below path
**app\code\Vendor\Extension\Preference\Checkout\Model\Session.php**

And add the below code
```
<?php
namespace VendorExtensionPreferenceCheckoutModel;

use MagentoCustomerApiDataCustomerInterface;
use MagentoFrameworkAppObjectManager;
use MagentoFrameworkExceptionNoSuchEntityException;
use MagentoQuoteApiDataCartInterface;
use MagentoQuoteModelQuote;
use MagentoQuoteModelQuoteIdMaskFactory;
use PsrLogLoggerInterface;

class Session extends MagentoCheckoutModelSession
{
    const CHECKOUT_STATE_BEGIN = 'begin';

    protected $_quote;
    protected $_customer;
    protected $_loadInactive = false;
    protected $_order;
    protected $_orderFactory;
    protected $_customerSession;
    protected $quoteRepository;
    protected $_remoteAddress;
    protected $_eventManager;
    protected $_storeManager;
    protected $customerRepository;
    protected $quoteIdMaskFactory;
    protected $isQuoteMasked;
    protected $quoteFactory;

    private $isLoading = false;
    private $logger;

    public function __construct(
        MagentoFrameworkAppRequestHttp $request,
        MagentoFrameworkSessionSidResolverInterface $sidResolver,
        MagentoFrameworkSessionConfigConfigInterface $sessionConfig,
        MagentoFrameworkSessionSaveHandlerInterface $saveHandler,
        MagentoFrameworkSessionValidatorInterface $validator,
        MagentoFrameworkSessionStorageInterface $storage,
        MagentoFrameworkStdlibCookieManagerInterface $cookieManager,
        MagentoFrameworkStdlibCookieCookieMetadataFactory $cookieMetadataFactory,
        MagentoFrameworkAppState $appState,
        MagentoSalesModelOrderFactory $orderFactory,
        MagentoCustomerModelSession $customerSession,
        MagentoQuoteApiCartRepositoryInterface $quoteRepository,
        MagentoFrameworkHTTPPhpEnvironmentRemoteAddress $remoteAddress,
        MagentoFrameworkEventManagerInterface $eventManager,
        MagentoStoreModelStoreManagerInterface $storeManager,
        MagentoCustomerApiCustomerRepositoryInterface $customerRepository,
        QuoteIdMaskFactory $quoteIdMaskFactory,
        MagentoQuoteModelQuoteFactory $quoteFactory,
        LoggerInterface $logger = null)
    {
        $this->_orderFactory = $orderFactory;
        $this->_customerSession = $customerSession;
        $this->quoteRepository = $quoteRepository;
        $this->_remoteAddress = $remoteAddress;
        $this->_eventManager = $eventManager;
        $this->_storeManager = $storeManager;
        $this->customerRepository = $customerRepository;
        $this->quoteIdMaskFactory = $quoteIdMaskFactory;
        $this->quoteFactory = $quoteFactory;
        parent::__construct(
            $request,
            $sidResolver,
            $sessionConfig,
            $saveHandler,
            $validator,
            $storage,
            $cookieManager,
            $cookieMetadataFactory,
            $appState
        );
        $this->logger = $logger ?: ObjectManager::getInstance()
            ->get(LoggerInterface::class);
    }

    public function setCustomerData($customer)
    {
        $this->_customer = $customer;
        return $this;
    }

    public function hasQuote()
    {
        return isset($this->_quote);
    }

    public function setLoadInactive($load = true)
    {
        $this->_loadInactive = $load;
        return $this;
    }

    public function getQuote()
    {
        $this->_eventManager->dispatch('custom_quote_process', ['checkout_session' => $this]);

        if ($this->_quote === null)
        {
            if ($this->isLoading)
            {
                throw new LogicException("Infinite loop detected, review the trace for the looping path");
            }
            $this->isLoading = true;
            $quote = $this->quoteFactory->create();
            if ($this->getQuoteId())
            {
                try
                {
                    if ($this->_loadInactive)
                    {
                        $quote = $this->quoteRepository->get($this->getQuoteId());
                    }
                    else
                    {
                        $quote = $this->quoteRepository->getActive($this->getQuoteId());
                    }

                    $customerId = $this->_customer
                        ? $this->_customer->getId()
                        : $this->_customerSession->getCustomerId();

                    if ($quote->getData('customer_id') && (int)$quote->getData('customer_id') !== (int)$customerId)
                    {
                        $quote = $this->quoteFactory->create();
                        $this->setQuoteId(null);
                    }

                    if ($quote->getQuoteCurrencyCode() != $this->_storeManager->getStore()->getCurrentCurrencyCode())
                    {
                        $quote->setStore($this->_storeManager->getStore());
                        $this->quoteRepository->save($quote->collectTotals());
                        $quote = $this->quoteRepository->get($this->getQuoteId());
                    }

                    if ($quote->getTotalsCollectedFlag() === false)
                    {
                        // $quote->collectTotals();

                        /* Here, $quote->collectTotals(); generates error because of recursive call so, by commenting $quote->collectTotals(); or whole if condition it will not go in infinite and code will work properly. Actually there is no need of $quote->collectTotals(); in this file. */
                    }
                }
                catch (NoSuchEntityException $e)
                {
                    $this->setQuoteId(null);
                }
            }

            if (!$this->getQuoteId())
            {
                if ($this->_customerSession->isLoggedIn() || $this->_customer)
                {
                    $quoteByCustomer = $this->getQuoteByCustomer();
                    if ($quoteByCustomer !== null)
                    {
                        $this->setQuoteId($quoteByCustomer->getId());
                        $quote = $quoteByCustomer;
                    }
                }
                else
                {
                    $quote->setIsCheckoutCart(true);
                    $quote->setCustomerIsGuest(1);
                    $this->_eventManager->dispatch('checkout_quote_init', ['quote' => $quote]);
                }
            }

            if ($this->_customer)
            {
                $quote->setCustomer($this->_customer);
            }
            elseif ($this->_customerSession->isLoggedIn())
            {
                $quote->setCustomer($this->customerRepository->getById($this->_customerSession->getCustomerId()));
            }

            $quote->setStore($this->_storeManager->getStore());
            $this->_quote = $quote;
            $this->isLoading = false;
        }

        if (!$this->isQuoteMasked() && !$this->_customerSession->isLoggedIn() && $this->getQuoteId())
        {
            $quoteId = $this->getQuoteId();
            $quoteIdMask = $this->quoteIdMaskFactory->create()->load($quoteId, 'quote_id');
            if ($quoteIdMask->getMaskedId() === null)
            {
                $quoteIdMask->setQuoteId($quoteId)->save();
            }
            $this->setIsQuoteMasked(true);
        }

        $remoteAddress = $this->_remoteAddress->getRemoteAddress();
        if ($remoteAddress)
        {
            $this->_quote->setRemoteIp($remoteAddress);
            $xForwardIp = $this->request->getServer('HTTP_X_FORWARDED_FOR');
            $this->_quote->setXForwardedFor($xForwardIp);
        }

        return $this->_quote;
    }

    protected function _getQuoteIdKey()
    {
        return 'quote_id_' . $this->_storeManager->getStore()->getWebsiteId();
    }

    public function setQuoteId($quoteId)
    {
        $this->storage->setData($this->_getQuoteIdKey(), $quoteId);
    }

    public function getQuoteId()
    {
        return $this->getData($this->_getQuoteIdKey());
    }

    public function loadCustomerQuote()
    {
        if (!$this->_customerSession->getCustomerId())
        {
            return $this;
        }

        $this->_eventManager->dispatch('load_customer_quote_before', ['checkout_session' => $this]);

        try
        {
            $customerQuote = $this->quoteRepository->getForCustomer($this->_customerSession->getCustomerId());
        }
        catch (NoSuchEntityException $e)
        {
            $customerQuote = $this->quoteFactory->create();
        }
        $customerQuote->setStoreId($this->_storeManager->getStore()->getId());

        if ($customerQuote->getId() && $this->getQuoteId() != $customerQuote->getId())
        {
            if ($this->getQuoteId())
            {
                $quote = $this->getQuote();
                $quote->setCustomerIsGuest(0);
                $this->quoteRepository->save(
                    $customerQuote->merge($quote)->collectTotals()
                );
                $newQuote = $this->quoteRepository->get($customerQuote->getId());
                $this->quoteRepository->save(
                    $newQuote->collectTotals()
                );
                $customerQuote = $newQuote;
            }

            $this->setQuoteId($customerQuote->getId());

            if ($this->_quote)
            {
                $this->quoteRepository->delete($this->_quote);
            }
            $this->_quote = $customerQuote;
        }
        else
        {
            $this->getQuote()->getBillingAddress();
            $this->getQuote()->getShippingAddress();
            $this->getQuote()->setCustomer($this->_customerSession->getCustomerDataObject())
                ->setCustomerIsGuest(0)
                ->setTotalsCollectedFlag(false)
                ->collectTotals();
            $this->quoteRepository->save($this->getQuote());
        }
        return $this;
    }

    public function setStepData($step, $data, $value = null)
    {
        $steps = $this->getSteps();
        if ($value === null)
        {
            if (is_array($data))
            {
                $steps[$step] = $data;
            }
        }
        else
        {
            if (!isset($steps[$step]))
            {
                $steps[$step] = [];
            }
            if (is_string($data))
            {
                $steps[$step][$data] = $value;
            }
        }
        $this->setSteps($steps);

        return $this;
    }

    public function getStepData($step = null, $data = null)
    {
        $steps = $this->getSteps();
        if ($step === null)
        {
            return $steps;
        }
        if (!isset($steps[$step]))
        {
            return false;
        }
        if ($data === null)
        {
            return $steps[$step];
        }
        if (!is_string($data) || !isset($steps[$step][$data]))
        {
            return false;
        }
        return $steps[$step][$data];
    }

    public function clearQuote()
    {
        $this->_eventManager->dispatch('checkout_quote_destroy', ['quote' => $this->getQuote()]);
        $this->_quote = null;
        $this->setQuoteId(null);
        $this->setLastSuccessQuoteId(null);
        return $this;
    }

    public function clearStorage()
    {
        parent::clearStorage();
        $this->_quote = null;
        return $this;
    }

    public function clearHelperData()
    {
        $this->setRedirectUrl(null)->setLastOrderId(null)->setLastRealOrderId(null)->setAdditionalMessages(null);
    }

    public function resetCheckout()
    {
        $this->setCheckoutState(self::CHECKOUT_STATE_BEGIN);
        return $this;
    }

    public function replaceQuote($quote)
    {
        $this->_quote = $quote;
        $this->setQuoteId($quote->getId());
        return $this;
    }

    public function getLastRealOrder()
    {
        $orderId = $this->getLastRealOrderId();
        if ($this->_order !== null && $orderId == $this->_order->getIncrementId())
        {
            return $this->_order;
        }
        $this->_order = $this->_orderFactory->create();
        if ($orderId)
        {
            $this->_order->loadByIncrementId($orderId);
        }
        return $this->_order;
    }

    public function restoreQuote()
    {
        $order = $this->getLastRealOrder();
        if ($order->getId())
        {
            try
            {
                $quote = $this->quoteRepository->get($order->getQuoteId());
                $quote->setIsActive(1)->setReservedOrderId(null);
                $this->quoteRepository->save($quote);
                $this->replaceQuote($quote)->unsLastRealOrderId();
                $this->_eventManager->dispatch('restore_quote', ['order' => $order, 'quote' => $quote]);
                return true;
            }
            catch (NoSuchEntityException $e)
            {
                $this->logger->critical($e);
            }
        }

        return false;
    }

    protected function setIsQuoteMasked($isQuoteMasked)
    {
        $this->isQuoteMasked = $isQuoteMasked;
    }

    protected function isQuoteMasked()
    {
        return $this->isQuoteMasked;
    }

    private function getQuoteByCustomer(): ?CartInterface
    {
        $customerId = $this->_customer
            ? $this->_customer->getId()
            : $this->_customerSession->getCustomerId();

        try
        {
            $quote = $this->quoteRepository->getActiveForCustomer($customerId);
        }
        catch (NoSuchEntityException $e)
        {
            $quote = null;
        }

        return $quote;
    }
}
```

## [![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Speed-Performance-Optimization-Services-CTA-1.webp)](https://magecomp.com/magento-performance-optimization.html)

## Conclusion:
This way you can resolve the Checkout Infinite Loop Detected Issue in Magento 2. If the error still exists, [**Hire a Dedicated Magento Developer**](https://magecomp.com/hire-magento-developer.html) that will help to overcome the infinite loop detected problem at[ checkout in Magento 2](https://magecomp.com/magento-2-checkout-success-page.html).
Share the solution to let your developer friends be free of this issue. For more such Magento solutions, stay in touch with us.
**Happy Coding!**


---

_View the original post at: [https://magecomp.com/blog/fix-checkout-infinite-loop-detected-issue-magento-2/](https://magecomp.com/blog/fix-checkout-infinite-loop-detected-issue-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-11-03 12:34:18 UTC_  
