document.addEventListener("DOMContentLoaded", () => {
if (!document.querySelector(".ex-cart")) return;
const updateCartSpecialOrder = () => {
const cartData = JSON.parse($("#cartdata").val());
const cartItems = cartData.store_cart;
const productData = [];
const products = [];
cartItems.forEach((element) => {
if (element.item_is_collect) return;
const sku = String(element.item_sku);
const uom = String(element.item_pack_uom || "");
const item_id = String(element.item_id || "");
const template_item = String(
element.item_additional_data.template_item || ""
);
const inventory_v2 = JSON.stringify(element.inventory_v2 || "");
products.push(sku);
productData.push({
item_id: item_id,
sku: sku,
uom: uom,
template_item: template_item,
inventory_v2: inventory_v2,
});
});
if (!productData || !products) return;
$.ajax({
method: "post",
url: "/ajax/live-inventory?_=" + new Date().getTime(),
cache: !1,
data: { products: products, productData: productData },
})
.then(function (t) {
t.data.forEach((element) => {
const productItems = [
...document.querySelectorAll(".item-sku-value.item_sku"),
].filter((el) => el.textContent.trim() == element.sku);
productItems.forEach((e) => {
const tableRow = e.closest("tr");
const orderedQuantity = tableRow.querySelector(
".quantity.form-control"
).value;
const availableForDelivery = element.shipping.count;
let availableForSpecialOrder = 0;
const hasSpecialOrder = element.providers.includes("native");
const isSpecialOrderOnly =
hasSpecialOrder && !element.providers.includes("spruce");
if (hasSpecialOrder) {
availableForSpecialOrder =
element.shipping.warehouses[0].quantity;
}
if (
orderedQuantity <=
availableForDelivery - availableForSpecialOrder &&
!isSpecialOrderOnly
)
return;
const messageWrapper = document.createElement("DIV");
if (isSpecialOrderOnly) {
messageWrapper.innerHTML = `This Item is available via special order only. This item will be delivered within 10-14 days. We will keep you updated via email on the status of your order.`;
} else if (hasSpecialOrder && !isSpecialOrderOnly) {
messageWrapper.innerHTML = `We currently have ${
availableForDelivery - availableForSpecialOrder
} of this item in stock. The remaining ${
orderedQuantity -
(availableForDelivery - availableForSpecialOrder)
} units will be ordered & available in 10–14 days. After checkout, we’ll contact you to confirm whether you’d like to receive a partial fulfillment now, or wait for your full order to arrive.`;
} else {
messageWrapper.innerHTML = `We currently have ${availableForDelivery} of this item in stock. The remaining ${
orderedQuantity - availableForDelivery
} units will be ordered & available in 10–14 days. After checkout, we’ll contact you to confirm whether you’d like to receive a partial fulfillment now, or wait for your full order to arrive.`;
}
const tableRowName = tableRow.querySelector(".name");
messageWrapper.classList.add("message-stock-wrapper", "bg-warning");
const infoElement = document.createElement("I");
infoElement.classList.add("fa-solid", "fa-circle-info");
messageWrapper.prepend(infoElement);
tableRowName.append(messageWrapper);
});
});
})
.fail(function () {
console.log("fail");
});
};
updateCartSpecialOrder();
document.addEventListener(
"cart-total-update",
() => {
updateCartSpecialOrder();
},
false
);
});