0

I have a piece of javascript that acts upon the reloadPrice function. It inserts a block of html into the dom to add a corresponding sample product (cloth sample for sofas). This works fine so far.

But to make sure a sample does not get added to cart more than once I need to access the quote (check items) via javascript. Now I am struggeling how to do it.

This is my code:

define([
    'jquery',
    'mage/translate',
    'Magento_Checkout/js/model/quote',
    'mage/utils/wrapper'
], function ($, $t, quote, wrapper) {
    'use strict';

    return function(targetModule){

        var reloadPrice = targetModule.prototype._reloadPrice;
        var reloadPriceWrapper = wrapper.wrap(reloadPrice, function(original) {
            var result = original();

            var simple = this.options.spConfig.children[this.simpleProduct];

            if (typeof simple != 'undefined') {
                if (simple.sample != '') {

                    console.log(quote.getItems);

                    if ($('#order-sample').length) {
                        $('#order-sample').replaceWith(simple.sample);
                    } else {
                        $('#product-options-wrapper').after(simple.sample);
                    }
                    $('#sample-add-to-cart').catalogAddToCart({
                        addToCartButtonTextDefault: $.mage.__('Order a cloth sample')
                    });
                } else {
                    $('#order-sample').remove();
                }
            }

            return result;
        });

        targetModule.prototype._reloadPrice = reloadPriceWrapper;
        return targetModule;
    };
});

My idea was to inject Magento_Checkout/js/model/quote and check the quote's items using getItems. Unfortunately this does only work inside the cart/checkout. When I use it like above I get the error TypeError: window.checkoutConfig is undefined in the console.

So my question is: how can I access the cart/quote and it's items outside checkout via javascript.

Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.