sbodak/magento2-checkout-custom-form

Magento_Checkout/js/model/cart/cache is missing in some Magento versions

ashishjob0424 opened this issue · 3 comments

Firstly thanks for the great extension.

I have 2.1.7 setup and i observe that its missing Magento_Checkout/js/model/cart/cache this file and in your component you required this. I have added this file from another setup to fix issue for now but can you please provide any other solution on this.

Code for this file is.

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

/**
 * Cart adapter for customer data storage.
 * It stores cart data in customer data(localStorage) without saving on server.
 * Adapter is created for shipping rates and totals data caching. It eliminates unneeded calculations requests.
 */
define([
    'underscore',
    'Magento_Customer/js/customer-data',
    'mageUtils'
], function (_, storage, utils) {
    'use strict';

    var cacheKey = 'cart-data',
        cartData = {
            totals: null,
            address: null,
            cartVersion: null,
            shippingMethodCode: null,
            shippingCarrierCode: null,
            rates: null
        },

        /**
         * Set data to local storage.
         *
         * @param {Object} checkoutData
         */
        setData = function (checkoutData) {
            storage.set(cacheKey, checkoutData);
        },

        /**
         * Get data from local storage.
         *
         * @param {String} [key]
         * @returns {*}
         */
        getData = function (key) {
            var data = key ? storage.get(cacheKey)()[key] : storage.get(cacheKey)();

            if (_.isEmpty(storage.get(cacheKey)())) {
                setData(utils.copy(cartData));
            }

            return data;
        },

        /**
         * Build method name base on name, prefix and suffix.
         *
         * @param {String} name
         * @param {String} prefix
         * @param {String} suffix
         * @return {String}
         */
        getMethodName = function (name, prefix, suffix) {
            prefix = prefix || '';
            suffix = suffix || '';

            return prefix + name.charAt(0).toUpperCase() + name.slice(1) + suffix;
        };

    /**
     * Provides get/set/isChanged/clear methods for work with cart data.
     * Can be customized via mixin functionality.
     */
    return {
        cartData: cartData,

        /**
         * Array of required address fields.
         */
        requiredFields: ['countryId', 'region', 'regionId', 'postcode'],

        /**
         * Get data from customer data.
         * Concatenate provided key with method name and call method if it exist or makes get by key.
         *
         * @param {String} key
         * @return {*}
         */
        get: function (key) {
            var methodName = getMethodName(key, '_get');

            if (key === cacheKey) {
                return getData();
            }

            if (this[methodName]) {
                return this[methodName]();
            }

            return getData(key);
        },

        /**
         * Set data to customer data.
         * Concatenate provided key with method name and call method if it exist or makes set by key.
         * @example _setCustomAddress method will be called, if it exists.
         *  set('address', customAddressValue)
         * @example Will set value by provided key.
         *  set('rates', ratesToCompare)
         *
         * @param {String} key
         * @param {*} value
         */
        set: function (key, value) {
            var methodName = getMethodName(key, '_set'),
                obj;

            if (key === cacheKey) {
                _.each(value, function (val, k) {
                    this.set(k, val);
                }, this);

                return;
            }

            if (this[methodName]) {
                this[methodName](value);
            } else {
                obj = getData();
                obj[key] = value;
                setData(obj);
            }
        },

        /**
         * Clear data in cache.
         * Concatenate provided key with method name and call method if it exist or clear by key.
         * @example _clearCustomAddress method will be called, if it exist.
         *  clear('customAddress')
         * @example Will clear data by provided key.
         *  clear('rates')
         *
         * @param {String} key
         */
        clear: function (key) {
            var methodName = getMethodName(key, '_clear');

            if (key === cacheKey) {
                setData(this.cartData);

                return;
            }

            if (this[methodName]) {
                this[methodName]();
            } else {
                this.set(key, null);
            }
        },

        /**
         * Check if provided data has difference with cached data.
         * Concatenate provided key with method name and call method if it exist or makes strict equality.
         * @example Will call existing _isAddressChanged.
         *  isChanged('address', addressToCompare)
         * @example Will get data by provided key and make strict equality with provided value.
         *  isChanged('rates', ratesToCompare)
         *
         * @param {String} key
         * @param {*} value
         * @return {Boolean}
         */
        isChanged: function (key, value) {
            var methodName = getMethodName(key, '_is', 'Changed');

            if (this[methodName]) {
                return this[methodName](value);
            }

            return this.get(key) !== value;
        },

        /**
         * Compare cached address with provided.
         * Custom method for check object equality.
         *
         * @param {Object} address
         * @returns {Boolean}
         */
        _isAddressChanged: function (address) {
            return JSON.stringify(_.pick(this.get('address'), this.requiredFields)) !==
                JSON.stringify(_.pick(address, this.requiredFields));
        }
    };
});


Also if any one tries this extension with PHP version lower than 7 then you need to make some more changes to get it work.

@ashishjob0424 Can you tell me what changes do I need to do to make this module working with my Magento version 2.1.6

I will prepare some workaround.

So, I currently support the latest version.