Plugin Directory

Changeset 3324913


Ignore:
Timestamp:
07/09/2025 10:41:57 AM (9 months ago)
Author:
inpostitaly
Message:

Version 1.2.0

Location:
inpost-italy/trunk
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • inpost-italy/trunk/inpost-italy.php

    r3195026 r3324913  
    44    Plugin URI: https://inpost.it/soluzioni-le-aziende
    55    Description: Invia, crea e stampa Spedizioni InPost attraverso il nostro Plugin ufficiale.
    6     Version: 1.1.9
     6    Version: 1.2.0
    77    Author: InPost
    88    Author URI: https://inpost.it/
     
    1111    Text Domain: inpost-italy
    1212    Domain Path: /languages/
    13     Tested up to: 6.7
     13    Tested up to: 6.8
    1414
    1515    InPost Italy for Woocommerce is free software: you can redistribute it and/or modify
     
    3939define( 'INPOST_ITALY_PLUGIN_FILE', __FILE__ );
    4040define( 'INPOST_ITALY_PLUGIN_DIR', __DIR__ );
    41 define( 'INPOST_ITALY_PLUGIN_VERSION', '1.1.9' );
     41define( 'INPOST_ITALY_PLUGIN_VERSION', '1.2.0' );
    4242
    4343add_action(
    44     'init',
     44    'after_setup_theme',
    4545    function () {
    4646        $res = load_plugin_textdomain(
  • inpost-italy/trunk/readme.txt

    r3195026 r3324913  
    33Tags: inpost, italia, etichetta, armadietto per pacchi
    44Requires at least: 5.3
    5 Tested up to: 6.7
     5Tested up to: 6.8
    66Requires PHP: 7.4
    7 Stable tag: 1.1.9
     7Stable tag: 1.2.0
    88License: GPLv3
    99License URI: https://www.gnu.org/licenses/gpl-3.0.html
     
    8181== Changelog ==
    8282
     83= 1.2.0 =
     84
     85* Fix open map location
     86
    8387= 1.1.9 =
    8488
  • inpost-italy/trunk/resources/assets/css/front.css

    r3121177 r3324913  
    9999    margin-top: 10px;
    100100    border-radius: 7px;
     101    margin-bottom: 20px;
    101102}
    102103
  • inpost-italy/trunk/resources/assets/js/debug-map.js

    r3121177 r3324913  
     1let inpost_italy_coords                    = [41.898386, 12.516985];
     2let inpost_italy_zip_val                   = null;
     3let inpost_it_is_ajax_get_point_running = false;
     4
    15function selectPointCallback(point) {
    26    parcelMachineAddressDesc = point.address.line1;
     
    7276    };
    7377
     78    let zip  = jQuery( '#shipping_postcode' );
     79    let zip2 = jQuery( '#billing_postcode' );
     80
     81    if ( typeof zip != 'undefined' && zip !== null ) {
     82        jQuery( zip ).on(
     83            'change',
     84            function () {
     85                let current_zip = jQuery( this ).val().trim();
     86                if ( ! current_zip ) {
     87                    return;
     88                }
     89                inpost_italy_get_map_coords( current_zip );
     90            }
     91        );
     92        jQuery( zip2 ).on(
     93            'change',
     94            function () {
     95                let current_zip = jQuery( this ).val().trim();
     96                if ( ! current_zip ) {
     97                    return;
     98                }
     99                inpost_italy_get_map_coords( current_zip );
     100            }
     101        );
     102
     103    }
     104
    74105    let method = jQuery('input[name^="shipping_method"]:checked').val();
    75106    let postfix = '';
     
    107138            }
    108139        }, {width: window.innerWidth, height: window.innerHeight});
     140
     141        inp_waitForElm('.types-list').then((elm) => {
     142            window.mapController.setCenterFromArray(inpost_italy_coords);
     143        });
     144
    109145        return true;
    110146    });
    111147
    112     waitForElm('.types-list').then((elm) => {
     148    inp_waitForElm('.types-list').then((elm) => {
    113149        let geo_location_button = document.querySelector('#custom-my-location');
    114150        if( 'undefined' === typeof geo_location_button || null === geo_location_button ) {
     
    123159                        longit = position.coords.longitude;
    124160                        window.mapController.setCenterFromArray([latit, longit]);
    125                         //window.mapController.setCenterFromArray([41.1171432, 16.8718715]);
    126161                    })
    127162                }
    128163            };
     164
     165            window.mapController.setCenterFromArray(inpost_italy_coords);
    129166
    130167            var firstChild = document.createElement('button');
     
    181218            }
    182219        }, {width: window.innerWidth, height: window.innerHeight});
     220
     221        inp_waitForElm('.types-list').then((elm) => {
     222            window.mapController.setCenterFromArray(inpost_italy_coords);
     223        });
     224
    183225        return true;
    184226    }
     
    204246
    205247
    206 function waitForElm(selector) {
     248function inp_waitForElm(selector) {
    207249    return new Promise(resolve => {
    208250        if (document.querySelector(selector)) {
     
    221263    });
    222264}
     265
     266
     267function inpost_italy_get_map_coords(zip) {
     268
     269    console.log('Inpost Italy, serch address: ' + zip );
     270
     271    let street = jQuery( '#shipping_address_1' );
     272    let city   = jQuery( '#shipping_city' );
     273
     274
     275    if ( ! street ) {
     276        street = jQuery( '#billing_address_1' );
     277    }
     278
     279    if ( ! city ) {
     280        city = jQuery( '#billing_city' );
     281    }
     282
     283    if (typeof street != 'undefined' && street !== null && typeof city != 'undefined' && city !== null ) {
     284
     285        if ( jQuery( city ).val() ) {
     286            if ( zip === inpost_italy_zip_val ) {
     287                return;
     288            }
     289
     290            // stop if ajax is already running.
     291            if (inpost_it_is_ajax_get_point_running) {
     292                return;
     293            }
     294
     295            let address = zip + ', Italy';
     296            /*if ( jQuery( street ).val() ) {
     297                address = jQuery( city ).val() + ',' + jQuery( street ).val() + ',' + zip;
     298            } else {
     299                address = jQuery( city ).val() + ', Italy';
     300            }*/
     301
     302            xhr = jQuery.ajax(
     303                {
     304                    data: {
     305                        action: 'inpost_italy_get_coords',
     306                        get_coord_address: address,
     307                        security: easypack_front_map.nonce
     308                    },
     309                    type: 'POST',
     310                    url: easypack_front_map.ajax_url,
     311                    beforeSend: function () {
     312                        inpost_it_is_ajax_get_point_running = true;
     313                        console.log( 'Search address:' );
     314                        console.log( address );
     315                    },
     316                    complete: function () {
     317                        console.log( 'Complete' );
     318                        inpost_italy_zip_val                   = zip;
     319                        inpost_it_is_ajax_get_point_running = false;
     320                    },
     321                    error: function (jqXHR, textStatus, errorThrown) {
     322                        console.log( 'retrieve_coord_error' );
     323                        console.log( 'error: ' + jqXHR.status );
     324                        inpost_it_is_ajax_get_point_running = false;
     325                        return false;
     326                    },
     327                    success: function (data, textStatus, jqXHR) {
     328
     329                        if (data) {
     330                            let obj = JSON.parse( data );
     331
     332                            if ( obj.hasOwnProperty( 'formatted_address' ) ) {
     333                                console.log( 'Found address:' );
     334                                console.log( obj.formatted_address );
     335                            }
     336
     337                            if ( obj.hasOwnProperty( 'lat' ) && obj.hasOwnProperty( 'lon' ) ) {
     338                                inpost_italy_coords = [obj.lat, obj.lon];
     339                            }
     340
     341                        }
     342                    }
     343                }
     344            );
     345        }
     346    }
     347}
  • inpost-italy/trunk/resources/assets/js/front-blocks.js

    r3153131 r3324913  
    1 var geowidgetSelectedPointItaly;
     1let geowidgetSelectedPointItaly;
     2let inpost_italy_blocks_coords                    = [41.898386, 12.516985];
     3let inpost_italy_blocks_ajax_get_point_running = false;
     4let inpost_italy_blocks_address = '';
     5let inpost_italy_blocks_zip_val = null;
     6
     7
     8function inpost_italy_validate_parcel_machine_for_gpay_block() {
     9
     10    let selected_shipping_radio = document.querySelector( '#shipping-option .wc-block-components-radio-control__input:checked' );
     11    if (selected_shipping_radio !== undefined && selected_shipping_radio !== null) {
     12        let id = selected_shipping_radio.value;
     13        console.log( 'Shipping method ID:', id );
     14
     15        if ( id.indexOf( 'easypack_italy_parcel_machines' ) !== -1 ) {
     16            let hidden_input = document.getElementById( 'inpost-parcel-locker-id' );
     17            if ( hidden_input !== undefined && hidden_input !== null ) {
     18                let locker_id = hidden_input.value;
     19                console.log( 'Locker ID:', locker_id );
     20
     21                if (locker_id.trim() === '') {
     22                    return false;
     23                }
     24            }
     25        }
     26    }
     27
     28    return true;
     29}
     30
     31function inpost_italy_get_shipping_method_block() {
     32    let data                = {};
     33    let shipping_block_html = jQuery( '.wc-block-components-shipping-rates-control' );
     34    if (typeof shipping_block_html != 'undefined' && shipping_block_html !== null) {
     35        let shipping_radio_buttons = jQuery( shipping_block_html ).find( 'input[name^="radio-control-"]' );
     36        if ( shipping_radio_buttons.length > 0 ) {
     37            let method                  = jQuery( 'input[name^="radio-control-"]:checked' ).val();
     38            let ship_method_instance_id = '';
     39            if ('undefined' == typeof method || null === method) {
     40                method = jQuery( 'input[name^="radio-control-"]' ).val();
     41            }
     42
     43            if (typeof method != 'undefined' && method !== null) {
     44                if (method.indexOf( ':' ) > -1) {
     45                    let arr                 = method.split( ':' );
     46                    method                  = arr[0];
     47                    ship_method_instance_id = arr[1];
     48                }
     49            }
     50            data.method      = method;
     51            data.instance_id = ship_method_instance_id;
     52        }
     53    }
     54
     55    return data;
     56}
     57
     58
     59function inpost_italy_open_modal() {
     60    document.getElementById( 'inpost_italy_checkout_validation_modal' ).style.display = 'flex';
     61}
     62
     63function inpost_italy_close_modal() {
     64    document.getElementById( 'inpost_italy_checkout_validation_modal' ).style.display = 'none';
     65
     66    // Scroll to map button.
     67    let scrollToElement = document.getElementById( 'easypack_italy_geowidget' );
     68
     69    if (scrollToElement) {
     70        scrollToElement.scrollIntoView( {behavior: 'smooth' } );
     71    }
     72
     73}
     74
     75function inpost_italy_delay_on_typein(callback, ms) {
     76    var timer = 0;
     77    return function() {
     78        var context = this, args = arguments;
     79        clearTimeout(timer);
     80        timer = setTimeout(function () {
     81            callback.apply(context, args);
     82        }, ms || 0);
     83    };
     84}
     85
     86
     87function inpost_italy_wait_element(selector) {
     88    return new Promise(
     89        resolve => {
     90            if (document.querySelector( selector )) {
     91                return resolve( document.querySelector( selector ) );
     92            }
     93            const observer = new MutationObserver(
     94                mutations => {
     95                    if (document.querySelector( selector )) {
     96                        resolve( document.querySelector( selector ) );
     97                        observer.disconnect();
     98                    }
     99                }
     100            );
     101            observer.observe(
     102                document.body,
     103                {
     104                    childList: true,
     105                    subtree: true
     106                }
     107            );
     108        }
     109    );
     110}
     111
     112
     113
     114function inpost_italy_wc_block_get_coords(address) {
     115    console.log('Inpost Italy, serch address: ' + address);
     116    xhr = jQuery.ajax({
     117        data: {
     118            action: 'inpost_italy_get_coords',
     119            get_coord_address: address,
     120            security: easypack_blocks.nonce
     121        },
     122        type: 'POST',
     123        url: easypack_blocks.ajax_url,
     124        beforeSend: function () {
     125            inpost_italy_blocks_ajax_get_point_running = true;
     126        },
     127        complete: function () {
     128            console.log('Complete address location');
     129            let current_zip = jQuery('#shipping-postcode').val();
     130            if( current_zip ) {
     131                zip_val = current_zip;
     132            }
     133            inpost_italy_blocks_ajax_get_point_running = false;
     134        },
     135        error: function (jqXHR, textStatus, errorThrown) {
     136            console.log('retrieve_coord_error');
     137            console.log('error: ' + jqXHR.status);
     138            inpost_italy_blocks_ajax_get_point_running = false;
     139            return false;
     140        },
     141        success: function (data, textStatus, jqXHR) {
     142            if (data) {
     143                let obj = JSON.parse(data);
     144                if( obj.hasOwnProperty('formatted_address') ) {
     145                    console.log('Found address:');
     146                    console.log(obj.formatted_address);
     147                }
     148                if( obj.hasOwnProperty('lat') && obj.hasOwnProperty('lon') ) {
     149                    inpost_italy_blocks_coords = [obj.lat, obj.lon];
     150                }
     151            }
     152        }
     153    });
     154}
    2155
    3156function inpost_italy_change_react_input(input,value){
     
    19172    let selected_point_data = '';
    20173    let InpostItalyPointObject;
     174    let address_details = '';
     175
     176    if( 'address_details' in point ) {
     177        if( 'city' in point.address_details ) {
     178            address_details += '<br>' + point.address_details.city;
     179        }
     180        if( 'post_code' in point.address_details ) {
     181            address_details += ' ' + point.address_details.post_code;
     182        }
     183    }
    21184
    22185    inpost_italy_change_react_input( document.getElementById( 'inpost-italy-parcel-locker-id' ), point.name );
     
    25188        selected_point_data = '<div class="easypack_selected_point_data" id="easypack_selected_point_data">\n'
    26189            + '<div id="selected-parcel-machine-id">' + point.name + '</div>\n'
    27             + '<span id="selected-parcel-machine-desc">' + point.address.line1 + '</span></div>\n';
     190            + '<span id="selected-parcel-machine-desc">' + point.address.line1 + address_details + '</span></div>\n';
    28191    } else {
    29192        selected_point_data = '<div class="easypack_selected_point_data" id="easypack_selected_point_data">\n'
    30193            + '<div id="selected-parcel-machine-id">' + point.name + '</div>\n'
    31             + '<span id="selected-parcel-machine-desc">' + point.address.line1 + '</span>\n'
     194            + '<span id="selected-parcel-machine-desc">' + point.address.line1 + address_details + '</span>\n'
    32195            + '<span id="selected-parcel-machine-desc1">(' + point.location_description + ')</span></div>\n';
    33196    }
     
    36199    jQuery( '#easypack_selected_point_data_wrap' ).show();
    37200
    38     if ( point.location_description ) {
    39         InpostItalyPointObject = { 'pointName': point.name, 'pointDesc': point.address.line1, 'pointAddDesc': point.location_description };
    40     } else {
    41         InpostItalyPointObject = { 'pointName': point.name, 'pointDesc': point.address.line1, 'pointAddDesc': '' };
    42     }
    43     // Put the object into storage.
    44     localStorage.setItem( 'InpostItalyPointObject', JSON.stringify( InpostItalyPointObject ) );
     201
    45202
    46203    geowidgetSelectedPointItaly = selected_point_data;
    47204    // remove map div - so it has to render again.
    48205    jQuery( '#widget-modal' ).parent( 'div' ).remove();
     206
     207    let lat = '41.898386';
     208    let lon = '12.516985';
     209
     210    if( point.hasOwnProperty('location') ) {
     211        if( point.location.hasOwnProperty('latitude') && point.location.hasOwnProperty('longitude') ) {
     212            inpost_italy_blocks_coords = [point.location.latitude, point.location.longitude];
     213            lat = point.location.latitude;
     214            lon = point.location.longitude;
     215        }
     216    }
     217
     218    if ( point.location_description ) {
     219        InpostItalyPointObject = {
     220            'pointName': point.name,
     221            'pointDesc': point.address.line1 + address_details,
     222            'lat': lat,
     223            'lon': lon,
     224            'pointAddDesc': point.location_description
     225        };
     226    } else {
     227        InpostItalyPointObject = {
     228            'pointName': point.name,
     229            'pointDesc': point.address.line1 + address_details,
     230            'lat': lat,
     231            'lon': lon,
     232            'pointAddDesc': ''
     233        };
     234    }
     235    // Put the object into storage.
     236    localStorage.setItem( 'InpostItalyPointObject', JSON.stringify( InpostItalyPointObject ) );
    49237
    50238}
     
    61249    if (typeof pointData.pointName != 'undefined' && pointData.pointName !== null) {
    62250        point = pointData.pointName;
     251    }
     252
     253    if( typeof pointData.lat != 'undefined' && pointData.lat !== null ) {
     254        if( typeof pointData.lon != 'undefined' && pointData.lon !== null ) {
     255            inpost_italy_blocks_coords  = [pointData.lat, pointData.lon];
     256        }
    63257    }
    64258
     
    100294jQuery( document ).ready(
    101295    function () {
     296
     297        console.log('InPost IT: front-blocks.js');
     298
    102299        window.easyPackAsyncInit = function () {
    103300            easyPack.init(
     
    172369        document.getElementById( 'inp_it_close_modal_button' ).addEventListener( 'click', inpost_italy_close_modal );
    173370
     371
     372        let address_exists = false;
     373        let shipping_country = '';
     374
     375        let wc_block_data = window.wcSettings;
     376        if (typeof wc_block_data != 'undefined' && wc_block_data !== null) {
     377            if (wc_block_data?.checkoutData?.shipping_address?.postcode != null) {
     378                inpost_italy_blocks_address = wc_block_data.checkoutData.shipping_address.postcode;
     379                address_exists = true;
     380            }
     381            if (wc_block_data?.checkoutData?.shipping_address?.city != null) {
     382                inpost_italy_blocks_address += ' ' +  wc_block_data.checkoutData.shipping_address.city + ', Italy';
     383            }
     384            if (wc_block_data?.checkoutData?.shipping_address?.country != null) {
     385                shipping_country = wc_block_data.checkoutData.shipping_address.country;
     386            }
     387
     388        }
     389
    174390        setTimeout(
    175391            function () {
    176392
     393                console.log('Inpost IT: inpost_italy_blocks_address');
     394                console.log(inpost_italy_blocks_address);
     395                //console.log('Inpost IT: checkoutData');
     396                //console.log(wc_block_data.checkoutData);
     397
     398                if( address_exists ) {
     399                    if( 'IT' === shipping_country ) {
     400                        inpost_italy_wc_block_get_coords(inpost_italy_blocks_address);
     401                    }
     402                }
     403
     404
     405                let street = jQuery('#shipping-address_1');
     406                let zip = jQuery('#shipping-postcode');
     407                let city = jQuery('#shipping-city');
     408
     409                if ( typeof zip != 'undefined' && zip !== null ) {
     410                    document.addEventListener( 'keyup', inpost_italy_delay_on_typein(function (e) {
     411                        e = e || window.event;
     412                        let target = e.target || e.srcElement;
     413
     414                        if ( target.hasAttribute( 'id' ) ) {
     415                            if( 'shipping-postcode' === target.getAttribute( 'id' ) || 'shipping-city' === target.getAttribute( 'id' ) ) {
     416                                e.preventDefault();
     417                                let zip = target.value;
     418                                if ( zip === inpost_italy_blocks_zip_val ) {
     419                                    return;
     420                                }
     421                                if (inpost_italy_blocks_ajax_get_point_running) {
     422                                    return;
     423                                }
     424
     425                                //inpost_italy_blocks_address = jQuery(city).val() + ',' + zip + ', Italy';
     426                                inpost_italy_blocks_address =  zip + ', Italy';
     427
     428                                shipping_country = jQuery('#shipping-country').val();
     429                                if ('IT' === shipping_country) {
     430                                    inpost_italy_wc_block_get_coords(inpost_italy_blocks_address);
     431                                }
     432                            }
     433
     434                        }
     435                    }, 2000 ), false );
     436
     437                }
     438
     439
    177440                let map_button = jQuery( '#easypack_italy_geowidget' );
    178441
    179                 if (typeof map_button != 'undefined' && map_button !== null) {
     442                if ( typeof map_button != 'undefined' && map_button !== null ) {
    180443                    jQuery( '#shipping-phone' ).prop( 'required', true );
    181444                    jQuery( 'label[for="shipping-phone"]' ).text( 'Telefono (richiesto)' );
     
    223486
    224487            },
    225             1000
     488            1600
    226489        );
    227490
     
    232495            controlDiv.classList.add( 'custom-my-location' );
    233496            controlDiv.id                    = 'custom-my-location';
     497
     498            window.mapController.setCenterFromArray( inpost_italy_blocks_coords );
    234499            var clickHandler                 = function () {
    235500                    if (navigator.geolocation) {
     
    239504                                longit = position.coords.longitude;
    240505                                window.mapController.setCenterFromArray( [latit, longit] );
    241                                 // window.mapController.setCenterFromArray([41.1171432, 16.8718715]);.
    242506                            }
    243507                        )
     
    276540            }
    277541        );
     542
     543
     544        jQuery("#easypack_italy_geowidget").click(function (e) {
     545            e.preventDefault();
     546            console.log('Inpost IT jQuery: coords');
     547            console.log(inpost_italy_blocks_coords);
     548            //console.log(easyPack.modalMap);
     549
     550            jQuery( '.easypack_selected_point_data' ).each(
     551                function (ind, elem) {
     552                    jQuery( elem ).remove();
     553                }
     554            );
     555
     556            easyPack.modalMap(function (point, modal) {
     557                modal.closeModal();
     558                console.log('Inpost IT: locker data');
     559                console.log(point);
     560
     561                if (point) {
     562                    jQuery("#easypack_italy_geowidget").text(easypack_blocks.button_text2);
     563                    inpost_italy_select_point_callback(point);
     564                }
     565            }, {width: window.innerWidth, height: window.innerHeight});
     566
     567            inp_waitForElm('.types-list').then((elm) => {
     568                window.mapController.setCenterFromArray(inpost_italy_blocks_coords);
     569            });
     570
     571            return true;
     572        });
     573
     574
     575
     576        document.addEventListener(
     577            'click',
     578            function (e) {
     579                e          = e || window.event;
     580                var target = e.target || e.srcElement;
     581
     582                if (target.hasAttribute( 'id' ) && target.getAttribute( 'id' ) === 'easypack-search') {
     583                    let search_was_started = false;
     584                    let mapSearchNoticeModal;
     585                    const search_input_x = document.querySelector( '.search-input' );
     586                    search_input_x.addEventListener(
     587                        "search",
     588                        function (event) {
     589                            window.mapController.setCenterFromArray( inpost_italy_blocks_coords );
     590                        }
     591                    );
     592                    target.addEventListener(
     593                        'keyup',
     594                        function () {
     595                            if (this.value.length > 0) {
     596                                search_was_started = true;
     597                            }
     598                            if (search_was_started && this.value.length < 1) {
     599                                // return map to Rome position.
     600                                window.mapController.setCenterFromArray( inpost_italy_blocks_coords );
     601                            }
     602                        }
     603                    );
     604                }
     605
     606                if ( target.hasAttribute( 'id' ) ) {
     607                    if (target.getAttribute( 'id' ) === 'easypack_italy_geowidget') {
     608                        e.preventDefault();
     609
     610                        //console.log('Inpost IT JS: coords');
     611                        //console.log(inpost_italy_blocks_coords);
     612
     613                        jQuery( '.easypack_selected_point_data' ).each(
     614                            function (ind, elem) {
     615                                jQuery( elem ).remove();
     616                            }
     617                        );
     618                        easyPack.modalMap(
     619                            function (point, modal) {
     620                                modal.closeModal();
     621                                if (point) {
     622                                    jQuery( "#easypack_italy_geowidget" ).text( easypack_blocks.button_text2 );
     623                                    inpost_italy_select_point_callback( point );
     624                                }
     625                            },
     626                            {width: window.innerWidth, height: window.innerHeight}
     627                        );
     628
     629                        inpost_italy_wait_element('.types-list').then((elm) => {
     630                            window.mapController.setCenterFromArray(inpost_italy_blocks_coords);
     631                        });
     632
     633                        return true;
     634                    }
     635                }
     636
     637                if ( target.classList.contains( 'wc-block-components-checkout-place-order-button' )
     638                    || target.classList.contains( 'wc-block-checkout__actions_row' ) ) {
     639
     640                    let reactjs_input       = document.getElementById( 'inpost-italy-parcel-locker-id' );
     641                    let reactjs_input_lalue = false;
     642                    if (typeof reactjs_input != 'undefined' && reactjs_input !== null) {
     643                        reactjs_input_lalue = reactjs_input.value;
     644                        if ( ! reactjs_input_lalue ) {
     645                            inpost_italy_open_modal();
     646                        }
     647                    }
     648                }
     649
     650                if ( target.classList.contains( 'wc-block-components-button__text' ) ) {
     651                    let parent = target.parentNode;
     652                    if ( parent.classList.contains( 'wc-block-components-checkout-place-order-button' ) ) {
     653                        let reactjs_input       = document.getElementById( 'inpost-italy-parcel-locker-id' );
     654                        let reactjs_input_lalue = false;
     655                        if (typeof reactjs_input != 'undefined' && reactjs_input !== null) {
     656                            reactjs_input_lalue = reactjs_input.value;
     657                            if ( ! reactjs_input_lalue ) {
     658                                inpost_italy_open_modal();
     659                            }
     660                        }
     661                    }
     662                }
     663            }
     664        );
     665
     666
    278667    }
    279668);
    280669
    281670
    282 function inpost_italy_open_modal() {
    283     document.getElementById( 'inpost_italy_checkout_validation_modal' ).style.display = 'flex';
    284 }
    285 
    286 function inpost_italy_close_modal() {
    287     document.getElementById( 'inpost_italy_checkout_validation_modal' ).style.display = 'none';
    288 
    289     // Scroll to map button.
    290     let scrollToElement = document.getElementById( 'easypack_italy_geowidget' );
    291 
    292     if (scrollToElement) {
    293         scrollToElement.scrollIntoView( {behavior: 'smooth' } );
    294     }
    295 
    296 }
    297 
    298 
    299 function inpost_italy_wait_element(selector) {
    300     return new Promise(
    301         resolve => {
    302             if (document.querySelector( selector )) {
    303                 return resolve( document.querySelector( selector ) );
    304             }
    305             const observer = new MutationObserver(
    306             mutations => {
    307                     if (document.querySelector( selector )) {
    308                         resolve( document.querySelector( selector ) );
    309                         observer.disconnect();
    310                     }
    311             }
    312         );
    313         observer.observe(
    314             document.body,
    315             {
    316                 childList: true,
    317                 subtree: true
    318             }
    319         );
    320         }
    321     );
    322 }
    323 
    324 
    325 document.addEventListener(
    326     'click',
    327     function (e) {
    328         e          = e || window.event;
    329         var target = e.target || e.srcElement;
    330 
    331         if (target.hasAttribute( 'id' ) && target.getAttribute( 'id' ) === 'easypack-search') {
    332             let search_was_started = false;
    333             let mapSearchNoticeModal;
    334             const search_input_x = document.querySelector( '.search-input' );
    335             search_input_x.addEventListener(
    336                 "search",
    337                 function (event) {
    338                     window.mapController.setCenterFromArray( [41.898386, 12.516985] );
    339                 }
    340             );
    341             target.addEventListener(
    342                 'keyup',
    343                 function () {
    344                     if (this.value.length > 0) {
    345                         search_was_started = true;
    346                     }
    347                     if (search_was_started && this.value.length < 1) {
    348                         // return map to Rome position.
    349                         window.mapController.setCenterFromArray( [41.898386, 12.516985] );
    350                     }
    351                 }
    352             );
    353         }
    354 
    355         if ( target.hasAttribute( 'id' ) ) {
    356             if (target.getAttribute( 'id' ) === 'easypack_italy_geowidget') {
    357                 e.preventDefault();
    358                 jQuery( '.easypack_selected_point_data' ).each(
    359                     function (ind, elem) {
    360                         jQuery( elem ).remove();
    361                     }
    362                 );
    363                 easyPack.modalMap(
    364                     function (point, modal) {
    365                         modal.closeModal();
    366                         if (point) {
    367                             jQuery( "#easypack_italy_geowidget" ).text( easypack_blocks.button_text2 );
    368                             inpost_italy_select_point_callback( point );
    369                         }
    370                     },
    371                     {width: window.innerWidth, height: window.innerHeight}
    372                 );
    373                 return true;
    374             }
    375         }
    376 
    377         if ( target.classList.contains( 'wc-block-components-checkout-place-order-button' )
    378             || target.classList.contains( 'wc-block-checkout__actions_row' ) ) {
    379 
    380             let reactjs_input       = document.getElementById( 'inpost-italy-parcel-locker-id' );
    381             let reactjs_input_lalue = false;
    382             if (typeof reactjs_input != 'undefined' && reactjs_input !== null) {
    383                 reactjs_input_lalue = reactjs_input.value;
    384                 if ( ! reactjs_input_lalue ) {
    385                     inpost_italy_open_modal();
    386                 }
    387             }
    388         }
    389 
    390         if ( target.classList.contains( 'wc-block-components-button__text' ) ) {
    391             let parent = target.parentNode;
    392             if ( parent.classList.contains( 'wc-block-components-checkout-place-order-button' ) ) {
    393                 let reactjs_input       = document.getElementById( 'inpost-italy-parcel-locker-id' );
    394                 let reactjs_input_lalue = false;
    395                 if (typeof reactjs_input != 'undefined' && reactjs_input !== null) {
    396                     reactjs_input_lalue = reactjs_input.value;
    397                     if ( ! reactjs_input_lalue ) {
    398                         inpost_italy_open_modal();
    399                     }
    400                 }
    401             }
    402         }
    403     }
    404 );
     671
     672
     673
  • inpost-italy/trunk/resources/assets/js/front.js

    r3121177 r3324913  
     1let inpost_italy_coords                    = [41.898386, 12.516985];
     2let inpost_italy_zip_val                   = null;
     3let inpost_it_is_ajax_get_point_running = false;
     4
    15jQuery(document).ready(function() {
     6
     7    console.log('InPost IT: front.js');
     8
     9    let zip  = jQuery( '#shipping_postcode' );
     10    let zip2 = jQuery( '#billing_postcode' );
     11
     12    if ( typeof zip != 'undefined' && zip !== null ) {
     13        jQuery( zip ).on(
     14            'change',
     15            function () {
     16                let current_zip = jQuery( this ).val().trim();
     17                if ( ! current_zip ) {
     18                    return;
     19                }
     20                inpost_italy_get_map_coords( current_zip );
     21            }
     22        );
     23        jQuery( zip2 ).on(
     24            'change',
     25            function () {
     26                let current_zip = jQuery( this ).val().trim();
     27                if ( ! current_zip ) {
     28                    return;
     29                }
     30                inpost_italy_get_map_coords( current_zip );
     31            }
     32        );
     33
     34    }
    235
    336    function selectPointCallback(point) {
     
    538
    639        let selected_point_data = '';
     40        let address_details = '';
     41
     42        if( 'address_details' in point ) {
     43            if( 'city' in point.address_details ) {
     44                address_details += '<br>' + point.address_details.city;
     45            }
     46            if( 'post_code' in point.address_details ) {
     47                address_details += ' ' + point.address_details.post_code;
     48            }
     49        }
    750
    851        if('undefined' == typeof point.location_description) {
    952            selected_point_data = '<div class="easypack_selected_point_data" id="easypack_selected_point_data">\n'
    1053                + '<div id="selected-parcel-machine-id">' + point.name + '</div>\n'
    11                 + '<span id="selected-parcel-machine-desc">' + point.address.line1 + '</span>'
     54                + '<span id="selected-parcel-machine-desc">' + point.address.line1 + address_details + '</span>'
    1255                + '<input type="hidden" id="parcel_machine_id"\n'
    1356                + ' name="parcel_machine_id" class="parcel_machine_id" value="'+point.name+'"/>\n'
     
    1760            selected_point_data = '<div class="easypack_selected_point_data" id="easypack_selected_point_data">\n'
    1861                + '<div id="selected-parcel-machine-id">' + point.name + '</div>\n'
    19                 + '<span id="selected-parcel-machine-desc">' + point.address.line1 + '</span>\n'
     62                + '<span id="selected-parcel-machine-desc">' + point.address.line1 + address_details + '</span>\n'
    2063                + '<span id="selected-parcel-machine-desc1">' + '(' + point.location_description + ')</span>'
    2164                + '<input type="hidden" id="parcel_machine_id"\n'
     
    2669        jQuery('#easypack_italy_geowidget').after(selected_point_data);
    2770
    28         if( point.location_description ) {
    29             InpostItalyPointObject = { 'pointName': point.name, 'pointDesc': point.address.line1, 'pointAddDesc': point.location_description };
    30         } else {
    31             InpostItalyPointObject = { 'pointName': point.name, 'pointDesc': point.address.line1, 'pointAddDesc': '' };
    32         }
    33         // Put the object into storage
    34         localStorage.setItem('InpostItalyPointObject', JSON.stringify(InpostItalyPointObject));
     71
    3572
    3673        // remove map div - so it has to render again
    3774        jQuery('#widget-modal').parent('div').remove();
     75
     76        let lat = '41.898386';
     77        let lon = '12.516985';
     78
     79        if ( point.hasOwnProperty( 'location' ) ) {
     80            if ( point.location.hasOwnProperty( 'latitude' ) && point.location.hasOwnProperty( 'longitude' ) ) {
     81                inpost_italy_coords = [point.location.latitude, point.location.longitude];
     82                lat = point.location.latitude;
     83                lon = point.location.longitude;
     84            }
     85        }
     86
     87        if( point.location_description ) {
     88            InpostItalyPointObject = {
     89                'pointName': point.name,
     90                'pointDesc': point.address.line1 + address_details,
     91                'lat': lat,
     92                'lon': lon,
     93                'pointAddDesc': point.location_description
     94            };
     95        } else {
     96            InpostItalyPointObject = {
     97                'pointName': point.name,
     98                'pointDesc': point.address.line1 + address_details,
     99                'lat': lat,
     100                'lon': lon,
     101                'pointAddDesc': ''
     102            };
     103        }
     104        // Put the object into storage.
     105        localStorage.setItem('InpostItalyPointObject', JSON.stringify(InpostItalyPointObject));
    38106
    39107    }
     
    43111        jQuery('#easypack_selected_point_data').remove();
    44112    });
     113
     114
    45115
    46116    jQuery( document.body ).on('updated_checkout', function() {
     
    101171                    let point,
    102172                        selected_point_data,
     173                        lot,
     174                        lat,
    103175                        desc;
    104176
    105177                    let pointData = JSON.parse(InpostItalyPointObject);
     178
     179                    if( typeof pointData.lat != 'undefined' && pointData.lat !== null ) {
     180                        if( typeof pointData.lon != 'undefined' && pointData.lon !== null ) {
     181                            inpost_italy_coords  = [pointData.lat, pointData.lon];
     182                        }
     183                    }
    106184
    107185                    if( typeof pointData.pointName != 'undefined' && pointData.pointName !== null ) {
     
    132210                        jQuery('#easypack_italy_geowidget').after(selected_point_data);
    133211                        jQuery("#easypack_italy_geowidget").text(easypack_front_map.button_text2);
     212
    134213                    }
    135214                }
     
    152231            jQuery("#easypack_italy_geowidget").click(function (e) {
    153232                e.preventDefault();
     233                console.log('Inpost IT: coords');
     234                console.log(inpost_italy_coords);
    154235
    155236                jQuery('#easypack_selected_point_data').remove();
     
    157238                easyPack.modalMap(function (point, modal) {
    158239                    modal.closeModal();
     240                    console.log('Inpost IT: locker data');
     241                    console.log(point);
    159242
    160243                    if (point) {
     
    163246                    }
    164247                }, {width: window.innerWidth, height: window.innerHeight});
     248
     249                inp_waitForElm('.types-list').then((elm) => {
     250                    window.mapController.setCenterFromArray(inpost_italy_coords);
     251                });
    165252
    166253                return true;
     
    176263
    177264        var clickHandler = function() {
    178 
    179265            if (navigator.geolocation) {
    180266                navigator.geolocation.getCurrentPosition(function (position) {
     
    182268                    longit = position.coords.longitude;
    183269                    window.mapController.setCenterFromArray([latit, longit]);
    184                     //window.mapController.setCenterFromArray([41.1171432, 16.8718715]);
    185270                })
    186 
    187271            }
    188272        };
     273
     274        window.mapController.setCenterFromArray(inpost_italy_coords);
    189275
    190276        var firstChild = document.createElement('button');
     
    223309    });
    224310
     311
     312    document.addEventListener(
     313        'change',
     314        function (e) {
     315            e          = e || window.event;
     316            var target = e.target;
     317            if (target.hasAttribute( 'name' )) {
     318                if (target.getAttribute('name') === 'shipping_method[0]') {
     319                    console.log('change shipping method');
     320
     321                    let method = jQuery('input[name^="shipping_method"]:checked').val();
     322                    let postfix = '';
     323
     324                    if('undefined' == typeof method || null === method ) {
     325                        method = jQuery('input[name^="shipping_method"]').val();
     326                    }
     327
     328                    if(typeof method != 'undefined' && method !== null) {
     329                        if (method.indexOf(':') > -1) {
     330                            let arr = method.split(':');
     331                            method = arr[0];
     332                            postfix = arr[1];
     333                        }
     334                    }
     335
     336                    jQuery( '.easypack_selected_point_data' ).each(
     337                        function (ind, elem) {
     338                            jQuery( elem ).remove();
     339                        }
     340                    );
     341
     342                    if(typeof method != 'undefined' && method !== null) {
     343
     344                        let selector = '#shipping_method_0_easypack_italy_parcel_machines' + postfix;
     345
     346                        let map_button = '<div class="easypack_italy_geowidget" id="easypack_italy_geowidget">\n' +
     347                            easypack_front_map.button_text1 + '</div>';
     348
     349                        let li = jQuery(selector).parent('li');
     350
     351                        if (method === 'easypack_italy_parcel_machines') {
     352                            jQuery(li).after(map_button);
     353                            jQuery('#ship-to-different-address').hide();
     354                            let InpostItalyPointObject = localStorage.getItem('InpostItalyPointObject');
     355                            if (InpostItalyPointObject !== null) {
     356                                let point,
     357                                    selected_point_data,
     358                                    lot,
     359                                    lat,
     360                                    desc;
     361
     362                                let pointData = JSON.parse(InpostItalyPointObject);
     363
     364                                if( typeof pointData.lat != 'undefined' && pointData.lat !== null ) {
     365                                    if( typeof pointData.lon != 'undefined' && pointData.lon !== null ) {
     366                                        inpost_italy_coords  = [pointData.lat, pointData.lon];
     367                                    }
     368                                }
     369                                if( typeof pointData.pointName != 'undefined' && pointData.pointName !== null ) {
     370                                    point = pointData.pointName;
     371                                }
     372                                if( typeof pointData.pointDesc != 'undefined' && pointData.pointDesc !== null ) {
     373                                    desc = pointData.pointDesc;
     374                                } else {
     375                                    desc = '';
     376                                }
     377                                if( pointData.pointAddDesc.length > 0 ) {
     378                                    additional_desc = ' (' + pointData.pointAddDesc + ')';
     379                                } else {
     380                                    additional_desc = '';
     381                                }
     382
     383                                if( point ) {
     384                                    selected_point_data = '<div class="easypack_selected_point_data" id="easypack_selected_point_data">\n'
     385                                        + '<span class="font-height-600">' +  easypack_front_map.selected_text + '</span>\n'
     386                                        + '<div id="selected-parcel-machine-id">' + point + '</div>\n'
     387                                        + '<span id="selected-parcel-machine-desc">' + desc + '</span>\n'
     388                                        + '<span id="selected-parcel-machine-desc1">' + additional_desc + '</span>'
     389                                        + '<input type="hidden" id="parcel_machine_id" name="parcel_machine_id" class="parcel_machine_id" value="' + point + '"/>\n'
     390                                        + '<input type="hidden" id="parcel_machine_desc" name="parcel_machine_desc" class="parcel_machine_desc" value="' + desc + '"/></div>';
     391
     392                                    jQuery('#easypack_italy_geowidget').after(selected_point_data);
     393                                    jQuery("#easypack_italy_geowidget").text(easypack_front_map.button_text2);
     394
     395                                }
     396                            }
     397
     398                        } else {
     399
     400                            jQuery('.easypack_italy_geowidget').each(function(ind, elem) {
     401                                jQuery(elem).remove();
     402                            });
     403
     404                            jQuery('.easypack_selected_point_data').each(function(ind, elem) {
     405                                jQuery(elem).remove();
     406                            });
     407                            //empty hidden values of selected point.
     408                            jQuery('.parcel_machine_id').val('');
     409                            jQuery('.parcel_machine_desc').val('');
     410                            jQuery('#ship-to-different-address').show();
     411                        }
     412
     413                        jQuery("#easypack_italy_geowidget").click(function (e) {
     414                            e.preventDefault();
     415                            console.log('Inpost IT: coords');
     416                            console.log(inpost_italy_coords);
     417
     418                            jQuery('#easypack_selected_point_data').remove();
     419
     420                            easyPack.modalMap(function (point, modal) {
     421                                modal.closeModal();
     422                                console.log('Inpost IT: locker data');
     423                                console.log(point);
     424
     425                                if (point) {
     426                                    jQuery("#easypack_italy_geowidget").text(easypack_front_map.button_text2);
     427                                    selectPointCallback(point);
     428                                }
     429                            }, {width: window.innerWidth, height: window.innerHeight});
     430
     431                            inp_waitForElm('.types-list').then((elm) => {
     432                                window.mapController.setCenterFromArray(inpost_italy_coords);
     433                            });
     434
     435                            return true;
     436                        });
     437
     438                    }
     439
     440
     441                }
     442            }
     443        }
     444    );
     445
    225446});
    226447
     
    231452    var target = e.target || e.srcElement;
    232453
    233     if (target.hasAttribute('id') && target.getAttribute('id') == 'easypack-search') {
     454
     455    if (target.hasAttribute( 'id' ) && 'easypack_italy_geowidget' === target.getAttribute('id')  ) {
     456        e.preventDefault();
     457
     458        //console.log('Inpost IT JS: coords');
     459        //console.log(inpost_italy_coords);
     460
     461        jQuery( '.easypack_selected_point_data' ).each(
     462            function (ind, elem) {
     463                jQuery( elem ).remove();
     464            }
     465        );
     466        easyPack.modalMap(
     467            function (point, modal) {
     468                modal.closeModal();
     469                if (point) {
     470                    jQuery( "#easypack_italy_geowidget" ).text( easypack_front_map.button_text2 );
     471                    selectPointCallback( point );
     472                }
     473            },
     474            {width: window.innerWidth, height: window.innerHeight}
     475        );
     476
     477        inp_waitForElm('.types-list').then((elm) => {
     478            window.mapController.setCenterFromArray(inpost_italy_coords);
     479        });
     480
     481        return true;
     482    }
     483
     484
     485    if (target.hasAttribute('id') && 'easypack-search' === target.getAttribute('id') ) {
    234486
    235487        let search_was_started = false;
     
    239491
    240492        search_input_x.addEventListener("search", function(event) {
    241             window.mapController.setCenterFromArray([41.898386, 12.516985]);
     493            window.mapController.setCenterFromArray(inpost_italy_coords);
    242494        });
    243495
     
    250502            if(search_was_started && this.value.length < 1) {
    251503                // return map to Rome position
    252                 window.mapController.setCenterFromArray([41.898386, 12.516985]);
     504                window.mapController.setCenterFromArray(inpost_italy_coords);
    253505            }
    254506        });
     507
    255508    }
    256509
     
    277530    });
    278531}
     532
     533
     534function inpost_italy_get_map_coords(zip) {
     535
     536    let street = jQuery( '#shipping_address_1' );
     537    let city   = jQuery( '#shipping_city' );
     538
     539    if ( ! street ) {
     540        street = jQuery( '#billing_address_1' );
     541    }
     542
     543    if ( ! city ) {
     544        city = jQuery( '#billing_city' );
     545    }
     546
     547    if (typeof street != 'undefined' && street !== null && typeof city != 'undefined' && city !== null ) {
     548
     549        if ( jQuery( city ).val() ) {
     550            if ( zip === inpost_italy_zip_val ) {
     551                return;
     552            }
     553
     554            // stop if ajax is already running.
     555            if (inpost_it_is_ajax_get_point_running) {
     556                return;
     557            }
     558
     559            let address = zip + ', Italy';
     560            /*if ( jQuery( street ).val() ) {
     561                address = jQuery( city ).val() + ',' + jQuery( street ).val() + ',' + zip;
     562            } else {
     563                address = jQuery( city ).val() + ', Italy';
     564            }*/
     565
     566            xhr = jQuery.ajax(
     567                {
     568                    data: {
     569                        action: 'inpost_italy_get_coords',
     570                        get_coord_address: address,
     571                        security: easypack_front_map.nonce
     572                    },
     573                    type: 'POST',
     574                    url: easypack_front_map.ajax_url,
     575                    beforeSend: function () {
     576                        inpost_it_is_ajax_get_point_running = true;
     577                        console.log( 'Search address:' );
     578                        console.log( address );
     579                    },
     580                    complete: function () {
     581                        console.log( 'Complete' );
     582                        inpost_italy_zip_val                   = zip;
     583                        inpost_it_is_ajax_get_point_running = false;
     584                    },
     585                    error: function (jqXHR, textStatus, errorThrown) {
     586                        console.log( 'retrieve_coord_error' );
     587                        console.log( 'error: ' + jqXHR.status );
     588                        inpost_it_is_ajax_get_point_running = false;
     589                        return false;
     590                    },
     591                    success: function (data, textStatus, jqXHR) {
     592
     593                        if (data) {
     594                            let obj = JSON.parse( data );
     595
     596                            if ( obj.hasOwnProperty( 'formatted_address' ) ) {
     597                                console.log( 'Found address:' );
     598                                console.log( obj.formatted_address );
     599                            }
     600
     601                            if ( obj.hasOwnProperty( 'lat' ) && obj.hasOwnProperty( 'lon' ) ) {
     602                                inpost_italy_coords = [obj.lat, obj.lon];
     603                            }
     604
     605                        }
     606                    }
     607                }
     608            );
     609        }
     610    }
     611}
  • inpost-italy/trunk/src/InspireLabs/InpostItaly/EasyPack_Italy.php

    r3195026 r3324913  
    5757
    5858
    59     /**
    60      * @return string
    61      */
    62     public static function getLabelsUri() {
    63         return plugins_url() . '/woo-inpost/web/labels/';
    64     }
    6559
    6660    public function __construct() {
    6761        parent::__construct();
    6862        add_action( 'plugins_loaded', [ $this, 'init_easypack_italy' ], 100 );
     63
     64        add_action(
     65            'after_setup_theme',
     66            function () {
     67                $this->init_shipping_methods();
     68            }
     69        );
    6970    }
    7071
     
    125126        try {
    126127            ( new Easypack_Shipping_Rates() )->init();
    127             $this->init_shipping_methods();
    128 
    129128            ( new EasyPack_Webhook() )->hooks();
    130129            ( new EasyPackBulkOrders() )->hooks();
     
    151150            ( new EasyPack_Italy_Product_Shipping_Method_Selector )->handle_product_edit_hooks();
    152151        } catch ( Exception $exception ) {
    153             \wc_get_logger()->debug( 'Exception init_shipping_methods: ', array( 'source' => 'inpost-it-log' ) );
     152            \wc_get_logger()->debug( 'Exception: ', array( 'source' => 'inpost-it-log' ) );
     153            \wc_get_logger()->debug( __METHOD__ . ': ' . __LINE__, array( 'source' => 'inpost-it-log' ) );
    154154            \wc_get_logger()->debug( print_r( $exception->getMessage(), true), array( 'source' => 'inpost-it-log' ) );
    155155        }
     
    402402                'easypack_front_map',
    403403                array(
    404                     'location_icon' => esc_url(EasyPack_Italy::get_assets_img_uri() . "mylocation-sprite-2x.png" )
     404                    'location_icon' => esc_url(EasyPack_Italy::get_assets_img_uri() . "mylocation-sprite-2x.png" ),
     405                    'nonce' => wp_create_nonce( 'easypack_nonce' ),
     406                    'ajax_url' => admin_url( 'admin-ajax.php' )
    405407                )
    406408            );
     
    418420                    'selected_text' => esc_html__( 'Selected parcel locker', 'inpost-italy' ),
    419421                    'placeholder_text' => esc_html__('Type a city, address or postal code and select your choice. Or type an Inpost machine number and press magnifier icon', 'inpost-italy'),
     422                    'nonce' => wp_create_nonce( 'easypack_nonce' ),
     423                    'ajax_url' => admin_url( 'admin-ajax.php' )
    420424                )
    421425            );
    422426        }
    423427
    424         if( is_checkout() ) {
     428        if( is_checkout() || has_block( 'woocommerce/checkout' ) ) {
    425429            wp_enqueue_style( 'easypack-jbox-css', $this->getPluginCss() . 'jBox.all.min.css', [], INPOST_ITALY_PLUGIN_VERSION );
    426430            wp_enqueue_script( 'easypack-jquery-modal', $this->getPluginJs() . 'jBox.all.min.js', [ 'jquery' ], INPOST_ITALY_PLUGIN_VERSION, true );
     
    859863
    860864    public function enqueue_block_script() {
    861         if( is_checkout() && has_block( 'woocommerce/checkout' )) {
     865        if( has_block( 'woocommerce/checkout' ) ) {
    862866
    863867            wp_enqueue_script('inpost-italy-front-blocks', $this->getPluginJs() . 'front-blocks.js', ['jquery'], INPOST_ITALY_PLUGIN_VERSION, true );
     
    871875                    'selected_text'      => esc_html__( 'Selected parcel locker', 'inpost-italy' ),
    872876                    'placeholder_text'   => esc_html__('Type a city, address or postal code and select your choice. Or type an Inpost machine number and press magnifier icon', 'inpost-italy'),
     877                    'nonce' => wp_create_nonce( 'easypack_nonce' ),
     878                    'ajax_url' => admin_url( 'admin-ajax.php' )
    873879                )
    874880            );
     
    927933            $single_rate = reset($rates);
    928934
    929             if( is_checkout() ) {
     935            if( is_checkout() || has_block( 'woocommerce/checkout' ) ) {
    930936                if( in_array($single_rate->method_id, $methods_required_geowidget) ) {
    931937
  • inpost-italy/trunk/src/InspireLabs/InpostItaly/EasyPack_Italy_AJAX.php

    r3121177 r3324913  
    2828        public static function init() {
    2929            add_action( 'wp_ajax_easypack', array( __CLASS__, 'ajax_easypack' ) );
     30            add_action( 'wp_ajax_inpost_italy_get_coords', array( __CLASS__, 'get_coords' ) );
     31            add_action( 'wp_ajax_nopriv_inpost_italy_get_coords', array( __CLASS__, 'get_coords' ) );
    3032        }
    3133
     
    209211        }
    210212
     213
     214        public static function get_coords() {
     215
     216            check_ajax_referer( 'easypack_nonce', 'security' );
     217
     218            $coords = array();
     219
     220            $google_map_api_key = 'AIzaSyBLB2vfXScQHyB7ME_wMAwuXUBZJuavyB4';
     221
     222            if ( ! empty( $_POST['get_coord_address'] ) ) {
     223
     224                $address = sanitize_text_field( wp_unslash( $_POST['get_coord_address'] ) );
     225
     226                $address = urlencode( $address );
     227
     228                $url       = 'https://maps.google.com/maps/api/geocode/json';
     229                $data      = array(
     230                    'address' => $address,
     231                    'sensor'  => 'false',
     232                    'region'  => 'Italy',
     233                    'key'     => $google_map_api_key,
     234                );
     235                $query_url = $url . '?' . http_build_query( $data );
     236
     237                $resp = wp_remote_get( $query_url );
     238
     239                if ( $resp ) {
     240                    $body_decoded = json_decode( wp_remote_retrieve_body( $resp ) );
     241
     242                    if ( ! is_object( $body_decoded ) ) {
     243                        return false;
     244                    }
     245
     246                    if ( property_exists( $body_decoded, 'error_message' ) ) {
     247                        \wc_get_logger()->debug( 'INPOST GEO CODING ERR: ', array( 'source' => 'inpost-log' ) );
     248                        \wc_get_logger()->debug( print_r( $body_decoded->error_message, true ), array( 'source' => 'inpost-log' ) );
     249                        return $coords;
     250                    }
     251
     252                    $resp = json_decode( wp_remote_retrieve_body( $resp ), true );
     253
     254                    if ( $resp['status'] === 'OK' ) {
     255                        $lat               = isset( $resp['results'][0]['geometry']['location']['lat'] ) ? $resp['results'][0]['geometry']['location']['lat'] : '';
     256                        $lon               = isset( $resp['results'][0]['geometry']['location']['lng'] ) ? $resp['results'][0]['geometry']['location']['lng'] : '';
     257                        $formatted_address = isset( $resp['results'][0]['formatted_address'] ) ? $resp['results'][0]['formatted_address'] : '';
     258
     259                        if ( $lat && $lon ) {
     260
     261                            $coords['lat']               = $lat;
     262                            $coords['lon']               = $lon;
     263                            $coords['formatted_address'] = $formatted_address;
     264
     265                        }
     266                    }
     267                }
     268            }
     269
     270            echo wp_json_encode( $coords );
     271            wp_die();
     272        }
     273
    211274    }
    212275
  • inpost-italy/trunk/src/InspireLabs/InpostItaly/Geowidget_v4.php

    r3121177 r3324913  
    3838    function geowidget_css_lib_code_front() {
    3939        // only on checkout page
    40         if( is_checkout() ) {
     40        if( is_checkout() || has_block( 'woocommerce/checkout' ) ) {
    4141            wp_enqueue_style('geowidget-css', $this->get_geowidget_css_src(), [], INPOST_ITALY_PLUGIN_VERSION );
    4242            wp_enqueue_style( 'easypack-front-font-awesome', inpost_italy()->getPluginCss() . 'font-awesome.min.css', [], INPOST_ITALY_PLUGIN_VERSION );
Note: See TracChangeset for help on using the changeset viewer.