Salesforce LWC: Total Quantity Not Updating Consistently in Shopping Cart Component

`updateQuantity(event) {
console.log(“onchange – entered updateQuantity event handler——->”)

    this.shippingAddressId = event.target.dataset.id;
    this.posItemId = event.target.dataset.pos;

    this.quantityUpdateValue = event.target.value;
    console.log("onchange - this.quantityUpdateValue------->",this.quantityUpdateValue);

    this.orderId = event.target.dataset.order;
    var posItemsListWithCart = JSON.parse(JSON.stringify(this.posItemsListInCart))
    // console.log('posItemsListWithCart--->',JSON.stringify(posItemsListWithCart))
    for (var i = 0; i < posItemsListWithCart.length; i++) {
        var individualQty = 0;
        // Author: Shilpa and Kavya
        // Date: 19-12-2023
        // To get the Logical_Inventory__c from POS_Item_Facilities__r
        for(var z=0;z<posItemsListWithCart[i].posItem.POS_Item_Facilities__r.length;z++){
        var currentAvailableStock = posItemsListWithCart[i].posItem.POS_Item_Facilities__r[z].Logical_Inventory__c;
        }
        // Changes done upto here
        for (var j = 0; j < posItemsListWithCart[i].selectedAddressWithQtyDetails.length; j++) {
            if (this.shippingAddressId == posItemsListWithCart[i].selectedAddressWithQtyDetails[j].selectedAddress.Id) {
                let quantityTobeUpdated = event.target.value;

                this.quantityUpdateValue = quantityTobeUpdated == '' ? 0 : quantityTobeUpdated;
                console.log("onchange - checking if it is empty or not ----------> ",this.quantityUpdateValue)


                posItemsListWithCart[i].selectedAddressWithQtyDetails[j].quantity = this.quantityUpdateValue;
                console.log("onchange - to update the quantity in the data structure ----------> ",posItemsListWithCart[i].selectedAddressWithQtyDetails[j].quantity)
            }

            // after getting the qunity to calculate the totla quntity
            var quantity = posItemsListWithCart[i].selectedAddressWithQtyDetails[j].quantity == '' ? 0 : posItemsListWithCart[i].selectedAddressWithQtyDetails[j].quantity;
            console.log("onchange - to ensure that the quantity variable always contains a number------>",quantity);


            individualQty = parseInt(individualQty) + parseInt(quantity);
            console.log("onchange - to sum up the individual quantities for each selected address within a POS item------>",individualQty);

            posItemsListWithCart[i].selectedAddressWithQtyDetails[j].quantity = parseInt(quantity);
            console.log("onchange - assigning the parser integer value --------->",posItemsListWithCart[i].selectedAddressWithQtyDetails[j].quantity)

        }
        posItemsListWithCart[i].totalQuantityFromAllAddresses = individualQty;
        console.log('onchange - asssigning parsed integer value to the totalQuantityFromAllAddresses container--------->',posItemsListWithCart[i].totalQuantityFromAllAddresses)

        // to check if the total quntity is less than current available stock
        if (posItemsListWithCart[i].totalQuantityFromAllAddresses <= currentAvailableStock) {
            posItemsListWithCart[i].isAvailableQuantityExceeded = false;

        } else {
            posItemsListWithCart[i].isAvailableQuantityExceeded = true;
            this.disablePreview = true;

        }

    }

    this.posItemsListInCart = posItemsListWithCart;
    console.log("onchange - this.posItemsListInCart ---------->",JSON.stringify(this.posItemsListInCart))
    // to send the updated quntity ot the apex method "updateQuantityForSelectedPosItem" with the parameter positemid,shipping address id and order id and quntity added.
    updateQuantityForSelectedPosItem({
        posItemId: this.posItemId,
        shippingAddressId: this.shippingAddressId,
        orderId: this.orderId,
        quantity: this.quantityUpdateValue
    })
        .then(result => {
            // once the quntity is sent to the apex method the result should be sent as 'done'
            let resultObtained = result;
            // console.log("resultObtained ---------> ",resultObtained)
            return refreshApex(this.cartItemsToBeDisplayed);
        })
        .catch(error => {
            this.error = error;
            //console.log('ERROR', this.error);
        });
}`

`get cartItemDetails() {
// To get the pos items in cart to display
if (this.cartItemsToBeDisplayed.data) {
console.log(‘onload START – inside if’)
console.log(“onload – after updating this.cartItemsToBeDisplayed.data”,JSON.stringify(this.cartItemsToBeDisplayed.data))
// console.log(‘cartItemsToBeDisplayed–>’, JSON.stringify(this.cartItemsToBeDisplayed.data))

        this.posItemsListInCart = this.cartItemsToBeDisplayed.data;
        // console.log("onload - hold the updated data in the this.posItemsListInCart continer ",JSON.stringify(this.posItemsListInCart))

        //To check if the given pos item has the data or not 
        if (this.posItemsListInCart.length > 0) {
            // console.log('onload - inside if 1 ')
            // console.log("onload - check the length -------->",this.posItemsListInCart.length)
            this.isAddressAvailableForDisplay = true;
            // console.log("onload - this.isAddressAvailableForDisplay is true -------->",this.isAddressAvailableForDisplay)
        } else {
            this.isAddressAvailableForDisplay = false;
            // console.log("onload - this.isAddressAvailableForDisplay is false -------->",this.isAddressAvailableForDisplay)
        }
        // to assign the total quanity to zero by default
        this.totalQuantity = 0;
        // if total quntity is 0 then disable the preview button
        this.disablePreview = true;
        // local variable to convert the pos items in cart to json parse so that anyone can edit the incoming items
        var posItemsListWithCart = JSON.parse(JSON.stringify(this.posItemsListInCart));
        // console.log("onload - local variable to convert the pos items in cart to json parse posItemsListWithCart -------->",JSON.stringify(this.isAddressAvailableForDisplay))

        var isAvailableQuantityExceeded = false;

        //to check if the entred quantity has exceed initally
        for (var i = 0; i < posItemsListWithCart.length; i++) {
            // console.log('inside for loop--------->')

            this.totalQuantity += this.posItemsListInCart[i].totalQuantityFromAllAddresses;
            // console.log('onload  - increments this.totalQuantity--------->',this.totalQuantity)

            var individualQty = 0;
            // Author: Shilpa and Kavya
            // Date: 19-12-2023
            // To get the Logical_Inventory__c from POS_Item_Facilities__r
            for(var z=0;z<posItemsListWithCart[i].posItem.POS_Item_Facilities__r.length;z++){
                // console.log('inside for loop 1')

            //local variable to store thhe logical inventory stock
            var currentAvailableStock = posItemsListWithCart[i].posItem.POS_Item_Facilities__r[z].Logical_Inventory__c;
            // console.log("onload - currentAvailableStock",currentAvailableStock)
            }
            // Changes done upto here
            //console.log('currentAvailableStock',currentAvailableStock)
            // to check if any item in cart has a quanity entred greater than available stock
            for (var j = 0; j < posItemsListWithCart[i].selectedAddressWithQtyDetails.length; j++) {
                // console.log('onload  - inside for loop 2')

                var quantity = posItemsListWithCart[i].selectedAddressWithQtyDetails[j].quantity == '' ? 0 : posItemsListWithCart[i].selectedAddressWithQtyDetails[j].quantity;
                // console.log('onload - quantity -------->',quantity)

                individualQty = parseInt(individualQty) + parseInt(quantity);
                // console.log('onload - individualQty -------->',individualQty)

                posItemsListWithCart[i].selectedAddressWithQtyDetails[j].quantity = parseInt(quantity);
                // console.log('onload - posItemsListWithCart[i].selectedAddressWithQtyDetails[j].quantity -------->',posItemsListWithCart[i].selectedAddressWithQtyDetails[j].quantity)
            }
            posItemsListWithCart[i].totalQuantityFromAllAddresses = individualQty;
            // console.log('onload - posItemsListWithCart[i].totalQuantityFromAllAddresses -------->',posItemsListWithCart[i].totalQuantityFromAllAddresses)
            //if exceded then disable the preview button
            //to display error message
            if (posItemsListWithCart[i].totalQuantityFromAllAddresses <= currentAvailableStock) {
                posItemsListWithCart[i].isAvailableQuantityExceeded = false;

            } else {
                posItemsListWithCart[i].isAvailableQuantityExceeded = true;
                isAvailableQuantityExceeded = true;

            }

        }
        this.posItemsListInCart = posItemsListWithCart;
        // console.log('onload - this.posItemsListInCart -------->',JSON.stringify(this.posItemsListInCart))
        
        this.cartItemsToBeDisplayed.data = posItemsListWithCart;
        // console.log('onload END - this.cartItemsToBeDisplayed.data -------->',JSON.stringify(this.cartItemsToBeDisplayed.data))

        // if quntity is > 0 then enable the repview button
        if (this.totalQuantity > 0) {
            this.disablePreview = false;
        }
        // if quntity is = 0 then the repview button is disabled with the help of the variable "disablePreview"
        if (this.totalQuantity == 0 || isAvailableQuantityExceeded) {
            this.disablePreview = true;
        }
        if (this.posItemsListInCart.length > 0) {
            if (this.posItemsListInCart[0].orderId) {
                this.orderId = this.posItemsListInCart[0].orderId;
            }
        }
        /**
    refreshApex(this.cartItemsToBeDisplayed);
}`

New contributor

Ananya SR is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật