-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchoose-shipping-method.html
More file actions
52 lines (45 loc) · 2.1 KB
/
choose-shipping-method.html
File metadata and controls
52 lines (45 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<script>
async function addShippingMethodPreference() {
saveShippingMethodButton.style.display = 'none';
saveButtonLoadingDiv.style.display = 'flex';
let method = "Service point";
var radioButtons = document.getElementsByName("shippingMethod");
for (var x = 0; x < radioButtons.length; x++) {
if (radioButtons[x].checked) {
method = radioButtons[x].value; // "Service point" or "Pickup"
}
}
await db.collection('users').doc(authUser.current.uid).update({ "preferences.shippingMethod": method }).then(async (docRef) => {
console.log(`Shipping method '${method}' stored as preference on user with ID: `, authUser.current.uid);
await batchUpdateShippingMethod(method);
triggerShippingMethodConfirmationView.click();
});
}
async function batchUpdateShippingMethod(shippingMethod) {
const items = await getItems(authUser.current.uid);
// Prepare the batch
let batch = db.batch();
let itemsCount = 0;
let updatesCount = 0;
items.forEach((doc) => {
itemsCount++;
// If no shippingMethod, add to batch
const currentShippingMethod = doc.data().shippingMethod || null;
const status = doc.data().status;
const archived = doc.data().archived;
if (!currentShippingMethod && status !== "Unsold" && !archived) {
const docRef = db.collection('items').doc(doc.id);
batch.update(docRef, { shippingMethod });
updatesCount++;
}
})
// Commit the batch
await batch.commit();
console.log(`Updated ${updatesCount} items out of user's ${itemsCount} available items with shippingMethod '${shippingMethod}'`);
}
saveShippingMethodButton.addEventListener("click", addShippingMethodPreference);
closeConfirmationButton.addEventListener("click", function () {
closeConfirmationButton.style.display = 'none';
closeButtonLoadingDiv.style.display = 'flex';
});
</script>