define(
‘JJ.MaxQuantitylimit.MaxQuantitylimit’
, [
“ProductDetails.Quantity.View”, “Cart.AddToCart.Button.View”,“Backbone”,‘Cart.Summary.View’,
]
, function (
productQuantityView,
CartAddToCartButtonView,Backbone,CartSummaryView
)
{
‘use strict’;
return {
mountToApp: function mountToApp (container)
{
var pdp = container.getComponent(“PDP”);
console.log(‘PDP’,pdp);
var childItemQuantity;
console.log(‘childItemQuantity’,childItemQuantity);
pdp.on(“afterOptionSelection”, function () {
var childInfo = pdp.getSelectedMatrixChilds();
childItemQuantity = childInfo[0].quantityavailable;
Backbone.Events.trigger(“updateQuantity”, childItemQuantity);
});
_.extend(productQuantityView.prototype, {
events: _.extend({}, productQuantityView.prototype.events, {
‘change [data-action=”changeQuantity”]’: “updateQuantity”,
}),
initialize: _.wrap(productQuantityView.prototype.initialize, function (fn) {
var originRet=fn.apply(this, _.toArray(arguments).slice(1));
var self=this;
console.log(‘originRet’,this)
Backbone.Events.off(“updateQuantity”);
Backbone.Events.on(“updateQuantity”, this.updateQuantity, this);
}),
updateQuantity: function updateQuantity(childItemQuantity) {
try {
var childInfo = pdp.getSelectedMatrixChilds();
var QUANTITY = $(“#quantity”).val();
if (!QUANTITY) {
QUANTITY = $(“#in-modal-quantity”).val();
}
console.log(‘QUANTITY:’, QUANTITY);
console.log(‘childInfo:’, childInfo);
$(“.product-details-full-main #product-details-full-form .inventory-display .inventory-display-stock-information-back-order”).css({
“color”: “#c01a29”,
“background-color”: “#fdf0f2”,
“border”: “1px solid #f1ced1”
});
$(“.product-details-full-main #product-details-full-form .inventory-display span.inventory-display-stock-information-back-order.icon”).css({
“color”: “#c01a29”,
“background-color”: “#fdf0f2”,
“border”: “1px solid #fdf0f2”
});
console.log(‘QUANTITY:’, QUANTITY);
console.log(‘childInfo:’, childInfo);
// Display a message based on the quantity value
var quantityAvailable = this.model.attributes.item.attributes.quantityavailable;
if (childInfo.length != 0) {
quantityAvailable = childInfo[0].quantityavailable;
}
if (QUANTITY > quantityAvailable) {
this.showMessage(“Total Ordered Exceeds “ + quantityAvailable + ” units Available”);
$(‘.cart-add-to-cart-button-button’).attr(‘disabled’, ‘disabled’);
} else {
$(‘.cart-add-to-cart-button-button’).removeAttr(‘disabled’);
this.clearMessage();
}
} catch (e) {
console.log(“Error occurred in update quantity”, e);
}
},
showMessage: function (message) {
try {
const MESSAGEELEMENT = $(“.product-details-quantity-container”);
const MESSAGECONTAINER = $(“.message-container”);
// Check if the message container already exists, if not, create it
if (MESSAGECONTAINER.length === 0) {
MESSAGEELEMENT.after(“<div class=’message-container’></div>”);
}
$(“.message-container”).text(message);
} catch (e) {
console.log(“Error occured in show Message”, e);
}
},
clearMessage: function () {
try {
$(“.message-container”).remove();
} catch (e) {
console.log(“message-container class is missing”, e);
}
},
});
_.extend(CartAddToCartButtonView.prototype, {
getContext: _.wrap(CartAddToCartButtonView.prototype.getContext, function (fn) {
var originalRet = fn.apply(this, _.toArray(arguments).slice(1));
try {
var quantityValue=0;
var url = window.location.href;
quantityValue = $(“#quantity”).val();
if (!quantityValue) {
quantityValue = $(“#in-modal-quantity”).val();
}
// Retrieve the quantity value directly without $(document).ready()
quantityValue = url.match(/[?&]quantity=(d+)/i);
if (quantityValue) {
quantityValue = quantityValue[1]; // Extract the captured value
}
var classQuantity = $(“.product-details-quantity-value”).val();
if (classQuantity != quantityValue && (!!classQuantity)) {
quantityValue = classQuantity;
}
var childInfo = pdp.getSelectedMatrixChilds();
var availQuantity = this.model.attributes.item.attributes.quantityavailable;
if (childInfo && childInfo.length > 0) {
availQuantity = childInfo[0].quantityavailable;
}
if (availQuantity === 0 || quantityValue > availQuantity) {
originalRet.disablecart = true;
} else {
originalRet.disablecart = false;
}
} catch (e) {
console.log(“issue in cart container”, e);
}
return originalRet;
})
});
}
};
});