Magento 2 – Fix Empty Checkout Session in Payment Method

cachecheckoutmagento2paymentquote

I'm modifying mercadopago module, and i need to work with the checkout session. When i use this function

$this->_checkoutSession->hasQuote()

It returns false. My class is:

class Payment
    extends \Magento\Payment\Model\Method\AbstractMethod

public function __construct(
        \MercadoPago\Core\Helper\Data $helperData,
        \Magento\Catalog\Helper\Image $helperImage,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Sales\Model\OrderFactory $orderFactory,
        \Magento\Framework\UrlInterface $urlBuilder,
        \Magento\Framework\Model\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
        \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
        \Magento\Payment\Helper\Data $paymentData,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Payment\Model\Method\Logger $logger,
        \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
        \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
        array $data = []
    )
    {
        parent::__construct(
            $context,
            $registry,
            $extensionFactory,
            $customAttributeFactory,
            $paymentData,
            $scopeConfig,
            $logger,
            $resource,
            $resourceCollection,
            $data
        );

        $this->_helperData = $helperData;
        $this->_helperImage = $helperImage;

        $this->_checkoutSession = $checkoutSession;

So it should work, i read that it might be a cache problem, i disabled the cache from everywhere in Magento dashboard -> System -> Cache management but still no success.

How can I solve it? I just need to get the quote ID for a SQL query later. Of course without quote, the function getQuoteId() doesn't work. I tried logging the $checkoutSession but it is empty.

Thanks for your time

Best Answer

I couldn't solve it, neither know what was the problem.

I "solved" it using $this->_checkoutSession->getLastRealOrderId() which works fine (still don't know why)

Related Topic