2

When you create a guest cart you will receive a token that may look like this "696EreQdYt2AAtEceQsHfD2gxGyWFodG" and you can use it to retrieve a guest cart that has an id number.

I know how to retrieve the guest cart using the id. My question is, can I retrieve the guest cart using the token (programmatically)?

4

Add this dependency to your class constructor:

private $quoteIdMaskFactory;

public function __construct(
   ....
   \Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory,
   ....
) {
   ....
   $this->quoteIdMaskFactory = $quoteIdMaskFactory;
   ....
}

Then your can retrieve the cart it like this:

$maskedId = '696EreQdYt2AAtEceQsHfD2gxGyWFodG';
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($maskedId, 'masked_id');
if ($quoteIdMask->getId()) {
    $cartId = $quoteIdMask->getQuoteId();
} else {
    //cart does not exist.
}
| improve this answer | |
  • You are faster then me:) – Amit Bera May 23 '19 at 7:58
  • Worked perfectly, thank you! – kolozz May 23 '19 at 8:18

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.