273 lines
10 KiB
JavaScript
Executable File
273 lines
10 KiB
JavaScript
Executable File
$(document).ready(function() {
|
|
|
|
$('#photos-completed-button').click(function(event){
|
|
event.preventDefault();
|
|
|
|
var accessCode = $('#product-additional-info-access-code').val();
|
|
|
|
$.ajax({
|
|
url: 'index.php?fc=module&module=thobbodymeasurements&controller=SetProductMeasurements&ajax=true&action=getCustomerMeasurementsForMirrorSize',
|
|
method: 'POST',
|
|
dataType: 'json',
|
|
headers: {
|
|
Accepts: "application/json",
|
|
},
|
|
data: {
|
|
ajax: true,
|
|
action: 'getCustomerMeasurementsForMirrorSize',
|
|
accessCode: accessCode,
|
|
},
|
|
success: function(response) {
|
|
console.log(response);
|
|
// window.location.reload();
|
|
},
|
|
error: function(xhr, status, error) {
|
|
console.error(xhr.responseText);
|
|
}
|
|
});
|
|
});
|
|
|
|
$('#set-measurements-button').click(function(event) {
|
|
// Prevent the default behavior of the anchor tag
|
|
event.preventDefault();
|
|
|
|
// Toggle the visibility of the modal
|
|
$('#measurements-modal').toggle();
|
|
});
|
|
$(document).click(function(event) {
|
|
// Check if the click target is not within the modal
|
|
if (!$(event.target).closest('#measurements-modal').length && !$(event.target).is('#set-measurements-button')) {
|
|
// Hide the modal
|
|
$('#measurements-modal').hide();
|
|
}
|
|
});
|
|
});
|
|
|
|
$('#closebutton').click(function() {
|
|
$('#measurements-modal').hide();
|
|
});
|
|
|
|
$('#closemirrorbutton').click(function() {
|
|
$('#measurements-modal').hide();
|
|
});
|
|
|
|
|
|
// Add click event handler to the "Photos Completed" button
|
|
$('#photos-completed-button').click(function(event) {
|
|
// Perform some action here
|
|
alert('Photos Completed button clicked!');
|
|
});
|
|
|
|
|
|
$('#submit-measurements-button').click(function(event) {
|
|
event.preventDefault();
|
|
var measurements = [];
|
|
|
|
$('.modal-set-measurements .form-group').each(function() {
|
|
var attributeId = $(this).find('input').attr('id');
|
|
var value = $(this).find('input').val();
|
|
console.log("Attribute id: ",attributeId)
|
|
console.log("value: ",value)
|
|
|
|
|
|
measurements.push({
|
|
"attributeId": attributeId,
|
|
"value": value
|
|
});
|
|
});
|
|
console.log("measurements: ",measurements)
|
|
|
|
var formId = $('#current_form_id').val();
|
|
var formLabel = $('#current_form_label').val();
|
|
|
|
var productId = $('#product-additional-info-product-id').val();
|
|
var categoryId = $('#product-additional-info-category-id').val();
|
|
var productAttributeId = $('#product-additional-info-product-attribute-id').val();
|
|
|
|
$.ajax({
|
|
url: 'index.php?fc=module&module=thobbodymeasurements&controller=SetProductMeasurements&ajax=true&action=saveProductMeasurements',
|
|
method: 'POST',
|
|
dataType: 'json',
|
|
headers: {
|
|
Accepts: "application/json",
|
|
},
|
|
data: {
|
|
ajax: true,
|
|
action: 'saveProductMeasurements',
|
|
formId: formId,
|
|
formLabel: formLabel,
|
|
measurements: JSON.stringify(measurements),
|
|
productId: productId,
|
|
categoryId: categoryId,
|
|
productAttributeId: productAttributeId,
|
|
},
|
|
success: function(response) {
|
|
console.log(response);
|
|
window.location.reload();
|
|
},
|
|
error: function(xhr, status, error) {
|
|
console.error(xhr.responseText);
|
|
}
|
|
});
|
|
});
|
|
|
|
$('.add-to-cart').click( (event) => {
|
|
console.log("inside add to cart click...");
|
|
|
|
var productId = $('#product-additional-info-product-id').val();
|
|
var categoryId = $('#product-additional-info-category-id').val();
|
|
var productAttributeId = $('#product-additional-info-product-attribute-id').val();
|
|
|
|
if(!productId && !categoryId){
|
|
return;
|
|
}
|
|
|
|
event.preventDefault();
|
|
event.stopImmediatePropagation();
|
|
|
|
var formId = $('#current_form_id').val();
|
|
var formLabel = $('#current_form_label').val();
|
|
var measurements = [];
|
|
|
|
$('.modal-set-measurements .form-group').each(function() {
|
|
var attributeId = $(this).find('input').attr('id');
|
|
var value = $(this).find('input').val();
|
|
|
|
measurements.push({
|
|
"attributeId": attributeId,
|
|
"value": value
|
|
});
|
|
});
|
|
|
|
//first check if the measurements are set by the customer
|
|
$.ajax({
|
|
url: 'index.php?fc=module&module=thobbodymeasurements&controller=SetProductMeasurements&ajax=true&action=customerHasMeasurements',
|
|
method: 'POST',
|
|
dataType: 'json',
|
|
headers: {
|
|
Accepts: "application/json",
|
|
},
|
|
data: {
|
|
ajax: true,
|
|
action: 'customerHasMeasurements',
|
|
productId: productId,
|
|
categoryId: categoryId,
|
|
}
|
|
}).done(function (response){
|
|
console.log('has measurements response....');
|
|
if(!response.hasMeasurements){
|
|
console.log("Set measurements first...");
|
|
$('#measurements-modal').modal('show');
|
|
}else{
|
|
$.ajax({
|
|
url: 'index.php?fc=module&module=thobbodymeasurements&controller=SetProductMeasurements&ajax=true&action=saveProductMeasurements',
|
|
method: 'POST',
|
|
dataType: 'json',
|
|
headers: {
|
|
Accepts: "application/json",
|
|
},
|
|
data: {
|
|
ajax: true,
|
|
action: 'saveProductMeasurements',
|
|
formId: formId,
|
|
formLabel: formLabel,
|
|
measurements: JSON.stringify(measurements),
|
|
productId: productId,
|
|
categoryId: categoryId,
|
|
productAttributeId: productAttributeId,
|
|
},
|
|
success: function(response) {
|
|
console.log(response);
|
|
console.log("Proceed to add to cart....");
|
|
console.log(event.currentTarget)
|
|
|
|
const $form = $(event.currentTarget.form);
|
|
const query = `${$form.serialize()}&add=1&action=update`;
|
|
console.log("Form: ", $form.serialize());
|
|
console.log("Query: ", query);
|
|
|
|
const actionURL = $form.attr('action');
|
|
console.log("actionURL: ", actionURL);
|
|
const addToCartButton = $(event.currentTarget);
|
|
|
|
addToCartButton.prop('disabled', true);
|
|
|
|
const isQuantityInputValid = ($input) => {
|
|
let validInput = true;
|
|
|
|
$input.each((index, input) => {
|
|
const $currentInput = $(input);
|
|
const minimalValue = parseInt($currentInput.attr('min'), 10);
|
|
|
|
if (minimalValue && $currentInput.val() < minimalValue) {
|
|
onInvalidQuantity($currentInput);
|
|
validInput = false;
|
|
}
|
|
});
|
|
|
|
return validInput;
|
|
};
|
|
|
|
let onInvalidQuantity = ($input) => {
|
|
$input
|
|
.parents(prestashop.selectors.product.addToCart)
|
|
.first()
|
|
.find(prestashop.selectors.product.minimalQuantity)
|
|
.addClass('error');
|
|
$input
|
|
.parent()
|
|
.find('label')
|
|
.addClass('error');
|
|
};
|
|
|
|
const $quantityInput = $form.find('input[min]');
|
|
|
|
if (!isQuantityInputValid($quantityInput)) {
|
|
onInvalidQuantity($quantityInput);
|
|
return;
|
|
}
|
|
|
|
$.post(actionURL, query, null, 'json')
|
|
.then((resp) => {
|
|
console.log('Resp: ', resp);
|
|
if (!resp.hasError) {
|
|
prestashop.emit('updateCart', {
|
|
reason: {
|
|
idProduct: resp.id_product,
|
|
idProductAttribute: resp.id_product_attribute,
|
|
idCustomization: resp.id_customization,
|
|
linkAction: 'add-to-cart',
|
|
cart: resp.cart,
|
|
},
|
|
resp,
|
|
});
|
|
} else {
|
|
prestashop.emit('handleError', {
|
|
eventType: 'addProductToCart',
|
|
resp,
|
|
});
|
|
}
|
|
})
|
|
.fail((resp) => {
|
|
prestashop.emit('handleError', {
|
|
eventType: 'addProductToCart',
|
|
resp,
|
|
});
|
|
})
|
|
.always(() => {
|
|
setTimeout(() => {
|
|
addToCartButton.prop('disabled', false);
|
|
}, 1000);
|
|
});
|
|
},
|
|
error: function(xhr, status, error) {
|
|
console.error(xhr.responseText);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
});
|
|
|
|
|
|
|