Magento 2 Get Masked Quote Id from Cart Id

In this table, you going to see the discussion regarding Magento 2 Get Masked Quote Id from Cart Id in detail and step by step.

If you are looking for this topic then you are at right place.

Let’s start with the topic of Magento 2 Get Masked Quote Id from Cart Id.

It is very important that you should know that Masked id is also called as Guest Cart Hash.

In Magento 2 using cart quote id only possible to get masked id from the cart id.

Table called quote_id_mask table masked id is stored.

It is a randomly generated string.

It is mainly for the guest customer quote.

To get masked id string use interface.

Below is the path for the interface file.

Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface

With help of the below code snippet you come to understand regarding getting of Masked Quote Id from Cart Id.

<?php
namespace Techone\QuoteId\Model;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;

class MaskedHash
{
    /**
     * @var QuoteIdToMaskedQuoteIdInterface
     */
    private $quoteIdToMaskedQuoteId;

    public function __construct(
        QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId
    ) {
        $this->quoteIdToMaskedQuoteId = $quoteIdToMaskedQuoteId;
    }

    /**
     * get Masked id by Quote Id
     *
     * @return string|null
     * @throws LocalizedException
     */
    public function getQuoteMaskId($quoteId)
    {
        $maskedId = null;
        try {
            $maskedId = $this->quoteIdToMaskedQuoteId->execute($quoteId);
        } catch (NoSuchEntityException $exception) {
            throw new LocalizedException(__('Current user does not have an active cart.'));
        }

        return $maskedId;
    }
}

The class file called QuoteIdToMaskedQuoteIdInterface play an important role in this.

Then call the method and pass the parameter called quote id in that method.

Note : It will return masked hash string as output only when cart id has available masked id.

If it is not present then it will through an error.

Error is the current user does not have an active cart.

And if it is present then it will give output as string.

Magento 2 change category name programmatically

Conclusion :

Use above code snippet to understand functionality.

I hope you like this article and if you have any query, please comment below.

Leave a Comment