diff --git a/assets/css/pickup-locations.scss b/assets/css/pickup-locations.scss
new file mode 100644
index 00000000..d6a72292
--- /dev/null
+++ b/assets/css/pickup-locations.scss
@@ -0,0 +1,174 @@
+.pickup_location_notice.hidden, #pickup_location_field.hidden, #pickup_location_customer_number_field.hidden {
+ display: none;
+}
+
+#pickup_location_field .select2-selection__clear {
+ margin-right: 5px; padding: 0 3px;
+}
+
+.woocommerce form {
+ #current_pickup_location_field {
+ display: none;
+ }
+
+ .pickup_location_notice {
+ margin-top: -.5rem;
+
+ p {
+ margin: 0;
+ padding: 0;
+ }
+
+ a.pickup-location-remove {
+ color: var(--wc-subtext);
+ font-size: 0.9em;
+ margin-top: 1rem;
+ }
+ }
+
+ .wc-gzd-shipments-managed-by-pickup-location {
+ .woocommerce-input-wrapper {
+ position: relative;
+ }
+ }
+}
+
+.wc-gzd-shipments-managed-by-pickup-location-notice {
+ font-size: 0.7em;
+ color: var(--wc-subtext);
+ line-height: 1em;
+ position: absolute;
+ right: 0;
+ top: 0;
+ margin-right: 1em;
+}
+
+body.wc-gzd-body-modal-active {
+ overflow: hidden;
+}
+
+.wc-gzd-modal-background {
+ display: none;
+ position: fixed;
+ top: 0;
+ left: 0;
+ width: 100%;
+ height: 100%;
+ background-color: #000;
+ opacity: .50;
+ filter: alpha(opacity=50);
+ z-index: 1000;
+}
+
+.select2-container--open {
+ .wc-gzd-pickup-location-select-dropdown {
+ z-index: 1051 !important;
+ }
+}
+
+.wc-gzd-modal-content {
+ display: none;
+ z-index: 1001;
+ position: fixed;
+ padding-top: 100px;
+ left: 0;
+ top: 0;
+ width: 100%;
+ height: 100%;
+ overflow: auto;
+
+ .wc-gzd-modal-close {
+ background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='40px' height='40px' viewbox='0 0 40 40'%3E%3Cpath d='M 10,10 L 30,30 M 30,10 L 10,30' stroke='black' stroke-width='4' stroke-linecap='butt' /%3E%3C/svg%3E");
+ text-indent: -9999px;
+ background-repeat: no-repeat;
+ width: 1.2em;
+ height: 1.2em;
+ background-size: contain;
+ }
+
+ .wc-gzd-modal-content-inner {
+ background-color: white;
+ border-radius: 5px;
+ margin: 0 auto;
+ width: 80%;
+
+ header {
+ background: #fcfcfc;
+ border-top-left-radius: 5px;
+ border-top-right-radius: 5px;
+ padding: 1em 1.2em;
+ border-bottom: 1px solid #ddd;
+ display: flex;
+ justify-content: space-between;
+ align-items: center;
+
+ h4 {
+ font-size: 1.2em;
+ margin: 0;
+ }
+ }
+
+ article {
+ overflow: auto;
+ padding: 1.2em;
+ }
+
+ .pickup-location-search-fields-wrapper {
+ display: flex;
+ flex-wrap: wrap;
+ justify-content: flex-start;
+
+ p.form-row {
+ float: none;
+ flex-grow: 1;
+ width: auto;
+ margin: 0;
+ margin-right: 1.5em;
+ padding: 0;
+ }
+
+ #wc-gzd-shipments-search-pickup-location-submit {
+ align-self: flex-end;
+ padding: .9rem 1.1rem;
+ }
+ }
+
+ .pickup-location-search-results {
+ margin-top: 2em;
+
+ #pickup_location_field {
+ padding: 0;
+
+ &.hidden {
+ display: none;
+ }
+ }
+ }
+
+ .pickup-location-search-actions {
+ display: flex;
+ justify-content: flex-end;
+ margin-top: 1.5em;
+ align-items: baseline;
+
+ a {
+ &.pickup-location-remove {
+ color: var(--wc-red);
+ font-size: .9em;
+ }
+
+ &.submit-pickup-location {
+ margin-left: 1.5em;
+ }
+ }
+
+ a.hidden {
+ display: none;
+ }
+ }
+ }
+}
+
+.wc-gzd-modal-background.active, .wc-gzd-modal-content.active {
+ display: block;
+}
\ No newline at end of file
diff --git a/assets/js/static/modal.js b/assets/js/static/modal.js
new file mode 100644
index 00000000..467c5c38
--- /dev/null
+++ b/assets/js/static/modal.js
@@ -0,0 +1,63 @@
+
+window.germanized = window.germanized || {};
+window.germanized.shipments_modal = window.germanized.shipments_modal || {};
+
+( function( $, germanized ) {
+
+ /**
+ * Core
+ */
+ germanized.shipments_modal = {
+ params: {},
+
+ init: function () {
+ var self = germanized.shipments_modal;
+
+ $( document ).on( 'click', '.wc-gzd-modal-launcher, .wc-gzd-modal-background, .wc-gzd-modal-close', self.onTriggerModal );
+ $( document ).on( 'click', '.wc-gzd-modal-content.active', self.detectBgClick );
+ },
+
+ detectBgClick: function( e ) {
+ if ( ! $( e.target ).closest('.wc-gzd-modal-content-inner' ).length && ! $( e.target ).is( '.wc-gzd-modal-content-inner' ) ) {
+ $( this ).find( '.wc-gzd-modal-close' ).trigger( 'click' );
+ }
+ },
+
+ onTriggerModal: function() {
+ var modalId = false;
+
+ if ( $( this ).parents( '.wc-gzd-modal-content' ).length > 0 ) {
+ modalId = $( this ).parents( '.wc-gzd-modal-content' ).data( 'id' );
+ } else {
+ modalId = $( this ).data( 'modal-id' );
+ }
+
+ if ( modalId ) {
+ if ( $( '.wc-gzd-modal-content[data-id="' + modalId + '"]' ).length > 0 ) {
+ var $modal = $( '.wc-gzd-modal-content[data-id="' + modalId + '"]' );
+
+ $modal.toggleClass( "active" );
+ $( '.wc-gzd-modal-background' ).toggleClass( "active" );
+
+ if ( $modal.hasClass( 'active' ) ) {
+ $( '.wc-gzd-modal-background' ).attr( 'data-id', modalId );
+ $( 'body' ).addClass( 'wc-gzd-body-modal-active' );
+
+ $( document.body ).trigger( 'wc_gzd_shipments_modal_open', [ modalId, $modal ] );
+ } else {
+ $( 'body' ).removeClass( 'wc-gzd-body-modal-active' );
+
+ $( document.body ).trigger( 'wc_gzd_shipments_modal_close', [ modalId, $modal ] );
+ }
+ }
+ }
+
+ return false;
+ },
+ };
+
+ $( document ).ready( function() {
+ germanized.shipments_modal.init();
+ });
+
+})( jQuery, window.germanized );
diff --git a/assets/js/static/pickup-locations.js b/assets/js/static/pickup-locations.js
index 3c4ddee6..8bea0a1c 100644
--- a/assets/js/static/pickup-locations.js
+++ b/assets/js/static/pickup-locations.js
@@ -11,6 +11,7 @@ window.germanized.shipments_pickup_locations = window.germanized.shipments_picku
params: {},
pickupLocations: {},
+ available: false,
init: function () {
var self = germanized.shipments_pickup_locations;
@@ -20,36 +21,196 @@ window.germanized.shipments_pickup_locations = window.germanized.shipments_picku
if ( $pickupSelect.length > 0 ) {
self.pickupLocations = $pickupSelect.data( 'locations' );
+ }
+
+ if ( $( '#current_pickup_location' ).length > 0 ) {
+ self.available = $( '.choose-pickup-location:visible' ).length > 0 || $( '.currently-shipping-to:visible' ).length > 0;
$( document.body ).on( 'updated_checkout', self.afterRefreshCheckout );
- $( document ).on( 'change', '#pickup_location_field #pickup_location', self.onSelectPickupLocation );
$( document ).on( 'change', '#ship-to-different-address-checkbox', self.onSelectDifferentShipping );
+ $( document ).on( 'submit', '#wc-gzd-shipments-pickup-location-search-form', self.onSearch );
+ $( document ).on( 'click', '.submit-pickup-location', self.onSelectPickupLocation );
+ $( document ).on( 'change', '#current_pickup_location', self.onChangeCurrentPickupLocation );
+ $( document ).on( 'click', '.pickup-location-remove', self.onRemovePickupLocation );
+ $( document ).on( 'change', '#pickup_location', self.onChangePickupLocation );
+ $( document ).on( 'change', '#billing_postcode, #shipping_postcode', self.onChangeAddress );
+
+ self.onChangeCurrentPickupLocation();
+ self.onChangePickupLocation();
+ self.maybeInitSelect2();
+ }
+ },
+
+ onChangeAddress: function() {
+ var self= germanized.shipments_pickup_locations,
+ postcode = $( '#shipping_postcode:visible' ).val() ? $( '#shipping_postcode:visible' ).val() : $( '#billing_postcode' ).val();
+
+ $( '#pickup-location-postcode' ).val( postcode );
+ },
+
+ onChangePickupLocation: function() {
+ var self= germanized.shipments_pickup_locations,
+ $pickupSelect = self.getPickupLocationSelect();
+
+ if ( $pickupSelect.val() ) {
+ $( '.pickup-location-search-actions' ).find( '.submit-pickup-location' ).show();
+ } else {
+ $( '.pickup-location-search-actions' ).find( '.submit-pickup-location' ).hide();
+ }
+ },
+
+ hasPickupLocationDelivery: function() {
+ var self = germanized.shipments_pickup_locations,
+ $current = $( '#current_pickup_location' ),
+ currentCode = $current.val();
+
+ if ( currentCode ) {
+ return true;
+ }
+
+ return false;
+ },
+
+ disablePickupLocationDelivery: function() {
+ var self= germanized.shipments_pickup_locations,
+ $modal = $( '.wc-gzd-modal-content[data-id="pickup-location"].active' );
+
+ $( '.wc-gzd-shipments-managed-by-pickup-location' ).val( '' );
+ $( '#current_pickup_location' ).val( '' ).trigger( 'change' );
+
+ if ( $modal.length > 0 ) {
+ $modal.find( '.wc-gzd-modal-close' ).trigger( 'click' );
+ }
+ },
+
+ onRemovePickupLocation: function() {
+ var self= germanized.shipments_pickup_locations;
+
+ self.disablePickupLocationDelivery();
+
+ return false;
+ },
- self.afterRefreshCheckout();
+ getCustomerNumberField: function() {
+ return $( '#pickup_location_customer_number_field' );
+ },
+
+ onChangeCurrentPickupLocation: function() {
+ var self = germanized.shipments_pickup_locations,
+ $current = $( '#current_pickup_location' ),
+ currentCode = $current.val(),
+ currentPickupLocation = currentCode ? self.getPickupLocation( currentCode ) : false,
+ $notice = $( '.pickup_location_notice' );
+
+ if ( currentCode && currentPickupLocation ) {
+ $current.attr( 'data-current-location', currentPickupLocation );
+
+ self.replaceShippingAddress( currentPickupLocation.address_replacements );
+ self.updateCustomerNumberField( currentPickupLocation );
+
+ $notice.find( '.pickup-location-manage-link' ).text( currentPickupLocation.label );
+ $notice.find( '.currently-shipping-to' ).show();
+ $notice.find( '.choose-pickup-location' ).hide();
+ } else {
+ $current.attr( 'data-current-location', '' );
+ $current.val( '' );
+
+ self.getCustomerNumberField().addClass( 'hidden' );
+ self.getCustomerNumberField().hide();
+
+ console.log($( '.wc-gzd-shipments-managed-by-pickup-location' ).find( 'input[type=text]' ));
+
+ $( '.wc-gzd-shipments-managed-by-pickup-location' ).find( 'input[type=text]' ).val( '' );
+ $( '.wc-gzd-shipments-managed-by-pickup-location' ).find( ':input' ).prop( 'readonly', false );
+
+ $( '.wc-gzd-shipments-managed-by-pickup-location' ).removeClass( 'wc-gzd-shipments-managed-by-pickup-location' );
+ $( '.wc-gzd-shipments-managed-by-pickup-location-notice' ).remove();
+
+ $notice.find( '.currently-shipping-to' ).hide();
+ $notice.find( '.choose-pickup-location' ).show();
}
},
+ onSearch: function() {
+ var self = germanized.shipments_pickup_locations,
+ $form = $( this ),
+ params = $form.serialize(),
+ $pickupSelect = self.getPickupLocationSelect(),
+ current = $pickupSelect.val();
+
+ $( '#wc-gzd-shipments-pickup-location-search-form' ).block({
+ message: null,
+ overlayCSS: {
+ background: '#fff',
+ opacity: 0.6
+ }
+ });
+
+ params['action'] = 'woocommerce_gzd_shipments_search_pickup_locations';
+
+ $.ajax({
+ type: "POST",
+ url: self.params.wc_ajax_url.toString().replace('%%endpoint%%', 'woocommerce_gzd_shipments_search_pickup_locations'),
+ data: params,
+ success: function( data ) {
+ if ( data.success ) {
+ self.pickupLocations = data.locations;
+ self.updatePickupLocationSelect();
+
+ $( '#wc-gzd-shipments-pickup-location-search-form' ).unblock();
+ }
+ },
+ error: function( data ) {},
+ dataType: 'json'
+ });
+
+ return false;
+ },
+
+ updatePickupLocationSelect: function() {
+ var self = germanized.shipments_pickup_locations,
+ $pickupSelect = self.getPickupLocationSelect(),
+ current = $pickupSelect.val();
+
+ $pickupSelect.attr('data-locations', self.pickupLocations );
+ $pickupSelect.find( 'option:not([value=""])' ).remove();
+
+ $.each( self.pickupLocations, function( code, pickupLocation ) {
+ var label = $( '' ).html( pickupLocation.formatted_address ).text();
+ $pickupSelect.append( $( "" ).attr("value", code ).text( label ) );
+ });
+
+ var currentLocation = self.getPickupLocation( current );
+
+ if ( currentLocation ) {
+ $pickupSelect.find( 'option[value="' + currentLocation.code + '"' )[0].selected = true;
+ }
+
+ $pickupSelect.trigger( 'change' );
+ },
+
onSelectDifferentShipping: function() {
- var self = germanized.shipments_pickup_locations,
- $pickupSelect = self.getPickupLocationSelect();
+ var self= germanized.shipments_pickup_locations;
if ( ! $( this ).is( ':checked' ) ) {
- if ( $pickupSelect.val() ) {
- $pickupSelect.val( '' ).trigger( 'change' );
- }
+ self.disablePickupLocationDelivery();
+
+ $( '#billing_pickup_location_notice' ).show();
+ } else {
+ $( '#billing_pickup_location_notice' ).hide();
}
},
maybeInitSelect2: function() {
if ( $().selectWoo ) {
- $( 'select#pickup_location:visible, select#pickup_location:visible' ).each( function() {
+ $( 'select#pickup_location' ).each( function() {
var $this = $( this );
var select2_args = {
placeholder: $this.attr( 'data-placeholder' ) || $this.attr( 'placeholder' ) || '',
label: $this.attr( 'data-label' ) || null,
width: '100%',
- allowClear: true
+ dropdownCssClass: "wc-gzd-pickup-location-select-dropdown"
};
$( this )
@@ -64,25 +225,21 @@ window.germanized.shipments_pickup_locations = window.germanized.shipments_picku
onSelectPickupLocation: function() {
var self = germanized.shipments_pickup_locations,
$pickupSelect = self.getPickupLocationSelect(),
- $customerNumberField = $( '#pickup_location_customer_number_field' ),
current = $pickupSelect.val();
- if ( ! $pickupSelect.val() ) {
- $customerNumberField.addClass( 'hidden' );
- $customerNumberField.hide();
- } else {
- var currentLocation = self.getPickupLocation( current );
+ $( '#current_pickup_location' ).val( current ).trigger( 'change' );
+ $( this ).parents( '.wc-gzd-modal-content' ).find( '.wc-gzd-modal-close' ).trigger( 'click' );
- if ( currentLocation ) {
- self.updateCustomerNumberField( currentLocation );
- }
- }
+ var scrollElement = $( '#shipping_address_1_field' );
+
+ $.scroll_to_notices( scrollElement );
- $( document.body ).trigger( 'update_checkout' );
+ return false;
},
updateCustomerNumberField: function( currentLocation ) {
- var $customerNumberField = $( '#pickup_location_customer_number_field' );
+ var self = germanized.shipments_pickup_locations,
+ $customerNumberField = self.getCustomerNumberField();
if ( currentLocation.supports_customer_number ) {
// Do not replace via .text() to prevent removing inner html elements, e.g. optional label.
@@ -119,6 +276,16 @@ window.germanized.shipments_pickup_locations = window.germanized.shipments_picku
if ( self.pickupLocations.hasOwnProperty( locationCode ) ) {
return self.pickupLocations[ locationCode ];
+ } else {
+ var $select = $( '#current_pickup_location' );
+
+ if ( $select.data( 'current-location' ) ) {
+ var currentLocation = $select.data( 'current-location' );
+
+ if ( currentLocation.code === locationCode ) {
+ return currentLocation;
+ }
+ }
}
return false;
@@ -126,76 +293,69 @@ window.germanized.shipments_pickup_locations = window.germanized.shipments_picku
afterRefreshCheckout: function( e, ajaxData ) {
var self = germanized.shipments_pickup_locations,
- $pickupSelect = self.getPickupLocationSelect(),
- $pickupSelectField = $pickupSelect.parents( '#pickup_location_field' ),
- $customerNumberField = $( '#pickup_location_customer_number_field' ),
- current = $pickupSelect.val();
+ supportsPickupLocationDelivery = false;
ajaxData = ( typeof ajaxData === 'undefined' ) ? {
'fragments': {
+ '.gzd-shipments-pickup-location-supported': false,
'.gzd-shipments-pickup-locations': JSON.stringify( self.pickupLocations ),
}
} : ajaxData;
- if ( ajaxData.hasOwnProperty( 'fragments' ) && ajaxData.fragments.hasOwnProperty( '.gzd-shipments-pickup-locations' ) ) {
- self.pickupLocations = JSON.parse( ajaxData.fragments['.gzd-shipments-pickup-locations'] );
- } else {
- self.pickupLocations = {};
- }
-
- $pickupSelect.attr('data-locations', self.pickupLocations );
+ if ( ajaxData.hasOwnProperty( 'fragments' ) ) {
+ if ( ajaxData.fragments.hasOwnProperty( '.gzd-shipments-pickup-location-supported' ) ) {
+ supportsPickupLocationDelivery = ajaxData.fragments['.gzd-shipments-pickup-location-supported'];
+ }
+ if ( ajaxData.fragments.hasOwnProperty( '.gzd-shipments-pickup-locations' ) && Object.keys( self.pickupLocations ).length <= 0 ) {
+ self.pickupLocations = JSON.parse( ajaxData.fragments['.gzd-shipments-pickup-locations'] );
- if ( Object.keys( self.pickupLocations ).length ) {
- $pickupSelectField.show();
- $pickupSelectField.removeClass( 'hidden' );
+ self.updatePickupLocationSelect();
+ }
+ }
- $pickupSelect.find( 'option:not([value=""])' ).remove();
+ if ( ! supportsPickupLocationDelivery ) {
+ self.disable();
+ } else {
+ self.enable();
+ }
+ },
- $.each( self.pickupLocations, function( code, pickupLocation ) {
- var label = $( '' ).html( pickupLocation.formatted_address).text();
- $pickupSelect.append( $( "" ).attr("value", code ).text( label ) );
- });
+ disable: function() {
+ var self = germanized.shipments_pickup_locations;
- var currentLocation = self.getPickupLocation( current );
+ self.available = false;
- if ( currentLocation ) {
- $pickupSelect.find( 'option[value="' + currentLocation.code + '"' )[0].selected = true;
+ if ( self.hasPickupLocationDelivery() ) {
+ self.disablePickupLocationDelivery();
- self.replaceShippingAddress( currentLocation.address_replacements );
- self.updateCustomerNumberField( currentLocation );
- } else {
- $customerNumberField.addClass( 'hidden' );
- $customerNumberField.hide();
+ var $form = $( 'form.checkout' );
- $pickupSelect.val( "" );
+ if ( $form.find( '.woocommerce-NoticeGroup-updateOrderReview' ).length <= 0 ) {
+ $form.prepend( '
' );
}
- } else {
- if ( "" !== current ) {
- $( '#shipping_address_1' ).val( "" );
- var $form = $( 'form.checkout' );
+ $form.find( '.woocommerce-NoticeGroup-updateOrderReview' ).prepend( '' + self.params.i18n_pickup_location_delivery_unavailable + '
' );
- if ( $form.find( '.woocommerce-NoticeGroup-updateOrderReview' ).length <= 0 ) {
- $form.prepend( '' );
- }
+ var scrollElement = $( '.woocommerce-NoticeGroup-updateOrderReview' );
- $form.find( '.woocommerce-NoticeGroup-updateOrderReview' ).prepend( 'Your selected pickup location is not available any longer. Please review your shipping address.
' );
+ $.scroll_to_notices( scrollElement );
+ }
- var scrollElement = $( '.woocommerce-NoticeGroup-updateOrderReview' );
+ $( '.pickup_location_notice' ).hide();
+ },
- $.scroll_to_notices( scrollElement );
- }
+ enable: function() {
+ var self = germanized.shipments_pickup_locations;
- $pickupSelectField.addClass( 'hidden' );
- $pickupSelectField.hide();
+ self.available = true;
- $customerNumberField.addClass( 'hidden' );
- $customerNumberField.hide();
+ $( '.pickup_location_notice' ).show();
- $pickupSelect.val( "" );
+ if ( $( '#ship-to-different-address-checkbox' ).is( ':checked' ) || self.hasPickupLocationDelivery() ) {
+ $( '#billing_pickup_location_notice' ).hide();
+ } else {
+ $( '#billing_pickup_location_notice' ).show();
}
-
- self.maybeInitSelect2();
},
replaceShippingAddress: function( replacements ) {
@@ -213,6 +373,23 @@ window.germanized.shipments_pickup_locations = window.germanized.shipments_picku
}
$( '#shipping_' + addressField ).val( value );
+ $( '#shipping_' + addressField ).prop( 'readonly', true );
+
+ if ( 'country' === addressField ) {
+ $( '#shipping_' + addressField ).trigger( 'change' ); // select2 needs a change event
+ }
+
+ var $row = $( '#shipping_' + addressField + '_field' );
+
+ if ( $row.length > 0 ) {
+ $row.addClass( 'wc-gzd-shipments-managed-by-pickup-location' );
+
+ if ( 'country' !== addressField && $row.find( '.wc-gzd-shipments-managed-by-pickup-location-notice' ).length <= 0 ) {
+ $row.find( ':input' ).after( '' + self.params.i18n_managed_by_pickup_location + '' );
+ }
+ } else {
+ $( '#shipping_' + addressField ).addClass( 'wc-gzd-shipments-managed-by-pickup-location' );
+ }
}
}
});
diff --git a/includes/wc-gzd-shipment-functions.php b/includes/wc-gzd-shipment-functions.php
index 7407b8ef..7433e701 100644
--- a/includes/wc-gzd-shipment-functions.php
+++ b/includes/wc-gzd-shipment-functions.php
@@ -37,6 +37,28 @@ function wc_gzd_country_to_alpha3( $country ) {
return Package::get_country_iso_alpha3( $country );
}
+function wc_gzd_get_customer_preferred_shipping_provider( $user_id ) {
+ $default_provider = wc_gzd_get_default_shipping_provider();
+ $provider = false;
+
+ if ( ! $default_provider ) {
+ $available = wc_gzd_get_available_shipping_providers();
+ $default_provider = 1 === count( $available ) ? array_values( $available )[0] : false;
+ }
+
+ if ( $customer = new WC_Customer( $user_id ) ) {
+ if ( $last_order = $customer->get_last_order() ) {
+ $provider = wc_gzd_get_order_shipping_provider( $last_order );
+ }
+ }
+
+ if ( ! $provider && $default_provider ) {
+ $provider = wc_gzd_get_shipping_provider( $default_provider );
+ }
+
+ return apply_filters( 'woocommerce_gzd_customer_shipping_provider', $provider, $user_id );
+}
+
function wc_gzd_country_to_alpha2( $country ) {
return Package::get_country_iso_alpha2( $country );
}
diff --git a/src/Blocks/Checkout.php b/src/Blocks/Checkout.php
index e0efb66a..01d6ed94 100644
--- a/src/Blocks/Checkout.php
+++ b/src/Blocks/Checkout.php
@@ -61,7 +61,9 @@ private function get_checkout_data_from_request( $request ) {
* @return void
*/
private function validate_checkout_data( $order, $request ) {
- $gzd_data = $this->get_checkout_data_from_request( $request );
+ $gzd_data = $this->get_checkout_data_from_request( $request );
+ $pickup_location = false;
+ $pickup_location_customer_number = '';
if ( $this->has_checkout_data( 'pickup_location', $request ) && ! empty( $gzd_data['pickup_location'] ) ) {
$pickup_location_code = $gzd_data['pickup_location'];
@@ -111,29 +113,37 @@ private function validate_checkout_data( $order, $request ) {
if ( $supports_customer_number ) {
$order->update_meta_data( '_pickup_location_customer_number', $pickup_location_customer_number );
}
+ }
+
+ if ( $order->get_customer_id() ) {
+ $wc_customer = new \WC_Customer( $order->get_customer_id() );
- /**
- * Persist customer changes for logged-in customers.
- */
- if ( $order->get_customer_id() ) {
- $wc_customer = new \WC_Customer( $order->get_customer_id() );
+ $wc_customer->update_meta_data( 'pickup_location_code', '' );
+ $wc_customer->update_meta_data( 'pickup_location_customer_number', '' );
- $wc_customer->update_meta_data( 'pickup_location_code', $pickup_location_code );
+ if ( $pickup_location ) {
+ $wc_customer->update_meta_data( 'pickup_location_code', $pickup_location->get_code() );
$pickup_location->replace_address( $wc_customer );
- if ( $supports_customer_number ) {
+ if ( $pickup_location->supports_customer_number() ) {
$wc_customer->update_meta_data( 'pickup_location_customer_number', $pickup_location_customer_number );
}
-
- $wc_customer->save();
}
- $customer = wc()->customer;
- $customer->update_meta_data( 'pickup_location_code', $pickup_location_code );
- $pickup_location->replace_address( $customer );
+ $wc_customer->save();
+ }
+
+ if ( $customer = wc()->customer ) {
+ $customer->update_meta_data( 'pickup_location_code', '' );
+ $customer->update_meta_data( 'pickup_location_customer_number', '' );
- if ( $supports_customer_number ) {
- $customer->update_meta_data( 'pickup_location_customer_number', $pickup_location_customer_number );
+ if ( $pickup_location ) {
+ $customer->update_meta_data( 'pickup_location_code', $pickup_location->get_code() );
+ $pickup_location->replace_address( $customer );
+
+ if ( $pickup_location->supports_customer_number() ) {
+ $customer->update_meta_data( 'pickup_location_customer_number', $pickup_location_customer_number );
+ }
}
$customer->save();
diff --git a/src/PickupDelivery.php b/src/PickupDelivery.php
index 9de1ebd0..493caadc 100644
--- a/src/PickupDelivery.php
+++ b/src/PickupDelivery.php
@@ -16,21 +16,283 @@ class PickupDelivery {
*/
public static function init() {
add_filter( 'woocommerce_order_formatted_shipping_address', array( __CLASS__, 'set_formatted_shipping_address' ), 20, 2 );
- add_filter( 'woocommerce_my_account_my_address_formatted_address', array( __CLASS__, 'set_formatted_user_shipping_address' ), 10, 3 );
+ add_filter( 'woocommerce_my_account_my_address_formatted_address', array( __CLASS__, 'set_formatted_customer_shipping_address' ), 10, 3 );
add_filter( 'woocommerce_formatted_address_replacements', array( __CLASS__, 'formatted_shipping_replacements' ), 20, 2 );
+ add_action( 'wp_enqueue_scripts', array( __CLASS__, 'register_assets' ), 100 );
+
+ add_action(
+ 'woocommerce_after_edit_address_form_shipping',
+ function() {
+ self::pickup_location_search_modal();
+ }
+ );
+
+ add_action(
+ 'woocommerce_after_save_address_validation',
+ function( $user_id, $address_type, $address, $customer ) {
+ if ( ! self::is_available() ) {
+ return;
+ }
+
+ $pickup_location_code = isset( $_POST['current_pickup_location'] ) ? wc_clean( wp_unslash( $_POST['current_pickup_location'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
+ $pickup_location_customer_number = isset( $_POST['pickup_location_customer_number'] ) ? wc_clean( wp_unslash( $_POST['pickup_location_customer_number'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
+
+ if ( empty( $pickup_location_code ) ) {
+ $pickup_location_code = '';
+ }
+
+ $customer->update_meta_data( 'pickup_location_code', '' );
+ $customer->update_meta_data( 'pickup_location_customer_number', '' );
+
+ if ( ! empty( $pickup_location_code ) ) {
+ if ( $provider = wc_gzd_get_customer_preferred_shipping_provider( $customer ) ) {
+ if ( is_a( $provider, 'Vendidero\Germanized\Shipments\Interfaces\ShippingProviderAuto' ) ) {
+ $address_data = array(
+ 'country' => $customer->get_shipping_country(),
+ 'postcode' => $customer->get_shipping_postcode(),
+ 'city' => $customer->get_shipping_city(),
+ 'address_1' => $customer->get_shipping_address_1(),
+ );
+
+ if ( $provider->supports_pickup_location_delivery( $address_data ) ) {
+ if ( $pickup_location = $provider->get_pickup_location_by_code( $pickup_location_code, $address_data ) ) {
+ if ( $pickup_location->supports_customer_number() ) {
+ if ( ! empty( $pickup_location_customer_number ) || $pickup_location->customer_number_is_mandatory() ) {
+ if ( ! $validation = $pickup_location->customer_number_is_valid( $pickup_location_customer_number ) ) {
+ if ( is_a( $validation, 'WP_Error' ) ) {
+ wc_add_notice( $validation->get_error_message(), 'error' );
+ } else {
+ wc_add_notice( _x( 'Sorry, your pickup location customer number is invalid.', 'shipments', 'woocommerce-germanized-shipments' ), 'error' );
+ }
+
+ return;
+ }
+ }
+
+ $customer->update_meta_data( 'pickup_location_code', $pickup_location_code );
+ $customer->update_meta_data( 'pickup_location_customer_number', $pickup_location_customer_number );
+
+ $pickup_location->replace_address( $customer );
+ }
+ }
+ }
+ }
+ }
+ }
+ },
+ 10,
+ 4
+ );
+
+ add_filter(
+ 'woocommerce_address_to_edit',
+ function( $address_fields, $load_address ) {
+ if ( ! self::is_available() ) {
+ return $address_fields;
+ }
+
+ if ( 'shipping' === $load_address ) {
+ $pickup_delivery_data = self::get_pickup_location_data( 'customer' );
+
+ $address_fields['shipping_pickup_location_notice'] = array(
+ 'type' => 'wc_gzd_shipments_pickup_location_notice',
+ 'hidden' => $pickup_delivery_data['supports_pickup_delivery'] ? false : true,
+ 'current_location' => $pickup_delivery_data['current_location'],
+ 'label' => '',
+ 'priority' => 61,
+ 'required' => false,
+ 'value' => '',
+ );
+
+ $address_fields['pickup_location_customer_number'] = array(
+ 'type' => 'wc_gzd_shipments_pickup_location_customer_number',
+ 'label' => $pickup_delivery_data['customer_number_field_label'],
+ 'current_location' => $pickup_delivery_data['current_location'],
+ 'default' => $pickup_delivery_data['current_location_customer_number'],
+ 'priority' => 62,
+ 'value' => $pickup_delivery_data['current_location_customer_number'],
+ );
+
+ $address_fields['current_pickup_location'] = array(
+ 'type' => 'wc_gzd_shipments_current_pickup_location',
+ 'current_location' => $pickup_delivery_data['current_location'],
+ 'default' => $pickup_delivery_data['current_location_code'],
+ 'label' => '',
+ 'value' => $pickup_delivery_data['current_location'] ? $pickup_delivery_data['current_location']->get_code() : '',
+ );
+ }
+
+ return $address_fields;
+ },
+ 10,
+ 2
+ );
+
+ add_action( 'woocommerce_after_checkout_form', array( __CLASS__, 'pickup_location_search_modal' ) );
add_filter( 'woocommerce_checkout_fields', array( __CLASS__, 'register_classic_checkout_fields' ), 10 );
add_filter( 'woocommerce_update_order_review_fragments', array( __CLASS__, 'register_order_review_fragments' ), 10, 1 );
add_action( 'woocommerce_after_checkout_validation', array( __CLASS__, 'register_classic_checkout_validation' ), 10, 2 );
add_action( 'woocommerce_checkout_create_order', array( __CLASS__, 'register_classic_checkout_order_data' ), 10, 2 );
- add_action( 'wp_enqueue_scripts', array( __CLASS__, 'register_classic_checkout_scripts' ), 100 );
+ add_action( 'wp_ajax_woocommerce_gzd_shipments_search_pickup_locations', array( __CLASS__, 'search_pickup_locations' ) );
+ add_action( 'wp_ajax_nopriv_woocommerce_gzd_shipments_search_pickup_locations', array( __CLASS__, 'search_pickup_locations' ) );
+ add_action( 'wc_ajax_woocommerce_gzd_shipments_search_pickup_locations', array( __CLASS__, 'search_pickup_locations' ) );
+
+ add_filter( 'woocommerce_form_field_wc_gzd_shipments_current_pickup_location', array( __CLASS__, 'register_current_pickup_location_field' ), 10, 4 );
add_filter( 'woocommerce_form_field_wc_gzd_shipments_pickup_location', array( __CLASS__, 'register_pickup_location_field' ), 10, 4 );
add_filter( 'woocommerce_form_field_wc_gzd_shipments_pickup_location_customer_number', array( __CLASS__, 'register_pickup_location_customer_number_field' ), 10, 4 );
+ add_filter( 'woocommerce_form_field_wc_gzd_shipments_pickup_location_notice', array( __CLASS__, 'register_pickup_location_notice_field' ), 10, 4 );
add_filter( 'woocommerce_admin_shipping_fields', array( __CLASS__, 'register_pickup_location_admin_fields' ), 10, 3 );
}
+ protected static function get_pickup_location_data( $context = 'checkout', $retrieve_locations = false ) {
+ $customer = wc()->customer;
+ $query_args = array();
+ $provider = false;
+ $result = array(
+ 'address' => array(
+ 'country' => '',
+ 'postcode' => '',
+ 'address_1' => '',
+ ),
+ 'supports_pickup_delivery' => false,
+ 'current_location_code' => null,
+ 'current_location_customer_number' => '',
+ 'customer_number_field_label' => _x( 'Customer Number', 'shipments', 'woocommerce-germanized-shipments' ),
+ 'locations' => array(),
+ );
+
+ if ( $method = wc_gzd_get_current_shipping_provider_method() ) {
+ $provider = $method->get_shipping_provider_instance();
+ }
+
+ if ( 'customer' === $context ) {
+ $customer = new \WC_Customer( get_current_user_id() );
+ $provider = wc_gzd_get_customer_preferred_shipping_provider( $customer );
+ } elseif ( 'checkout' === $context ) {
+ $query_args = self::get_pickup_delivery_cart_args();
+ }
+
+ if ( $customer ) {
+ $result['address'] = array(
+ 'country' => $customer->get_shipping_country() ? $customer->get_shipping_country() : $customer->get_billing_country(),
+ 'postcode' => $customer->get_shipping_postcode() ? $customer->get_shipping_postcode() : $customer->get_billing_postcode(),
+ 'address_1' => $customer->get_shipping_address_1() ? $customer->get_shipping_address_1() : $customer->get_billing_address_1(),
+ );
+ }
+
+ if ( is_a( $provider, 'Vendidero\Germanized\Shipments\Interfaces\ShippingProviderAuto' ) ) {
+ if ( $provider->supports_pickup_location_delivery( $result['address'], $query_args ) ) {
+ $result['supports_pickup_delivery'] = true;
+
+ if ( $retrieve_locations ) {
+ $result['locations'] = $provider->get_pickup_locations( $result['address'], $query_args );
+ }
+
+ $current_location_code = self::get_pickup_location_code_by_customer( $customer );
+
+ if ( $current_location_code ) {
+ if ( $current_location = $provider->get_pickup_location_by_code( $current_location_code, $result['address'] ) ) {
+
+ if ( $retrieve_locations ) {
+ $result['locations'][] = $current_location;
+ }
+
+ if ( $current_location->supports_customer_number() ) {
+ $result['customer_number_field_label'] = $current_location->get_customer_number_field_label();
+ $result['current_location_customer_number'] = self::get_pickup_location_customer_number_by_customer( $customer );
+ }
+
+ $result['current_location_code'] = $current_location_code;
+ $result['current_location'] = $current_location;
+ }
+ }
+ }
+ }
+
+ return $result;
+ }
+
+ public static function pickup_location_search_modal( $context = 'checkout' ) {
+ if ( ! self::is_available() ) {
+ return;
+ }
+
+ $pickup_delivery_data = self::get_pickup_location_data( $context, true );
+ ?>
+
+
+
+ version, '8.6.0', '<' ) ) {
global $theorder;
@@ -67,6 +329,38 @@ public static function register_pickup_location_admin_fields( $fields, $order =
return $fields;
}
+ public static function register_pickup_location_notice_field( $field, $key, $args, $value ) {
+ $args = wp_parse_args(
+ $args,
+ array(
+ 'id' => $key,
+ 'hidden' => true,
+ 'current_location' => null,
+ )
+ );
+ ob_start();
+ ?>
+
+ null,
'required' => false,
'custom_attributes' => array(),
+ 'current_location' => null,
'hidden' => true,
'class' => array(),
)
);
- $args['options'] = array(
- '' => _x( 'None', 'shipments-default-pickup-location', 'woocommerce-germanized-shipments' ),
+ $args['type'] = 'hidden';
+ $args['return'] = true;
+ $args['custom_attributes']['data-current-location'] = $args['current_location'] ? wp_json_encode( $args['current_location']->get_data() ) : '';
+
+ $field = woocommerce_form_field( $key, $args, $value );
+
+ return $field;
+ }
+
+ public static function register_pickup_location_field( $field, $key, $args, $value ) {
+ $args = wp_parse_args(
+ $args,
+ array(
+ 'locations' => array(),
+ 'id' => $key,
+ 'priority' => '',
+ 'required' => false,
+ 'custom_attributes' => array(),
+ 'current_location' => null,
+ 'hidden' => true,
+ 'class' => array(),
+ )
);
+
+ $args['options'] = array();
$args['custom_attributes']['data-locations'] = array();
$args['type'] = 'select';
- $args['placeholder'] = _x( 'Select pickup location', 'shipments', 'woocommerce-germanized-shipments' );
+ $args['placeholder'] = _x( 'No pickup location found', 'shipments', 'woocommerce-germanized-shipments' );
$args['return'] = true;
foreach ( $args['locations'] as $location ) {
@@ -131,9 +448,22 @@ public static function register_pickup_location_field( $field, $key, $args, $val
$args['custom_attributes']['data-locations'][ $location->get_code() ] = $location->get_data();
}
+ if ( $args['current_location'] ) {
+ if ( ! array_key_exists( $args['current_location']->get_code(), $args['options'] ) ) {
+ $args['options'][ $args['current_location']->get_code() ] = $location->get_formatted_address();
+ $args['custom_attributes']['data-locations'][ $args['current_location']->get_code() ] = $args['current_location']->get_data();
+ }
+
+ $args['default'] = $args['current_location']->get_code();
+ }
+
+ if ( empty( $args['options'] ) ) {
+ $args['options'][] = _x( 'Search pickup locations', 'shipments', 'woocommerce-germanized-shipments' );
+ }
+
$args['custom_attributes']['data-locations'] = wp_json_encode( $args['custom_attributes']['data-locations'] );
- if ( count( $args['options'] ) > 1 ) {
+ if ( count( $args['options'] ) > 0 ) {
$args['hidden'] = false;
}
@@ -157,10 +487,11 @@ public static function register_classic_checkout_order_data( $order, $data ) {
return;
}
- $pickup_location_code = isset( $data['pickup_location'] ) ? trim( wc_clean( $data['pickup_location'] ) ) : '';
+ $pickup_location_code = isset( $data['current_pickup_location'] ) ? trim( wc_clean( $data['current_pickup_location'] ) ) : '';
$pickup_location_customer_number = isset( $data['pickup_location_customer_number'] ) ? trim( wc_clean( $data['pickup_location_customer_number'] ) ) : '';
+ $pickup_location = false;
- if ( '-1' === $pickup_location_code || empty( $pickup_location_code ) ) {
+ if ( empty( $pickup_location_code ) ) {
$pickup_location_code = '';
}
@@ -187,23 +518,28 @@ public static function register_classic_checkout_order_data( $order, $data ) {
}
$pickup_location->replace_address( $order );
+ }
+ }
+ }
+ }
+ }
- if ( $order->get_customer_id() ) {
- $wc_customer = new \WC_Customer( $order->get_customer_id() );
+ if ( $order->get_customer_id() ) {
+ $wc_customer = new \WC_Customer( $order->get_customer_id() );
- $wc_customer->update_meta_data( 'pickup_location_code', $pickup_location_code );
- $pickup_location->replace_address( $wc_customer );
+ $wc_customer->update_meta_data( 'pickup_location_code', '' );
+ $wc_customer->update_meta_data( 'pickup_location_customer_number', '' );
- if ( $pickup_location->supports_customer_number() ) {
- $wc_customer->update_meta_data( 'pickup_location_customer_number', $pickup_location_customer_number );
- }
+ if ( $pickup_location ) {
+ $wc_customer->update_meta_data( 'pickup_location_code', $pickup_location_code );
+ $pickup_location->replace_address( $wc_customer );
- $wc_customer->save();
- }
- }
- }
+ if ( $pickup_location->supports_customer_number() ) {
+ $wc_customer->update_meta_data( 'pickup_location_customer_number', $pickup_location_customer_number );
}
}
+
+ $wc_customer->save();
}
}
@@ -218,10 +554,10 @@ public static function register_classic_checkout_validation( $data, $errors ) {
return;
}
- $pickup_location_code = isset( $data['pickup_location'] ) ? trim( wc_clean( $data['pickup_location'] ) ) : '';
+ $pickup_location_code = isset( $data['current_pickup_location'] ) ? trim( wc_clean( $data['current_pickup_location'] ) ) : '';
$pickup_location_customer_number = isset( $data['pickup_location_customer_number'] ) ? wc_clean( $data['pickup_location_customer_number'] ) : '';
- if ( '-1' === $pickup_location_code || empty( $pickup_location_code ) ) {
+ if ( empty( $pickup_location_code ) ) {
$pickup_location_code = '';
}
@@ -268,15 +604,43 @@ public static function register_order_review_fragments( $fragments ) {
return $fragments;
}
+ $pickup_delivery_data = self::get_pickup_location_data( 'checkout', true );
+
+ if ( ! empty( $pickup_delivery_data['locations'] ) ) {
+ $new_locations = array();
+
+ foreach ( $pickup_delivery_data['locations'] as $location ) {
+ $new_locations[ $location->get_code() ] = $location->get_data();
+ }
+
+ $locations = $new_locations;
+ }
+
+ $fragments['.gzd-shipments-pickup-locations'] = wp_json_encode( $locations );
+ $fragments['.gzd-shipments-pickup-location-supported'] = $pickup_delivery_data['supports_pickup_delivery'];
+
+ return $fragments;
+ }
+
+ public static function search_pickup_locations() {
+ check_ajax_referer( 'wc-gzd-shipments-search-pickup-location' );
+
+ $postcode = isset( $_POST['pickup_location_postcode'] ) ? wc_clean( wp_unslash( $_POST['pickup_location_postcode'] ) ) : '';
+ $address_1 = isset( $_POST['pickup_location_address'] ) ? wc_clean( wp_unslash( $_POST['pickup_location_address'] ) ) : '';
+
+ if ( empty( $postcode ) ) {
+ $postcode = wc()->customer->get_shipping_postcode() ? wc()->customer->get_shipping_postcode() : wc()->customer->get_billing_postcode();
+ }
+
$locations = array();
if ( $method = wc_gzd_get_current_shipping_provider_method() ) {
if ( $provider = $method->get_shipping_provider_instance() ) {
if ( is_a( $provider, 'Vendidero\Germanized\Shipments\Interfaces\ShippingProviderAuto' ) ) {
$address = array(
- 'country' => wc()->customer->get_shipping_country(),
- 'postcode' => wc()->customer->get_shipping_postcode(),
- 'address_1' => wc()->customer->get_shipping_address_1(),
+ 'country' => wc()->customer->get_shipping_country() ? wc()->customer->get_shipping_country() : wc()->customer->get_billing_country(),
+ 'postcode' => $postcode,
+ 'address_1' => $address_1,
);
$query_args = self::get_pickup_delivery_cart_args();
@@ -298,23 +662,40 @@ public static function register_order_review_fragments( $fragments ) {
$locations = $new_locations;
}
- $fragments['.gzd-shipments-pickup-locations'] = wp_json_encode( $locations );
+ wp_send_json(
+ array(
+ 'success' => true,
+ 'locations' => $locations,
+ )
+ );
+ }
- return $fragments;
+ protected static function is_edit_address_page() {
+ global $wp;
+
+ return is_account_page() && isset( $wp->query_vars['edit-address'] ) && 'shipping' === $wp->query_vars['edit-address'];
}
- public static function register_classic_checkout_scripts() {
- if ( ! is_checkout() || ! self::is_available() ) {
+ public static function register_assets() {
+ if ( ( ! is_checkout() && ! self::is_edit_address_page() ) || ! self::is_available() ) {
return;
}
- wp_register_script( 'wc-gzd-shipments-pickup-locations', Package::get_assets_url( 'static/pickup-locations.js' ), array( 'jquery', 'wc-checkout' ), Package::get_version() ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter
- wp_add_inline_style( 'woocommerce-layout', '#pickup_location_field.hidden, #pickup_location_customer_number_field.hidden { display: none; } #pickup_location_field .select2-selection__clear { margin-right: 5px; padding: 0 3px; }' );
+ wp_register_script( 'wc-gzd-shipments-modal', Package::get_assets_url( 'static/modal.js' ), array( 'jquery' ), Package::get_version() ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter
+ wp_register_script( 'wc-gzd-shipments-pickup-locations', Package::get_assets_url( 'static/pickup-locations.js' ), array( 'jquery', 'woocommerce', 'selectWoo', 'wc-gzd-shipments-modal' ), Package::get_version() ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter
+
+ // Register admin styles.
+ wp_register_style( 'woocommerce_gzd_shipments_pickup_locations', Package::get_assets_url( 'static/pickup-locations-styles.css' ), array(), Package::get_version() );
+ wp_enqueue_style( 'woocommerce_gzd_shipments_pickup_locations' );
wp_localize_script(
'wc-gzd-shipments-pickup-locations',
'wc_gzd_shipments_pickup_locations_params',
- array()
+ array(
+ 'wc_ajax_url' => \WC_AJAX::get_endpoint( '%%endpoint%%' ),
+ 'i18n_managed_by_pickup_location' => _x( 'Managed by pickup location', 'shipments', 'woocommerce-germanized-shipments' ),
+ 'i18n_pickup_location_delivery_unavailable' => _x( 'Pickup location delivery is not available any longer. Please review your shipping address.', 'shipments', 'woocommerce-germanized-shipments' ),
+ )
);
wp_enqueue_script( 'wc-gzd-shipments-pickup-locations' );
@@ -407,7 +788,7 @@ public static function is_enabled() {
public static function is_available() {
$available = self::is_enabled();
- if ( wc()->cart && ! wc()->cart->needs_shipping() ) {
+ if ( is_checkout() && ( wc()->cart && ! wc()->cart->needs_shipping() ) ) {
$available = false;
}
@@ -423,58 +804,39 @@ public static function register_classic_checkout_fields( $fields ) {
return $fields;
}
- $locations = array();
- $current_location_code = '';
- $current_customer_number = '';
- $current_location = null;
- $customer_number_field_label = _x( 'Customer Number', 'shipments', 'woocommerce-germanized-shipments' );
+ $pickup_delivery_data = self::get_pickup_location_data( 'checkout' );
- if ( $method = wc_gzd_get_current_shipping_provider_method() ) {
- if ( $provider = $method->get_shipping_provider_instance() ) {
- if ( is_a( $provider, 'Vendidero\Germanized\Shipments\Interfaces\ShippingProviderAuto' ) ) {
- $address = array(
- 'country' => wc()->customer->get_shipping_country(),
- 'postcode' => wc()->customer->get_shipping_postcode(),
- 'address_1' => wc()->customer->get_shipping_address_1(),
- );
-
- $query_args = self::get_pickup_delivery_cart_args();
-
- if ( $provider->supports_pickup_location_delivery( $address, $query_args ) ) {
- $locations = $provider->get_pickup_locations( $address, $query_args );
- $current_location_code = self::get_pickup_location_code_by_user();
-
- if ( $current_location_code ) {
- $current_location = $provider->get_pickup_location_by_code( $current_location_code, $address );
-
- if ( ! $current_location ) {
- $current_location_code = '';
- } else {
- $locations[] = $current_location;
-
- if ( $current_location->supports_customer_number() ) {
- $current_customer_number = self::get_pickup_location_customer_number_by_customer();
- $customer_number_field_label = $current_location->get_customer_number_field_label();
- }
- }
- }
- }
- }
- }
- }
+ $fields['billing']['billing_pickup_location_notice'] = array(
+ 'type' => 'wc_gzd_shipments_pickup_location_notice',
+ 'hidden' => $pickup_delivery_data['supports_pickup_delivery'] && ! $pickup_delivery_data['current_location'] ? false : true,
+ 'current_location' => $pickup_delivery_data['current_location'],
+ 'label' => '',
+ 'priority' => 61,
+ 'required' => false,
+ );
- $fields['order']['pickup_location'] = array(
- 'type' => 'wc_gzd_shipments_pickup_location',
- 'locations' => $locations,
- 'default' => $current_location_code,
- 'label' => _x( 'Pickup location', 'shipments', 'woocommerce-germanized-shipments' ),
+ $fields['shipping']['shipping_pickup_location_notice'] = array(
+ 'type' => 'wc_gzd_shipments_pickup_location_notice',
+ 'hidden' => $pickup_delivery_data['supports_pickup_delivery'] ? false : true,
+ 'current_location' => $pickup_delivery_data['current_location'],
+ 'label' => '',
+ 'priority' => 61,
+ 'required' => false,
);
- $fields['order']['pickup_location_customer_number'] = array(
+ $fields['shipping']['pickup_location_customer_number'] = array(
'type' => 'wc_gzd_shipments_pickup_location_customer_number',
- 'label' => $customer_number_field_label,
- 'current_location' => $current_location,
- 'default' => $current_customer_number,
+ 'label' => $pickup_delivery_data['customer_number_field_label'],
+ 'current_location' => $pickup_delivery_data['current_location'],
+ 'default' => $pickup_delivery_data['current_location_customer_number'],
+ 'priority' => 62,
+ );
+
+ $fields['order']['current_pickup_location'] = array(
+ 'type' => 'wc_gzd_shipments_current_pickup_location',
+ 'current_location' => $pickup_delivery_data['current_location'],
+ 'default' => $pickup_delivery_data['current_location_code'],
+ 'label' => '',
);
return $fields;
@@ -499,10 +861,12 @@ public static function set_formatted_shipping_address( $fields, $order ) {
return $fields;
}
- public static function set_formatted_user_shipping_address( $address, $customer_id, $name ) {
+ public static function set_formatted_customer_shipping_address( $address, $customer_id, $name ) {
if ( 'shipping' === $name ) {
- if ( $customer_number = self::get_pickup_location_customer_number_by_customer( $customer_id ) ) {
- $address['pickup_location_customer_number'] = $customer_number;
+ if ( self::is_pickup_location_delivery_available_for_customer( $customer_id ) ) {
+ if ( $customer_number = self::get_pickup_location_customer_number_by_customer( $customer_id ) ) {
+ $address['pickup_location_customer_number'] = $customer_number;
+ }
}
}
@@ -531,6 +895,8 @@ protected static function get_customer( $customer_id = false ) {
$customer = new \WC_Customer( $customer_id );
} elseif ( is_a( $customer_id, 'WC_Customer' ) ) {
$customer = $customer_id;
+ } elseif ( is_user_logged_in() ) {
+ $customer = new \WC_Customer( get_current_user_id() );
} elseif ( wc()->customer ) {
$customer = wc()->customer;
}
@@ -538,7 +904,26 @@ protected static function get_customer( $customer_id = false ) {
return $customer;
}
- public static function get_pickup_location_code_by_user( $customer_id = false ) {
+ public static function is_pickup_location_delivery_available_for_customer( $customer_id ) {
+ $supports_pickup_delivery = false;
+
+ if ( $customer = self::get_customer( $customer_id ) ) {
+ $address = $customer->get_shipping();
+ $shipping_provider = wc_gzd_get_customer_preferred_shipping_provider( $customer->get_id() );
+
+ if ( $shipping_provider ) {
+ if ( is_a( $shipping_provider, 'Vendidero\Germanized\Shipments\ShippingProvider\Auto' ) ) {
+ if ( $shipping_provider->supports_pickup_location_delivery( $address ) ) {
+ $supports_pickup_delivery = true;
+ }
+ }
+ }
+ }
+
+ return apply_filters( 'woocommerce_gzd_shipment_pickup_location_delivery_available_for_customer', $supports_pickup_delivery, $customer_id );
+ }
+
+ public static function get_pickup_location_code_by_customer( $customer_id = false ) {
$customer = self::get_customer( $customer_id );
$pickup_code = '';
@@ -553,6 +938,10 @@ public static function get_pickup_location_code_by_user( $customer_id = false )
return apply_filters( 'woocommerce_gzd_shipment_customer_pickup_location_code', $pickup_code, $customer );
}
+ public static function get_pickup_location_code_by_user( $customer_id = false ) {
+ return self::get_pickup_location_code_by_customer( $customer_id );
+ }
+
public static function formatted_shipping_replacements( $fields, $args ) {
if ( isset( $args['pickup_location_customer_number'] ) && ! empty( $args['pickup_location_customer_number'] ) ) {
$fields['{name}'] = $fields['{name}'] . "\n" . $args['pickup_location_customer_number'];