Files
torebki-fabiola.pl/wp-content/plugins/woocommerce-dhl/assets/js/checkout.js
2026-03-05 13:07:40 +01:00

167 lines
4.6 KiB
JavaScript

(
function ( $ ) {
$( document ).on(
'change',
'#dhl_parcelshop',
function () {
jQuery( 'body' ).trigger( 'update_checkout' );
}
);
var dhl_select_type = dhl_checkout_js.select_type ? dhl_checkout_js.select_type : 'select2';
var dhl_lang = dhl_checkout_js.lang;
$( document ).on(
'click',
'#dhl-parcelshop-select-map',
function () {
var source = 'dhl';
var popup = $( '.js--popup-' + source );
if ( ! popup.length ) {
var map_url = new URL( dhl_checkout_js.map_url );
map_url.searchParams.append( 'options_pickup_cod', $( this ).attr( 'data-dhl_parcelshop_cod' ) );
map_url.searchParams.append( 'country', $( this ).attr( 'data-country' ) );
map_url.searchParams.append( 'postCode', $( this ).attr( 'data-postcode' ) );
map_url.searchParams.append( 'zoom', '1' );
var close_button = $( '<a></a>' ).attr( 'href', '#0' ).text( dhl_checkout_js.l10n.close_popup ).addClass( 'js--popup-close-button-' + source );
var iframe = $( '<iframe>' ).attr( 'id', source + '_map' ).attr( 'src', map_url )
popup = $( '<div></div>' ).attr( 'id', source + '_popup' ).addClass( 'js--popup-' + source ).append( close_button ).append( iframe );
$( '.js--popup-container-' + source ).html( popup );
}
popup.show();
return false;
}
);
$( document ).on(
'click',
'.js--popup-close-button-dhl',
function () {
$( '.js--popup-dhl' ).hide();
return false;
}
);
$( 'body' ).on(
'updated_checkout',
function () {
var _dhl_select = $( '#dhl_parcelshop' );
var containerClass = 'dhl-parcelshop-select-container';
var options = {
containerCssClass: containerClass,
};
if ( dhl_select_type === 'select2_ajax' ) {
let dhl_select = jQuery( '#dhl_parcelshop' );
options = {
containerCssClass: containerClass,
ajax: {
url: woocommerce_params.ajax_url,
dataType: 'json',
delay: 300,
type: 'POST',
data: function ( params ) {
return {
action: 'dhl_search_machines_via_ajax',
city: params.term,
postcode: dhl_select.data( 'postcode' ),
country: dhl_select.data( 'country' ),
security: dhl_checkout_js.ajax_nonce
};
},
processResults: function ( data ) {
return {
results: data.items,
};
},
cache: true,
},
minimumInputLength: 3,
escapeMarkup: function ( markup ) {
return markup;
},
placeholder: dhl_lang.placeholder,
language: {
inputTooShort: function ( args ) {
var remainingChars = args.minimum - args.input.length;
return dhl_lang.min_chars.replace( '%', remainingChars );
},
loadingMore: function () {
return dhl_lang.loadingMore;
},
noResults: function () {
return dhl_lang.noResults;
},
searching: function () {
return dhl_lang.searching;
},
errorLoading: function () {
return dhl_lang.errorLoading;
},
}
};
}
if ( dhl_select_type === 'select2' || dhl_select_type === 'select2_ajax' ) {
if ( $.fn.selectWoo ) {
_dhl_select.selectWoo( options );
} else if ( $.fn.select2 ) {
_dhl_select.select2( options );
}
}
}
);
}
)( jQuery );
function dhl_set_dhl_parcelshop( point ) {
var dhl_select_type = dhl_checkout_js.select_type ? dhl_checkout_js.select_type : 'select2';
let $dhl_parcelshop = jQuery( '#dhl_parcelshop' );
let country = $dhl_parcelshop.data( 'country' );
jQuery( '.js--popup-dhl' ).hide();
window.console.log( point );
let code = point.sap;
if ( 'PL' !== country ) {
code = point.sap + ':' + point.zip;
}
if ( dhl_select_type === 'select2' ) {
var exists = jQuery( "#dhl_parcelshop:has(option[value='" + code + "'])" ).length > 0;
if ( exists ) {
$dhl_parcelshop.val( code ).trigger( 'change' );
} else {
alert( dhl_checkout_js.parcelshop_not_exists );
}
} else if ( dhl_select_type === 'select2_ajax' ) {
var data = {
action: 'dhl_search_machines_via_ajax',
code: code,
postcode: point.zip,
city: point.city,
country: jQuery( '#dhl-parcelshop-select-map' ).attr( 'data-country' ),
security: dhl_checkout_js.ajax_nonce
};
jQuery.post(
woocommerce_params.ajax_url,
data,
function ( response ) {
if ( typeof response.id !== 'undefined' ) {
$dhl_parcelshop.html( '<option selected value="' + response.id + '">' + response.text + '</option>' );
jQuery( 'body' ).trigger( 'update_checkout' );
} else {
alert( dhl_checkout_js.parcelshop_not_exists );
}
}
);
}
}