Retrieve Quote id from the masked hash string of the guest quote in Magento 2 to fetch quote object.
masked_id is stored in the quote_id_mask table and it’s the cart hash for the guest customer. When you add a guest item to the cart, one entry generated in the quote_id_mask table to prevent quote id and masked id of the quote.
You can retrieve quote id by using interface,
Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface
<?php namespace Jesadiya\Quote\Model; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface; class Data { /** * @var MaskedQuoteIdToQuoteIdInterface */ private $maskedQuoteIdToQuoteId; public function __construct( MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId ) { $this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId; } /** * get QuoteId by masked id. * * @return int * @throws LocalizedException */ public function getQuoteIdFromMaskedHash() { $maskedHashId = 'oRmLRgab2gka0drj51lCI3tAcfL2fOvI'; try { $cartId = $this->maskedQuoteIdToQuoteId->execute($maskedHashId); } catch (NoSuchEntityException $exception) { throw new LocalizedException( __('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $maskedHashId]) ); } return $cartId; } }
Call from the template or PHP class,
echo $quoteId = $this->getQuoteIdFromMaskedHash();
Output: (int) value
3