diff --git a/assets/css/pickup-locations.scss b/assets/css/pickup-locations.scss index 3ff39287..853a6f8f 100644 --- a/assets/css/pickup-locations.scss +++ b/assets/css/pickup-locations.scss @@ -1,4 +1,4 @@ -.pickup_location_notice.hidden, #pickup_location_field.hidden, #pickup_location_customer_number_field.hidden { +#pickup_location_field.hidden { display: none; } @@ -8,6 +8,10 @@ .woocommerce-checkout, .woocommerce-account { .woocommerce form { + .pickup_location_notice.hidden, #pickup_location_customer_number_field.hidden { + display: none !important; + } + #current_pickup_location_field { display: none; } diff --git a/assets/js/static/pickup-locations.js b/assets/js/static/pickup-locations.js index 1e5f3dc5..c8fce42f 100644 --- a/assets/js/static/pickup-locations.js +++ b/assets/js/static/pickup-locations.js @@ -148,7 +148,7 @@ window.germanized.shipments_pickup_locations = window.germanized.shipments_picku } }); - params['action'] = 'woocommerce_gzd_shipments_search_pickup_locations'; + params += '&action=woocommerce_gzd_shipments_search_pickup_locations&context=' + self.params.context; $.ajax({ type: "POST", @@ -197,9 +197,11 @@ window.germanized.shipments_pickup_locations = window.germanized.shipments_picku if ( ! $( this ).is( ':checked' ) ) { self.disablePickupLocationDelivery(); - $( '#billing_pickup_location_notice' ).show(); + if ( self.isAvailable() ) { + $( '#billing_pickup_location_notice' ).removeClass( 'hidden' ).show(); + } } else { - $( '#billing_pickup_location_notice' ).hide(); + $( '#billing_pickup_location_notice' ).addClass( 'hidden' ).hide(); } }, @@ -343,7 +345,7 @@ window.germanized.shipments_pickup_locations = window.germanized.shipments_picku $.scroll_to_notices( scrollElement ); } - $( '.pickup_location_notice' ).hide(); + $( '.pickup_location_notice' ).addClass( 'hidden' ).hide(); }, enable: function() { @@ -351,15 +353,21 @@ window.germanized.shipments_pickup_locations = window.germanized.shipments_picku self.available = true; - $( '.pickup_location_notice' ).show(); + $( '.pickup_location_notice' ).removeClass( 'hidden' ).show(); if ( $( '#ship-to-different-address-checkbox' ).is( ':checked' ) || self.hasPickupLocationDelivery() ) { - $( '#billing_pickup_location_notice' ).hide(); + $( '#billing_pickup_location_notice' ).addClass( 'hidden' ).hide(); } else { - $( '#billing_pickup_location_notice' ).show(); + $( '#billing_pickup_location_notice' ).removeClass( 'hidden' ).show(); } }, + isAvailable: function() { + var self= germanized.shipments_pickup_locations; + + return self.available; + }, + replaceShippingAddress: function( replacements ) { var self = germanized.shipments_pickup_locations, $shipToDifferent = $( '#ship-to-different-address input' ), diff --git a/src/PickupDelivery.php b/src/PickupDelivery.php index fe1f65cc..c3b5874b 100644 --- a/src/PickupDelivery.php +++ b/src/PickupDelivery.php @@ -158,7 +158,7 @@ public static function register_customer_address_fields( $address_fields, $load_ return $address_fields; } - protected static function get_pickup_location_data( $context = 'checkout', $retrieve_locations = false ) { + protected static function get_pickup_location_data( $context = 'checkout', $retrieve_locations = false, $address_args = array() ) { $customer = wc()->customer; $query_args = array(); $provider = false; @@ -195,6 +195,8 @@ protected static function get_pickup_location_data( $context = 'checkout', $retr ); } + $result['address'] = array_replace_recursive( $result['address'], $address_args ); + if ( is_a( $provider, 'Vendidero\Germanized\Shipments\Interfaces\ShippingProviderAuto' ) ) { if ( $provider->supports_pickup_location_delivery( $result['address'], $query_args ) ) { $result['supports_pickup_delivery'] = true; @@ -635,39 +637,28 @@ public static function search_pickup_locations() { $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'] ) ) : ''; + $context = isset( $_POST['context'] ) ? wc_clean( wp_unslash( $_POST['context'] ) ) : 'checkout'; + $context = in_array( $context, array( 'customer', 'checkout' ), true ) ? $context : 'checkout'; 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() ? wc()->customer->get_shipping_country() : wc()->customer->get_billing_country(), - 'postcode' => $postcode, - 'address_1' => $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 ); - } - } - } - } + $pickup_data = self::get_pickup_location_data( + $context, + true, + array( + 'postcode' => $postcode, + 'address_1' => $address_1, + ) + ); - if ( ! empty( $locations ) ) { - $new_locations = array(); + $locations = array(); - foreach ( $locations as $location ) { - $new_locations[ $location->get_code() ] = $location->get_data(); + if ( ! empty( $pickup_data['locations'] ) ) { + foreach ( $pickup_data['locations'] as $location ) { + $locations[ $location->get_code() ] = $location->get_data(); } - - $locations = $new_locations; } wp_send_json( @@ -701,6 +692,7 @@ public static function register_assets() { 'wc_gzd_shipments_pickup_locations_params', array( 'wc_ajax_url' => \WC_AJAX::get_endpoint( '%%endpoint%%' ), + 'context' => self::is_edit_address_page() ? 'customer' : 'checkout', 'i18n_managed_by_pickup_location' => sprintf( _x( 'Managed by %1$s', 'shipments', 'woocommerce-germanized-shipments' ), '' . _x( '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' ), )