From 62a3a901fecdc74af42fc251ffa9e5eace0b37c2 Mon Sep 17 00:00:00 2001 From: UVLabs Date: Wed, 17 Nov 2021 16:42:38 -0400 Subject: [PATCH] chore: syncing --- assets/admin/js/lpac-admin.js | 99 ++++++++++ assets/public/js/maps/checkout-page-map.js | 172 ++++++++++++++++-- class-lpac-uninstall.php | 3 + includes/Bootstrap/Main.php | 16 +- .../Controllers/Admin_Settings_Controller.php | 2 +- .../Controllers/Checkout_Page_Controller.php | 22 ++- includes/Controllers/Emails_Controller.php | 2 +- .../Controllers/Map_Visibility_Controller.php | 20 -- includes/Helpers/Functions.php | 31 +++- includes/Models/Location_Details.php | 78 ++++++++ includes/Models/Save_Location_Details.php | 41 ----- includes/Views/Admin.php | 18 +- includes/Views/Admin_Settings.php | 40 +++- includes/Views/Frontend.php | 29 ++- lpac.php | 14 +- readme.txt | 25 ++- 16 files changed, 497 insertions(+), 115 deletions(-) create mode 100644 includes/Models/Location_Details.php delete mode 100644 includes/Models/Save_Location_Details.php diff --git a/assets/admin/js/lpac-admin.js b/assets/admin/js/lpac-admin.js index 28f5e66..f6cb1b7 100644 --- a/assets/admin/js/lpac-admin.js +++ b/assets/admin/js/lpac-admin.js @@ -64,6 +64,105 @@ } }); + function toggleAutoDetectOptions(){ + + const autoDetectLocation = $('#lpac_auto_detect_location'); + + if( ! autoDetectLocation ){ + return; + } + + const autoDetectLocationChecked = autoDetectLocation.is(":checked"); + const forceMapUse = $('#lpac_force_map_use'); + + // Hide suboptions if feature disabled + if( autoDetectLocationChecked ){ + forceMapUse.closest('tr').hide(); + } + + autoDetectLocation.on('click', () => { + + if( autoDetectLocation.is(':checked') ){ + forceMapUse.closest('tr').hide(); + }else{ + forceMapUse.closest('tr').show(); + } + + }); + + } + + function toggleMapLinkOrderEmailOptions(){ + + const addToEmail = $('#lpac_enable_delivery_map_link_in_email'); + + if( ! addToEmail ){ + return; + } + + const addToEmailChecked = addToEmail.is(":checked"); + const linkType = $('#lpac_email_delivery_map_link_type'); + const linkLocation = $('#lpac_email_delivery_map_link_location'); + const selectedEmails = $('#lpac_email_delivery_map_emails'); + + // Hide suboptions if feature disabled + if( ! addToEmailChecked ){ + linkType.closest('tr').hide(); + linkLocation.closest('tr').hide(); + selectedEmails.closest('tr').hide(); + } + + addToEmail.on('click', () => { + + if( addToEmail.is(':checked') ){ + linkType.closest('tr').show(); + linkLocation.closest('tr').show(); + selectedEmails.closest('tr').show(); + }else{ + linkType.closest('tr').hide(); + linkLocation.closest('tr').hide(); + selectedEmails.closest('tr').hide(); + } + + }); + + } + + function togglePlacesAutoCompleteOptions(){ + + const placesAutoComplete = $('#lpac_enable_places_autocomplete'); + + if( ! placesAutoComplete ){ + return; + } + + const placesAutoCompleteChecked = placesAutoComplete.is(":checked"); + const placesAllowedFields = $('#lpac_places_autocomplete_fields'); + const placesAutoCompleteHideMap = $('#lpac_places_autocomplete_hide_map'); + + if( ! placesAutoCompleteChecked ){ + placesAllowedFields.closest('tr').hide(); + placesAutoCompleteHideMap.closest('tr').hide(); + } + + placesAutoComplete.on('click', () => { + + if( placesAutoComplete.is(':checked') ){ + placesAllowedFields.closest('tr').show(); + placesAutoCompleteHideMap.closest('tr').show(); + }else{ + placesAllowedFields.closest('tr').hide(); + placesAutoCompleteHideMap.closest('tr').hide(); + } + + }); + + } + + toggleAutoDetectOptions(); + toggleMapLinkOrderEmailOptions(); + togglePlacesAutoCompleteOptions(); + }); })(jQuery); diff --git a/assets/public/js/maps/checkout-page-map.js b/assets/public/js/maps/checkout-page-map.js index e3fff5c..27fb731 100644 --- a/assets/public/js/maps/checkout-page-map.js +++ b/assets/public/js/maps/checkout-page-map.js @@ -12,6 +12,7 @@ const infowindow = window.lpac_infowindow; const geocoder = new google.maps.Geocoder(); const find_location_btn = document.querySelector("#lpac-find-location-btn"); +const places_autocomplete_used = document.querySelector('#lpac_places_autocomplete'); if (typeof (find_location_btn) !== 'undefined' && find_location_btn !== null) { find_location_btn.addEventListener( @@ -106,6 +107,8 @@ async function lpac_bootstrap_map_functionality(geocoder, map, infowindow) { */ lpac_fill_in_latlng(latlng); + places_autocomplete_used.value = 0; + } /** @@ -223,6 +226,7 @@ function lpac_map_listen_to_clicks() { infowindow.setContent(detected_address); infowindow.open(map, marker); + places_autocomplete_used.value = 0; }); } @@ -253,13 +257,14 @@ function lpac_marker_listen_to_drag() { return; } - let moved_to_address = results[ 0 ].formatted_address + let moved_to_address = results[ 0 ].formatted_address; moved_to_address = lpacRemovePlusCode(moved_to_address); - infowindow.setContent(moved_to_address) + infowindow.setContent(moved_to_address); - lpac_fill_in_address_fields(results, moved_to_latlng) + lpac_fill_in_address_fields(results, moved_to_latlng); + places_autocomplete_used.value = 0; } @@ -301,11 +306,7 @@ function lpac_fill_in_address_fields(results, latLng = '') { lpac_fill_in_latlng(latLng) - lpac_fill_in_shipping_country_region(results) - lpac_fill_in_shipping_full_address(results) - lpac_fill_in_shipping_town_city(results) - lpac_fill_in_shipping_state_county(results) - lpac_fill_in_shipping_zipcode(results) + lpac_fill_in_shipping_fields( results ); if (typeof (mapOptions) === 'undefined' || mapOptions === null) { console.log('LPAC: mapOptions object not present, skipping...') @@ -315,15 +316,37 @@ function lpac_fill_in_address_fields(results, latLng = '') { const lpac_autofill_billing_fields = mapOptions.lpac_autofill_billing_fields if (lpac_autofill_billing_fields) { - lpac_fill_in_billing_country_region(results) - lpac_fill_in_billing_full_address(results) - lpac_fill_in_billing_town_city(results) - lpac_fill_in_billing_state_county(results) - lpac_fill_in_billing_zipcode(results) + lpac_fill_in_billing_fields( results ); } } +/** + * Fill in all shipping fields. + * + * @param {array} results + */ +function lpac_fill_in_shipping_fields( results ){ + lpac_fill_in_shipping_country_region(results) + lpac_fill_in_shipping_full_address(results) + lpac_fill_in_shipping_town_city(results) + lpac_fill_in_shipping_state_county(results) + lpac_fill_in_shipping_zipcode(results) +} + +/** + * Fill in all billing fields. + * + * @param {array} results + */ +function lpac_fill_in_billing_fields( results ){ + lpac_fill_in_billing_country_region(results) + lpac_fill_in_billing_full_address(results) + lpac_fill_in_billing_town_city(results) + lpac_fill_in_billing_state_county(results) + lpac_fill_in_billing_zipcode(results) +} + /* * Get country from map. */ @@ -660,7 +683,6 @@ function lpacHideShowMap(){ }); - } /** @@ -675,6 +697,11 @@ function lpacSetLastOrderMarker(){ return; } + // If no coordinates exist don't try to plot the location on the map + if( ! lpacLastOrder.latitude || ! lpacLastOrder.longitude ){ + return; + } + const latlng = { lat: parseFloat(lpacLastOrder.latitude), lng: parseFloat(lpacLastOrder.longitude), @@ -713,6 +740,110 @@ function lpacSetLastOrderMarker(){ } +/** + * Places AutoComplete feature. + * + * https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete + * + * @returns + */ +function addPlacesAutoComplete(){ + + // Return if feature not enabled + if( ! mapOptions.lpac_enable_places_autocomplete ){ + return; + } + + // Hide map if option is turned on. + if( mapOptions.lpac_places_autocomplete_hide_map ){ + changeMapVisibility(false); + } + + const fields = mapOptions.lpac_places_autocomplete_fields; + + fields.forEach(fieldID => { + + const field = document.querySelector('#' + fieldID); + + /* + * If field doesn't exist bail. + * This might happen if user sets shipping destination to "Force shipping to the customer billing address" so the shipping fields wouldn't exist. + */ + if( !field ){ + return; + } + + const options = { + // componentRestrictions: { country: ["us", "ca"] }, // TODO let users control this + fields: ["address_components", "formatted_address", "geometry"], + types: ["address"], + } + + const autoComplete = new google.maps.places.Autocomplete(field, options); + + /* Bind the map's bounds (viewport) property to the autocomplete object, + so that the autocomplete requests use the current map bounds for the + bounds option in the request. */ + autoComplete.bindTo("bounds", map); + + autoComplete.addListener("place_changed", () => { + + const results = [autoComplete.getPlace()]; + + const latlng = { + lat: parseFloat( results[0].geometry.location.lat() ), + lng: parseFloat( results[0].geometry.location.lng() ), + } + + if( fieldID.includes('shipping') ){ + + if( mapOptions.lpac_places_fill_shipping_fields ){ + lpac_fill_in_shipping_fields(results); + } + + lpac_fill_in_latlng(latlng); + + map.setCenter(latlng); + marker.setPosition(latlng); + map.setZoom(16); + infowindow.setContent(results[0].formatted_address); + infowindow.open(map, marker); + places_autocomplete_used.value = 1; + // Add event listeners to map + lpac_marker_listen_to_drag(); + lpac_map_listen_to_clicks(); + }else{ + + if( mapOptions.lpac_places_fill_shipping_fields ){ + lpac_fill_in_billing_fields(results); + } + + /* + * When Shipping destination is set as "Force shipping to the customer billing address" in WooCommerce->Shipping->Shipping Options + * We would want to adjust the map as needed. + */ + if( mapOptions.lpac_wc_shipping_destination_setting === 'billing_only'){ + lpac_fill_in_latlng(latlng); + map.setCenter(latlng); + marker.setPosition(latlng); + map.setZoom(16); + infowindow.setContent(results[0].formatted_address); + infowindow.open(map, marker); + places_autocomplete_used.value = 1; + // Add event listeners to map + lpac_marker_listen_to_drag(); + lpac_map_listen_to_clicks(); + } + + } + + }) + + }); + +} +addPlacesAutoComplete(); + /** * Detect when shipping methods are changed based on WC custom updated_checkout event. * This event can't be accessed via vanilla JS because it's triggered by jQuery. @@ -722,9 +853,18 @@ function lpacSetLastOrderMarker(){ $(document).ready( function () { + // Prevents ajax call in lpacHideShowMap from overriding our lpac_places_autocomplete_hide_map option. + if( ! mapOptions.lpac_places_autocomplete_hide_map ){ + $(document.body).on('updated_checkout', lpacHideShowMap); + } + + // If the auto detect location feature is turned on, then detect the location but don't output the last order details. + if( mapOptions.lpac_auto_detect_location ){ + lpac_bootstrap_map_functionality(geocoder, map, infowindow) + }else{ + lpacSetLastOrderMarker(); + } - $(document.body).on('updated_checkout', lpacHideShowMap); - lpacSetLastOrderMarker(); } ); diff --git a/class-lpac-uninstall.php b/class-lpac-uninstall.php index 0f1a70c..1c1e14e 100644 --- a/class-lpac-uninstall.php +++ b/class-lpac-uninstall.php @@ -82,6 +82,9 @@ public static function remove_plugin_settings() { 'lpac_admin_view_order_map_id', 'lpac_installed_at_version', 'lpac_remove_address_plus_code', + 'lpac_enable_places_autocomplete', + 'lpac_places_autocomplete_fields', + 'lpac_auto_detect_location', ); foreach ( $option_keys as $key ) { diff --git a/includes/Bootstrap/Main.php b/includes/Bootstrap/Main.php index d25faec..7881d3d 100644 --- a/includes/Bootstrap/Main.php +++ b/includes/Bootstrap/Main.php @@ -39,6 +39,7 @@ use Lpac\Notices\Admin as Admin_Notices ; use Lpac\Views\Frontend as Frontend_Display ; use Lpac\Compatibility\WooFunnels\Woo_Funnels ; +use Lpac\Models\Location_Details ; /** * Class Main. * @@ -183,6 +184,7 @@ private function define_public_hooks() $controller_emails = new Emails_Controller(); $controller_map_visibility = new Map_Visibility_Controller(); $controler_checkout_page = new Checkout_Page_Controller(); + $model_location_details = new Location_Details(); /* * If plugin not enabled don't continue */ @@ -271,15 +273,25 @@ private function define_public_hooks() $this->loader->add_action( 'wp_ajax_nopriv_lpac_to_be_or_not_to_be', $controller_map_visibility, 'checkout_map_visibility_ajax_handler' ); $this->loader->add_action( 'wp_ajax_lpac_to_be_or_not_to_be', $controller_map_visibility, 'checkout_map_visibility_ajax_handler' ); /* - * Add the latitude and longitude to the order meta + * Validate checkout map details and then add the latitude and longitude to the order meta. */ $this->loader->add_action( 'woocommerce_checkout_update_order_meta', - $controller_map_visibility, + $model_location_details, 'validate_map_visibility', 10, 2 ); + /* + * Add places autocomplete order meta. + */ + $this->loader->add_action( + 'woocommerce_checkout_update_order_meta', + $model_location_details, + 'save_places_autocomplete', + 10, + 2 + ); } /** diff --git a/includes/Controllers/Admin_Settings_Controller.php b/includes/Controllers/Admin_Settings_Controller.php index 0ac5f9d..0ffbb92 100644 --- a/includes/Controllers/Admin_Settings_Controller.php +++ b/includes/Controllers/Admin_Settings_Controller.php @@ -6,7 +6,7 @@ * Created on: 19/10/2021 (d/m/y) * * @link https://uriahsvictor.com -* @since 1.3.3 +* @since 1.3.4 * @package Lpac */ diff --git a/includes/Controllers/Checkout_Page_Controller.php b/includes/Controllers/Checkout_Page_Controller.php index f46729d..b975759 100644 --- a/includes/Controllers/Checkout_Page_Controller.php +++ b/includes/Controllers/Checkout_Page_Controller.php @@ -21,7 +21,6 @@ */ class Checkout_Page_Controller { - /** * Map settings. * @@ -32,13 +31,20 @@ public function get_map_options() { $options = Functions::set_map_options(); $data = array( - 'lpac_map_default_latitude' => $options['latitude'], - 'lpac_map_default_longitude' => $options['longitude'], - 'lpac_map_zoom_level' => $options['zoom_level'], - 'lpac_map_clickable_icons' => $options['clickable_icons'] === 'yes' ? true : false, - 'lpac_map_background_color' => $options['background_color'], - 'lpac_autofill_billing_fields' => $options['fill_in_billing_fields'] === 'yes' ? true : false, - 'lpac_remove_address_plus_code' => $options['remove_address_plus_code'] === 'yes' ? true : false, + 'lpac_map_default_latitude' => $options['latitude'], + 'lpac_map_default_longitude' => $options['longitude'], + 'lpac_map_zoom_level' => $options['zoom_level'], + 'lpac_map_clickable_icons' => $options['clickable_icons'] === 'yes' ? true : false, + 'lpac_map_background_color' => $options['background_color'], + 'lpac_autofill_billing_fields' => $options['fill_in_billing_fields'] === 'yes' ? true : false, + 'lpac_remove_address_plus_code' => $options['remove_address_plus_code'] === 'yes' ? true : false, + 'lpac_enable_places_autocomplete' => $options['enable_places_search'] === 'yes' ? true : false, + 'lpac_places_autocomplete_fields' => $options['places_search_fields'], + 'lpac_places_autocomplete_hide_map' => $options['places_autocomplete_hide_map'] === 'yes' ? true : false, + 'lpac_places_fill_shipping_fields' => $options['places_fill_shipping_fields'], + 'lpac_places_fill_billing_fields' => $options['places_fill_billing_fields'], + 'lpac_auto_detect_location' => $options['auto_detect_location'] === 'yes' ? true : false, + 'lpac_wc_shipping_destination_setting' => $options['wc_shipping_destination_setting'], ); return apply_filters( 'lpac_map_stored_public_settings', $data ); diff --git a/includes/Controllers/Emails_Controller.php b/includes/Controllers/Emails_Controller.php index 6a2a014..1c473c0 100644 --- a/includes/Controllers/Emails_Controller.php +++ b/includes/Controllers/Emails_Controller.php @@ -7,7 +7,7 @@ * Created on: 03/10/2021 (d/m/y) * * @link https://uriahsvictor.com -* @since 1.4.0 +* @since 1.3.4 * @package Lpac */ diff --git a/includes/Controllers/Map_Visibility_Controller.php b/includes/Controllers/Map_Visibility_Controller.php index 5f72fa4..bbcd155 100644 --- a/includes/Controllers/Map_Visibility_Controller.php +++ b/includes/Controllers/Map_Visibility_Controller.php @@ -95,26 +95,6 @@ public function checkout_map_visibility_ajax_handler() } } - /** - * Validate the Map's visibility setting to prevent manipulations via the DOM. - * - * @return void - */ - public function validate_map_visibility( $order_id, $data ) - { - $show = self::lpac_show_map( 'checkout' ); - $map_shown = $data['lpac_is_map_shown'] ?? ''; - - if ( $show === false || empty($map_shown) ) { - \Lpac\Models\Save_Location_Details::save_order_meta_cords( $order_id, '', '' ); - return; - } - - $lat = $data['lpac_latitude'] ?? ''; - $long = $data['lpac_longitude'] ?? ''; - \Lpac\Models\Save_Location_Details::save_order_meta_cords( $order_id, $lat, $long ); - } - /** * Default map visibility rules and order. * diff --git a/includes/Helpers/Functions.php b/includes/Helpers/Functions.php index 2b248df..7dfb764 100644 --- a/includes/Helpers/Functions.php +++ b/includes/Helpers/Functions.php @@ -243,14 +243,31 @@ public static function set_map_options() { $remove_address_plus_code = get_option( 'lpac_remove_address_plus_code', 'no' ); $remove_address_plus_code = apply_filters( 'lpac_remove_address_plus_code', $remove_address_plus_code ); + $enable_places_search = get_option( 'lpac_enable_places_autocomplete', 'no' ); + $places_search_fields = get_option( 'lpac_places_autocomplete_fields', array() ); + $places_search_fields = apply_filters( 'lpac_places_autocomplete_fields', $places_search_fields ); + $places_autocomplete_hide_map = get_option( 'lpac_places_autocomplete_hide_map', 'no' ); + $places_fill_shipping_fields = apply_filters( 'lpac_places_fill_shipping_fields', true ); + $places_fill_billing_fields = apply_filters( 'lpac_places_fill_billing_fields', true ); + $wc_shipping_destination_setting = get_option( 'woocommerce_ship_to_destination' ); + + $auto_detect_location = get_option( 'lpac_auto_detect_location', 'no' ); + $options = array( - 'latitude' => $latitude, - 'longitude' => $longitude, - 'zoom_level' => $zoom_level, - 'clickable_icons' => $clickable_icons, - 'background_color' => $background_color, - 'fill_in_billing_fields' => $fill_in_billing_fields, - 'remove_address_plus_code' => $remove_address_plus_code, + 'latitude' => $latitude, + 'longitude' => $longitude, + 'zoom_level' => $zoom_level, + 'clickable_icons' => $clickable_icons, + 'background_color' => $background_color, + 'fill_in_billing_fields' => $fill_in_billing_fields, + 'remove_address_plus_code' => $remove_address_plus_code, + 'enable_places_search' => $enable_places_search, + 'places_search_fields' => $places_search_fields, + 'places_autocomplete_hide_map' => $places_autocomplete_hide_map, + 'places_fill_shipping_fields' => $places_fill_shipping_fields, + 'places_fill_billing_fields' => $places_fill_billing_fields, + 'auto_detect_location' => $auto_detect_location, + 'wc_shipping_destination_setting' => $wc_shipping_destination_setting, ); return apply_filters( 'lpac_map_options', $options ); diff --git a/includes/Models/Location_Details.php b/includes/Models/Location_Details.php new file mode 100644 index 0000000..d34d2bd --- /dev/null +++ b/includes/Models/Location_Details.php @@ -0,0 +1,78 @@ +save_order_meta_cords( $order_id, $lat, $long ); + + } + + /** + * Save the coordinates to the database. + * + * @since 1.0.0 + * @param int $order_id The order id. + */ + public function save_order_meta_cords( int $order_id, float $lat, float $long ) : void { + + if ( empty( $order_id ) || empty( $lat ) || empty( $long ) ) { + return; + } + + update_post_meta( $order_id, '_lpac_latitude', sanitize_text_field( $lat ) ); + update_post_meta( $order_id, '_lpac_longitude', sanitize_text_field( $long ) ); + } + + /** + * Save whether the Places Autocomplete feature was used. + * + * The value saved is a 1 or 0. 1 meaning yes and 0 meaning no. + * + * @param int $order_id + * @param array $data + * @return void + */ + public function save_places_autocomplete( int $order_id, array $data ) : void { + $places_autocomplete_used = $data['lpac_places_autocomplete']; + update_post_meta( $order_id, '_places_autocomplete', sanitize_text_field( $places_autocomplete_used ) ); + } + +} diff --git a/includes/Models/Save_Location_Details.php b/includes/Models/Save_Location_Details.php deleted file mode 100644 index 9c327dc..0000000 --- a/includes/Models/Save_Location_Details.php +++ /dev/null @@ -1,41 +0,0 @@ -get_id(), '_lpac_latitude', true ); - $longitude = get_post_meta( $order->get_id(), '_lpac_longitude', true ); + $latitude = get_post_meta( $order->get_id(), '_lpac_latitude', true ); + $longitude = get_post_meta( $order->get_id(), '_lpac_longitude', true ); + $places_autocomplete_used = get_post_meta( $order->get_id(), '_places_autocomplete', true ); + + /* translators: 1: Dashicons outbound link icon*/ + $learn_more = sprintf( __( 'Learn More %s', 'map-location-picker-at-checkout-for-woocommerce' ), '' ); /** * If we have no values for these options bail. @@ -45,14 +49,20 @@ public function lpac_display_lpac_admin_order_meta( $order ) { return; } - $order_meta_text = __( 'Customer Location', 'map-location-picker-at-checkout-for-woocommerce' ); - $view_on_map_text = __( 'View on Map', 'map-location-picker-at-checkout-for-woocommerce' ); + $order_meta_text = esc_html( __( 'Customer Location', 'map-location-picker-at-checkout-for-woocommerce' ) ); + $view_on_map_text = esc_html( __( 'View on Map', 'map-location-picker-at-checkout-for-woocommerce' ) ); + + $places_autocomplete_used_text = ''; + if ( ! empty( $places_autocomplete_used ) ) { + $places_autocomplete_used_text = sprintf( esc_html( __( 'It looks like this customer used the Places Autocomplete feature. The coordinates on the map might be an approximation. %s' ) ), " $learn_more " ); + } $map_link = apply_filters( 'lpac_map_provider', "https://www.google.com/maps/search/?api=1&query=${latitude},${longitude}", $latitude, $longitude ); $markup = <<$order_meta_text:

+

$places_autocomplete_used_text

HTML; echo $markup; diff --git a/includes/Views/Admin_Settings.php b/includes/Views/Admin_Settings.php index d4a5d29..064a85c 100644 --- a/includes/Views/Admin_Settings.php +++ b/includes/Views/Admin_Settings.php @@ -107,7 +107,7 @@ private function lpac_create_plugin_settings_banner() if ( empty(get_option( 'lpac_google_maps_api_key', '' )) ) { $no_api_key = __( 'You need an API Key to use Google Maps. Please see this document for how to get it ', 'map-location-picker-at-checkout-for-woocommerce' ); - $no_api_key .= "{$here}"; + $no_api_key .= "{$here}"; $no_api_key .= $external_icon; } else { $no_api_key = ''; @@ -181,11 +181,19 @@ public function lpac_create_general_setting_fields() $lpac_settings[] = array( 'name' => __( 'Google Maps API Key', 'map-location-picker-at-checkout-for-woocommerce' ), 'desc_tip' => __( 'Enter the API key from Google cloud console.', 'map-location-picker-at-checkout-for-woocommerce' ), - 'desc' => __( 'Enter the API key you copied from the Google Cloud Console. Learn More ', 'map-location-picker-at-checkout-for-woocommerce' ), + 'desc' => __( 'Enter the API key you copied from the Google Cloud Console. Learn More ', 'map-location-picker-at-checkout-for-woocommerce' ), 'id' => 'lpac_google_maps_api_key', 'type' => 'text', 'css' => 'min-width:300px;', ); + $lpac_settings[] = array( + 'name' => __( 'Detect Customer Location on Checkout Page Load', 'map-location-picker-at-checkout-for-woocommerce' ), + 'desc' => __( 'Yes', 'map-location-picker-at-checkout-for-woocommerce' ), + 'desc_tip' => sprintf( __( 'Enabling this option will have the plugin immediately try to detect the customer location when the checkout page loads. NOTE: This can negatively impact customer experiences. Think carefully before enabling this option. %s', 'map-location-picker-at-checkout-for-woocommerce' ), "{$learn_more}" ), + 'id' => 'lpac_auto_detect_location', + 'type' => 'checkbox', + 'css' => 'max-width:80px;', + ); $lpac_settings[] = array( 'name' => __( 'Force Use of Map', 'map-location-picker-at-checkout-for-woocommerce' ), 'desc' => __( 'Yes', 'map-location-picker-at-checkout-for-woocommerce' ), @@ -297,6 +305,34 @@ public function lpac_create_general_setting_fields() ), 'css' => 'min-width:300px;height: 100px', ); + $lpac_settings[] = array( + 'name' => __( 'Enable Places Autocomplete Feature', 'map-location-picker-at-checkout-for-woocommerce' ), + 'desc_tip' => sprintf( __( 'Allows customers to begin typing an address and receive suggestions from Google. NOTE: This is not as reliable as allowing customers to select their location on the map. %s', 'map-location-picker-at-checkout-for-woocommerce' ), "{$learn_more}" ), + 'desc' => __( 'Yes', 'map-location-picker-at-checkout-for-woocommerce' ), + 'id' => 'lpac_enable_places_autocomplete', + 'type' => 'checkbox', + 'css' => 'min-width:300px;', + ); + $lpac_settings[] = array( + 'name' => __( 'Allowed Places AutoComplete Fields', 'map-location-picker-at-checkout-for-woocommerce' ), + 'desc' => __( 'Select the input fields where the places autocomplete should be allowed.', 'map-location-picker-at-checkout-for-woocommerce' ), + 'id' => 'lpac_places_autocomplete_fields', + 'type' => 'multiselect', + 'options' => array( + 'billing_address_1' => __( 'Billing Address 1', 'map-location-picker-at-checkout-for-woocommerce' ), + 'shipping_address_1' => __( 'Shipping Address 1', 'map-location-picker-at-checkout-for-woocommerce' ), + ), + 'css' => 'min-width:300px;', + 'class' => 'wc-enhanced-select', + ); + $lpac_settings[] = array( + 'name' => __( 'Hide Map When Using Places Autocomplete', 'map-location-picker-at-checkout-for-woocommerce' ), + 'desc_tip' => __( 'Hide the map when using the Places Autocomplete feature.', 'map-location-picker-at-checkout-for-woocommerce' ), + 'desc' => __( 'Yes', 'map-location-picker-at-checkout-for-woocommerce' ), + 'id' => 'lpac_places_autocomplete_hide_map', + 'type' => 'checkbox', + 'css' => 'min-width:300px;', + ); $lpac_settings[] = array( 'name' => __( 'Housekeeping', 'map-location-picker-at-checkout-for-woocommerce' ), 'desc_tip' => __( 'Delete all plugin settings on uninstall.', 'map-location-picker-at-checkout-for-woocommerce' ), diff --git a/includes/Views/Frontend.php b/includes/Views/Frontend.php index aa2b63c..277b995 100644 --- a/includes/Views/Frontend.php +++ b/includes/Views/Frontend.php @@ -198,6 +198,12 @@ public function lpac_create_lat_and_long_inputs( $fields ) { 'class' => ( LPAC_DEBUG ) ? array( 'form-row-wide' ) : array( 'form-row-wide', 'hidden' ), ); + $fields['billing']['lpac_places_autocomplete'] = array( + 'label' => __( 'Places Autocomplete', 'map-location-picker-at-checkout-for-woocommerce' ), + 'required' => false, + 'class' => ( LPAC_DEBUG ) ? array( 'form-row-wide' ) : array( 'form-row-wide', 'hidden' ), + ); + return $fields; } @@ -312,19 +318,36 @@ public function lpac_add_admin_checkout_notice() { return; } + $learn_more = esc_html__( 'Learn More', 'map-location-picker-at-checkout-for-woocommerce' ); + $api_key = get_option( 'lpac_google_maps_api_key' ); $notice_text = esc_html__( 'Hi Admin, some websites might have issues with displaying or using the Google Map. If you\'re having issues then please have a look at your browser console for any errors.' ); $additional = esc_html__( 'Only Admins on your website can see this notice. You can turn it off in the plugin settings from the "Debug" submenu if everything works fine.' ); - $markup = <<$learn_more >>" ); + + $no_api_key_markup = << +

Location Picker at Checkout: + $no_api_key +

+ +HTML; + + echo $no_api_key_markup; + } + + $markup = << -

LPAC: +

Location Picker at Checkout: $notice_text

$additional

-MARKUP; +HTML; echo $markup; diff --git a/lpac.php b/lpac.php index 40654a1..10cb937 100644 --- a/lpac.php +++ b/lpac.php @@ -17,7 +17,7 @@ * Plugin Name: Location Picker At Checkout For WooCommerce * Plugin URI: https://soaringleads.com * Description: Allow customers to choose their shipping location using a map at checkout. - * Version: 1.3.4 + * Version: 1.3.5 * Author: Uriahs Victor * Author URI: https://uriahsvictor.com * License: GPL-2.0+ @@ -25,7 +25,7 @@ * Text Domain: map-location-picker-at-checkout-for-woocommerce * Domain Path: /languages * WC requires at least: 3.0 - * WC tested up to: 5.8 + * WC tested up to: 5.9 * Requires PHP: 7.0 */ // If this file is called directly, abort. @@ -108,7 +108,7 @@ function lpac_fs() * Start at version 1.0.0 and use SemVer - https://semver.org * Rename this for your plugin and update it as you release new versions. */ -define( 'LPAC_VERSION', '1.3.4' ); +define( 'LPAC_VERSION', '1.3.5' ); define( 'LPAC_PLUGIN_NAME', 'lpac' ); define( 'LPAC_PLUGIN_DIR', __DIR__ . '/' ); define( 'LPAC_PLUGIN_ASSETS_DIR', __DIR__ . '/assets/' ); @@ -117,7 +117,13 @@ function lpac_fs() define( 'LPAC_GOOGLE_MAPS_LINK', 'https://maps.googleapis.com/maps/api/js?key=' ); define( 'LPAC_GOOGLE_MAPS_API_KEY', get_option( 'lpac_google_maps_api_key', '' ) ); $site_locale = get_locale(); -define( 'LPAC_GOOGLE_MAPS_PARAMS', "&language={$site_locale}&libraries=&v=weekly" ); +$google_params = array( "language={$site_locale}", 'v=weekly' ); +$places_autocomplete = get_option( 'lpac_enable_places_autocomplete' ); +if ( !empty($places_autocomplete) ) { + array_push( $google_params, 'libraries=places' ); +} +$google_params = '&' . implode( '&', $google_params ); +define( 'LPAC_GOOGLE_MAPS_PARAMS', $google_params ); $debug = false; if ( function_exists( 'wp_get_environment_type' ) ) { /* File will only exist in local installation */ diff --git a/readme.txt b/readme.txt index a4dcc63..1563531 100644 --- a/readme.txt +++ b/readme.txt @@ -5,7 +5,7 @@ Tags: woocommerce, location picker, map, geolocation, checkout map, delivery map Requires at least: 5.5 Requires PHP: 7.0 Tested up to: 5.8 -Stable tag: 1.3.3 +Stable tag: 1.3.4 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -21,11 +21,12 @@ Location Picker At Checkout for WooCommerce(LPAC) enables store owners to get mo ## How Can Location Picker At Checkout for WooCommerce Help Me? -LPAC is a Checkout Location Picker plugin for WooCommerce that is suitable for any website that offers Delivery or Pickups for their customers. A Delivery website example would be an online restaurant, a Pickup website example would be a private taxi website. The plugin adds a Google map on the WooCommerce checkout page that customers can use to select their desired location. +LPAC is a Checkout Location Picker plugin for WooCommerce that is suitable for any website that offers Delivery or Pickups for their customers. A Delivery website example would be an online restaurant, a Pickup website example would be a private taxi website. The plugin adds a Google map on the WooCommerce checkout page that customers can use to select their desired location. Alternatively, the plugin can be set up to also allow it to automatically detect the customer's location on checkout page load and allow them to make any corrections. The plugin makes use of the Google Maps API to carry out it's functions; it can work as any of the following: ### WooCommerce Checkout Map Plugin + This plugin adds a Google Map to the checkout page of WooCommerce which customers can use to select their location whether for deliveries or pickups. ### WooCommerce Billing & Shipping Address AutoFill Plugin @@ -33,11 +34,17 @@ This plugin adds a Google Map to the checkout page of WooCommerce which customer This plugin has built-in support for automatically filling in the WooCommerce checkout fields with the information pulled from the Google map. Save users some typing while pulling accurate address information. ### WooCommerce Restaurant & Food Delivery Plugin + This plugin is excellent for online restaurants or food delivery websites that deliver customer orders after they have been placed. ### WooCommerce Pickup Plugin + If you ran a website where customers select their location for pickup then Location Picker at Checkout for WooCommerce(LPAC) would be a suitable plugin for your website. +### WooCommerce Autocomplete Checkout Address Plugin + +Though the primary focus of the plugin is to allow customers to select accurate locations by using a map. Location Picker at Checkout for WooCommerce also allows store owners to enable the Places autocomplete feature on their checkout address fields. This feature shows customers address predictions as they type in their address. When a customer clicks the desired address from the dropdown, it can also automatically fill in the rest of the checkout fields. + ### Below are a few types of stores that would benefit tremendously from this plugin: - Online Food Delivery websites @@ -52,7 +59,7 @@ If you ran a website where customers select their location for pickup then Locat ### Features: -- Detect current location of customer +- Detect current location of customer at checkout (Automatically or on click of a button) - Allow customers to pick their exact location using Google Maps - Autofill checkout fields with information pulled from Google Maps - Show/Hide Map based on Shipping Method @@ -62,6 +69,7 @@ If you ran a website where customers select their location for pickup then Locat - Include a QR Code or button link to the customer's selected location in the WooCommerce order emails. - "View on map" button to allow admin to view exact location for delivery of any order. - Customers can see the delivery location they selected on past orders. +- Option to turn on Places Autocomplete feature. [Learn More](https://lpacwp.com/docs/getting-started/google-cloud-console/places-autocomplete-feature/) - Customizable Map container - Automatically translated map buttons based on the website's language (set in WordPress' general settings) - Have a feature in mind? Feel free to submit it on the support forum. @@ -103,7 +111,7 @@ Alternatively, install this plugin by searching for it from the plugins area of = The map doesn't show = -Ensure that you have setup the plugin with your API key by going to WordPress Dashboard->WooCommerce->Location Picker At Checkout. See [this doc](https://github.com/UVLabs/location-picker-at-checkout-for-woocommerce/wiki/Getting-Your-API-Key) for getting your API key. If you've entered your API key and the map still doesn't show, then please have a look at your [browser console](https://balsamiq.com/support/faqs/browserconsole/#apple-safari) for any errors relating to the map, you can submit an issue thread on the plugin's [support forum](https://wordpress.org/support/plugin/map-location-picker-at-checkout-for-woocommerce/) +Ensure that you have setup the plugin with your API key by going to WordPress Dashboard->WooCommerce->Location Picker At Checkout. See [this doc](https://lpacwp.com/docs/getting-started/google-cloud-console/getting-your-google-maps-api-key/) for getting your API key. If you've entered your API key and the map still doesn't show, then please have a look at your [browser console](https://balsamiq.com/support/faqs/browserconsole/#apple-safari) for any errors relating to the map, you can submit an issue thread on the plugin's [support forum](https://wordpress.org/support/plugin/map-location-picker-at-checkout-for-woocommerce/) = Nothing happens when I click on the map = @@ -111,11 +119,11 @@ These sorts of issues are usually due to a JavaScript issue on the website. Chec = Map says "For development purposes only" = -This message shows when you have not finished setting up the map correctly inside the Google Cloud Console. Please make sure you've followed all the steps from the [setup guide](https://github.com/UVLabs/location-picker-at-checkout-for-woocommerce/wiki/Getting-Your-API-Key) including the "Setting up Billing & Google Monthly Credit" section. +This message shows when you have not finished setting up the map correctly inside the Google Cloud Console. Please make sure you've followed all the steps from the [setup guide](https://lpacwp.com/docs/getting-started/google-cloud-console/getting-your-google-maps-api-key/) including the "Setting up Billing & Google Monthly Credit" section. = Do I need to pay to use this plugin? = -No! The plugin is free to use. The Google Maps APIs it uses, however, do require you to setup an account on Google's Cloud Console. The process is easy and requires you also attaching a billing method to your account. Google provides a $200 monthly credit for usage of their APIs so you don't have to worry about paying anything unless you receive alot of orders on your store (upwards of 15,000 orders in a month). See [this step](https://github.com/UVLabs/location-picker-at-checkout-for-woocommerce/wiki/Getting-Your-API-Key#setting-up-billing--google-monthly-credit) in the setup guide for more details about how this works. +No! The plugin is free to use. The Google Maps APIs it uses, however, do require you to setup an account on Google's Cloud Console. The process is easy and requires you also attaching a billing method to your account. Google provides a $200 monthly credit for usage of their APIs so you don't have to worry about paying anything unless you receive alot of orders on your store (upwards of 15,000 orders in a month). See [this step](https://lpacwp.com/docs/getting-started/google-cloud-console/getting-your-google-maps-api-key/#important-setting-up-billing-google-monthly-credit) in the setup guide for more details about how this works. = Why are the plugin settings not in my language? = @@ -144,6 +152,11 @@ The way in which shipping methods are saved by the plugin has been changed. Plea == Changelog == += 1.3.5 = +* [New] Added Places Autocomplete feature [Learn More](https://lpacwp.com/docs/getting-started/google-cloud-console/places-autocomplete-feature/). +* [New] New option in General Settings to allow plugin to immediately request the customer location on checkout page load. [Learn More](https://lpacwp.com/docs/getting-started/plugin-settings/general-settings/#detect-customer-location-on-checkout-page-load). +* [Info] Tested on WC 5.9. + = 1.3.4 = * [New] Get the customer's last order location and display it on the map at checkout. * [New] Option to remove Plus Code from Google Map addresses.