How to Get Quote Id from Masked Quote Id in Magento 2

ajaxcouponknockoutjsmagento2.2.2quote

I need to do something after applying the coupon code in the checkout page, So I have created the ajax call in the response of set-coupon-code.js in Sales Rule module.

But I got the Masked quote Id in set-coupon-code.js.

I have passing quote Id and Coupon code which is applied by the customer to my controller.

QuoteIdMask 2a79ff3a9dfc16b963779118e8c9e4c0

How Do I get Quote Id form masked quote ID? Please provide me a solution

set-coupon-code.js (I have modified the core code in my custom module)

/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/**
 * Customer store credit(balance) application
 */
define([
    'ko',
    'jquery',
    'Magento_Checkout/js/model/quote',
    'Magento_Checkout/js/model/resource-url-manager',
    'Magento_Checkout/js/model/error-processor',
    'Magento_SalesRule/js/model/payment/discount-messages',
    'mage/storage',
    'mage/translate',
    'Magento_Checkout/js/action/get-payment-information',
    'Magento_Checkout/js/model/totals',
    'Magento_Checkout/js/model/full-screen-loader'
], function (ko, $, quote, urlManager, errorProcessor, messageContainer, storage, $t, getPaymentInformationAction,
        totals, fullScreenLoader
        ) {
    'use strict';

    return function (couponCode, isApplied) {
        var quoteId = quote.getQuoteId(),
                url = urlManager.getApplyCouponUrl(couponCode, quoteId),
                message = $t('Your coupon was successfully applied.');

        fullScreenLoader.startLoader();

        return storage.put(
                url,
                {},
                false
                ).done(function (response) {
            var deferred;

            if (response) {
                deferred = $.Deferred();

                isApplied(true);
                totals.isLoading(true);
                getPaymentInformationAction(deferred);
                $.when(deferred).done(function () {
                    fullScreenLoader.stopLoader();
                    totals.isLoading(false);
                });
                messageContainer.addSuccessMessage({
                    'message': message
                });


                return storage.post(
                        'xxx/index/index',
                        JSON.stringify({quoteId: quoteId, couponCode: couponCode})
                        ).done(
                        function (response) {
                            alert("test");



                        }

                ).fail(
                        function (response) {

                        }
                );
            }
        }).fail(function (response) {
            fullScreenLoader.stopLoader();
            totals.isLoading(false);
            errorProcessor.process(response, messageContainer);
        });
    };
});

Controller

public function execute()
    {

        $data = $this->_jsonHelper->jsonDecode($this->getRequest()->getContent());

        $quoteId = $data['quoteId'];
        $couponCode = $data['couponCode'];
echo $quoteId;

}

Best Answer

I got the solution to get the original quote id from Masked Quote id by injecting the Magento\Quote\Model\QuoteIdMaskFactory in my controller.

public function execute()
    {

        $data = $this->_jsonHelper->jsonDecode($this->getRequest()->getContent());

        $quoteId = $data['quoteId'];
        $couponCode = $data['couponCode'];
  $quoteIdMask = $this->quoteIdMaskFactory->create()->load($quoteId, 'masked_id');
            $id = $quoteIdMask->getQuoteId();
echo $id;

}
Related Topic