Skip to content

Commit

Permalink
Pickup location search context awareness. Force hiding pickup locatio…
Browse files Browse the repository at this point in the history
…n fields in case not available to prevent flashes.
  • Loading branch information
dennisnissle committed May 23, 2024
1 parent 2deb2ba commit 4c49d9e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 34 deletions.
6 changes: 5 additions & 1 deletion assets/css/pickup-locations.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.pickup_location_notice.hidden, #pickup_location_field.hidden, #pickup_location_customer_number_field.hidden {
#pickup_location_field.hidden {
display: none;
}

Expand All @@ -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;
}
Expand Down
22 changes: 15 additions & 7 deletions assets/js/static/pickup-locations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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();
}
},

Expand Down Expand Up @@ -343,23 +345,29 @@ 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() {
var self = germanized.shipments_pickup_locations;

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' ),
Expand Down
44 changes: 18 additions & 26 deletions src/PickupDelivery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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' ), '<a href="#" class="pickup-location-notice-link wc-gzd-modal-launcher" data-modal-id="pickup-location">' . _x( 'pickup location', 'shipments', 'woocommerce-germanized-shipments' ) . '</a>' ),
'i18n_pickup_location_delivery_unavailable' => _x( 'Pickup location delivery is not available any longer. Please review your shipping address.', 'shipments', 'woocommerce-germanized-shipments' ),
)
Expand Down

0 comments on commit 4c49d9e

Please sign in to comment.