0

So what I have done so far is adding new value to the quote_item table using an observer. the column name is expected_delivery_time.

Now the column expected_delivery_time will be populated for the product which added to the cart.

I see the column expected_delivery_time is exist and has the value whenever I see the page source.

Now I would like to show this column value in the checkout page to the users on each selected product.

How do I achieve this ?

For some reason simple thing is not easy in magento 2.

1 Answer 1

0

There are js file in magento 2 called 'Magento_Checkout/js/model/quote' You can use this to get the expected_deliver_time value from your table.

So you can use underscore to looping through the quote and getting the quote_item. and the get the value like this :

var itemQuoteData = quote.getItems();
var result = null;
_.each(itemQuoteData, function (item) {
    if (item_id == item.item_id) {
        result = item.expected_delivery_time; // here it is.
    }
});

and here is the complete code use it in your js file :

define(
    [
        'ko',
        'underscore',
        'uiComponent',
        'Magento_Checkout/js/model/quote',
        'uiLayout',
        'moment',
        'mageUtils'
    ],
    function (
        ko,
        _,
        Component,
        quote,
        layout,
        moment,
        utils
    ) {
        'use strict';


            /**
             * Get Delivery Time
             * from itemQuoteData
             *
             * @param {string} item_id
             * @returns {string} dateTime
             */
            getDeliveryTime: function (item_id) {
                var itemQuoteData = quote.getItems();
                var result = null;
                _.each(itemQuoteData, function (item) {
                    if (item_id == item.item_id) {
                        result = item.expected_delivery_time; // here it is.
                    }
                });

                var deliveryTime = new Date(result.split(" ")[0]);      
                return moment(deliveryTime).format("DD MMMM YYYY");                
            },            


    }
);

Finding this needs research and trial and error. But it is not that hard until you know it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.