immobilien-sparrer/assets/js/checkout.js

161 lines
6.9 KiB
JavaScript
Raw Normal View History

2022-10-19 14:42:20 +00:00
function clearAddressFields() {
$('#address-components').hide();
$('#street-number').val('');
$('#street-number').prop('disabled', true);
$('#route').val('');
$('#locality').val('');
$('#postal-code').val('');
}
function getAddressFiedlValue(addressComponents, $field){
let field = addressComponents.find(obj => obj.types.indexOf($field) > -1);
return field != undefined?field.long_name:'';
}
let input = document.querySelector("#ctelefon");
let iti = null;
if(input != undefined){
iti = window.intlTelInput(input, {
initialCountry: "de",
preferredCountries: ["de"],
geoIpLookup: function(callback) {
$.get('https://ipinfo.io', function() {}, "jsonp").always(function(resp) {
let countryCode = (resp && resp.country)?resp.country:"us";
callback(countryCode);
});
},
utilsScript: "/template/assets/libs/intl-tel-input/js/utils.min.js",
});
}
function updateProducts(){
$('#list-products').html('');
if(objProducts.length > 0){
getInfoFromSelectedProduct().done(function(response, status){
response.contents.forEach(function(product){
let objProduct = getProductInfo(product);
let elementProduct = createProductElement(objProduct);
$('#list-products').append(elementProduct);
});
if(totalPrice > 0){
$('#total-checkout-price').text(numberToEuroFormat(totalPrice));
}else{
$('#form-section').hide();
$('#list-products').parent().parent().parent().parent().parent().find('.modal-footer').hide();
$('#total-checkout-price').parent().parent().parent().hide();
$('#no-products-in-checkout').hide();
$('#cart-error-in-checkout').show();
}
}).fail(function(response){
$('#form-section').hide();
$('#list-products').parent().parent().parent().parent().parent().find('.modal-footer').hide();
$('#total-checkout-price').parent().parent().parent().hide();
$('#no-products-in-checkout').hide();
$('#cart-error-in-checkout').show();
});
$('#form-section').show();
$('#list-products').parent().parent().parent().parent().parent().find('.modal-footer').show();
$('#total-checkout-price').parent().parent().parent().show();
$('#no-products-in-checkout').hide();
$('#cart-error-in-checkout').hide();
}
else{
$('#form-section').hide();
$('#list-products').parent().parent().parent().parent().parent().find('.modal-footer').hide();
$('#total-checkout-price').parent().parent().parent().hide();
$('#no-products-in-checkout').show();
$('#cart-error-in-checkout').hide();
}
}
$(window).on('load', function(){
updateProducts();
$("#successful-submitting-form").delay(4000).slideUp(200, function() { $(this).alert('close'); });
$('#anfrage').submit(function(event){
event.preventDefault();
let form = $(this);
getInfoFromSelectedProduct().done(function(response, status){
let products = response.contents.map(product => {
product = getProductInfo(product);
product = calculatePrice(product);
return product.quantity+'x '+product.title+' ('+numberToEuroFormat(product.calc_preis)+')';
});
let cart = products.join(" - ")+" - Gesammt: "+numberToEuroFormat(totalPrice);
$('#ccart').val(cart);
$('#ctelefon').val(iti.getNumber());
objProducts = [];
localStorage.setItem("products", JSON.stringify(objProducts));
form.unbind('submit').submit();
});
});
$(document).on('click', '.btn-remove-product', function(){
if(objProducts.length > 0){
$('#form-section').show();
$('#list-products').parent().parent().parent().parent().parent().find('.modal-footer').show();
$('#total-checkout-price').parent().parent().parent().show();
$('#no-products-in-checkout').hide();
}else{
$('#form-section').hide();
$('#list-products').parent().parent().parent().parent().parent().find('.modal-footer').hide();
$('#total-checkout-price').parent().parent().parent().hide();
$('#no-products-in-checkout').show();
}
});
$(document).on('input', '.select-quantity', function(){
getInfoFromSelectedProduct().done(function(response, status){
totalPrice = 0;
response.contents.forEach(function(product){
let objProduct = getProductInfo(product);
objProduct = calculatePrice(objProduct);
totalPrice += objProduct.calc_preis*objProduct.quantity;
});
$('#total-checkout-price').text(numberToEuroFormat(totalPrice));
});
});
});
$(window).on('load', google.maps.event, function(){
let input = document.getElementById('field_location');
let options = {
types: ['address'],
componentRestrictions: { country: "de" }
};
var autocomplete = new google.maps.places.Autocomplete(input, options);
autocomplete.addListener("place_changed", () => {
clearAddressFields();
const place = autocomplete.getPlace();
if (!place.geometry) return;
var componentMap = {
country: 'country',
locality: 'locality',
administrative_area_level_1 : 'administrative_area_level_1',
administrative_area_level_2 : 'administrative_area_level_2',
postal_code: 'postal_code',
route: 'route',
street_number : 'street_number',
};
$('#address-components').show();
let addressComponents = autocomplete.getPlace().address_components;
let streetNumber = getAddressFiedlValue(addressComponents, 'street_number');
if(streetNumber == '') {
$('#street-number').prop('disabled', false);
$('#street-number').focus();
}
else {
$('#street-number').prop('disabled', true);
$('#street-number').val(streetNumber);
}
let country = getAddressFiedlValue(addressComponents, 'country');
let route = getAddressFiedlValue(addressComponents, 'route');
$('#route').val(route);
let locality = getAddressFiedlValue(addressComponents, 'locality');
$('#locality').val(locality);
let postalCode = getAddressFiedlValue(addressComponents, 'postal_code');
$('#postal-code').val(postalCode);
$('#street-number').blur(function(){
if($('#street-number').val() == '') $('#street-number').focus();
else{
$('#street-number').prop('disabled', true);
$('#field_location').val(route+' '+$('#street-number').val()+', '+locality+', '+country);
}
});
});
});