From 96044bc31dd065766705a17778ec0860f3ffbdbc Mon Sep 17 00:00:00 2001 From: Olivier Lance Date: Fri, 8 Nov 2024 11:17:27 +0100 Subject: [PATCH] fix: don't crash when there is not quantity input --- alma/views/js/alma-product-insurance.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/alma/views/js/alma-product-insurance.js b/alma/views/js/alma-product-insurance.js index 3711df75..1b1b1c8a 100644 --- a/alma/views/js/alma-product-insurance.js +++ b/alma/views/js/alma-product-insurance.js @@ -28,7 +28,6 @@ let loaded = false; let insuranceSelected = false; - let selectedAlmaInsurance = null; let addToCartFlow = false; let quantity = getQuantity(); let almaEligibilityAnswer = false; @@ -105,11 +104,15 @@ } + function getQuantityInput() { + return document.querySelector('.qty [name="qty"]'); + } + // Retrieve wanted product quantity from the quantity selector function getQuantity() { let quantity = 1; - const qtyInput = document.querySelector('.qty [name="qty"]'); + const qtyInput = getQuantityInput(); if (qtyInput) { quantity = Number(qtyInput.value); } @@ -126,8 +129,10 @@ quantity = 1; } - const qtyInput = document.querySelector('.qty [name="qty"]'); - qtyInput.value = quantity; + const qtyInput = getQuantityInput(); + if (qtyInput) { + qtyInput.value = quantity; + } } // Display/hide a spinner on the add to cart button @@ -176,7 +181,8 @@ // Widget is sending us selected insurance data case 'getSelectedInsuranceData': - if (parseInt(document.querySelector('.qty [name="qty"]').value) !== quantity) { + const qtyInput = getQuantityInput(); + if (qtyInput && parseInt(qtyInput.value) !== quantity) { quantity = getQuantity(); }