areya-energy/assets/js/checkout.js

161 lines
6.9 KiB
JavaScript
Raw Normal View History

2022-09-04 19:41:13 +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:'';
}
2022-09-07 12:50:07 +00:00
2022-09-04 19:41:13 +00:00
let input = document.querySelector("#ctelefon");
2022-09-08 11:31:11 +00:00
let iti = null;
2022-09-05 14:58:04 +00:00
if(input != undefined){
2022-09-08 11:31:11 +00:00
iti = window.intlTelInput(input, {
2022-09-05 14:58:04 +00:00
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);
});
},
2022-09-07 08:51:17 +00:00
utilsScript: "/template/assets/libs/intl-tel-input/js/utils.min.js",
2022-09-05 14:58:04 +00:00
});
}
2022-09-07 10:46:44 +00:00
function updateProducts(){
2022-09-07 12:40:02 +00:00
$('#list-products').html('');
2022-09-07 10:46:44 +00:00
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);
});
2022-09-08 11:14:40 +00:00
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();
}
2022-09-08 10:13:43 +00:00
}).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();
2022-09-07 10:46:44 +00:00
});
$('#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();
2022-09-08 10:13:43 +00:00
$('#cart-error-in-checkout').hide();
}
else{
2022-09-07 10:46:44 +00:00
$('#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();
2022-09-08 10:13:43 +00:00
$('#cart-error-in-checkout').hide();
2022-09-07 10:46:44 +00:00
}
}
2022-09-07 12:50:07 +00:00
2022-09-04 19:41:13 +00:00
$(window).on('load', function(){
2022-09-07 10:46:44 +00:00
updateProducts();
2022-09-05 14:58:04 +00:00
$("#successful-submitting-form").delay(4000).slideUp(200, function() { $(this).alert('close'); });
2022-09-07 14:36:43 +00:00
$('#anfrage').submit(function(event){
event.preventDefault();
2022-09-07 14:39:11 +00:00
let form = $(this);
2022-09-07 14:04:33 +00:00
getInfoFromSelectedProduct().done(function(response, status){
let products = response.contents.map(product => {
product = getProductInfo(product);
product = calculatePrice(product);
2022-09-08 10:13:43 +00:00
return product.quantity+'x '+product.title+' ('+numberToEuroFormat(product.calc_preis)+')';
2022-09-07 14:04:33 +00:00
});
2022-09-08 11:14:40 +00:00
let cart = products.join(" - ")+" - Gesammt: "+numberToEuroFormat(totalPrice);
2022-09-08 10:13:43 +00:00
$('#ccart').val(cart);
2022-09-08 11:31:11 +00:00
$('#ctelefon').val(iti.getNumber());
2022-09-08 10:19:26 +00:00
objProducts = [];
localStorage.setItem("products", JSON.stringify(objProducts));
form.unbind('submit').submit();
2022-09-07 14:04:33 +00:00
});
2022-09-04 19:41:13 +00:00
});
2022-09-07 13:02:32 +00:00
$(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();
}
});
2022-09-07 10:46:44 +00:00
$(document).on('input', '.select-quantity', function(){
getInfoFromSelectedProduct().done(function(response, status){
totalPrice = 0;
response.contents.forEach(function(product){
let objProduct = getProductInfo(product);
2022-09-07 12:40:02 +00:00
objProduct = calculatePrice(objProduct);
2022-09-07 10:46:44 +00:00
totalPrice += objProduct.calc_preis*objProduct.quantity;
});
$('#total-checkout-price').text(numberToEuroFormat(totalPrice));
});
});
2022-09-07 12:50:07 +00:00
});
$(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);
}
});
});
2022-09-04 19:41:13 +00:00
});