Skip to content

Commit

Permalink
Check for product existence before applying bundle compatibility. Onl…
Browse files Browse the repository at this point in the history
…y trigger pickup location select change event in case available locations really changed to prevent compatibility issues.
  • Loading branch information
dennisnissle committed Jul 9, 2024
1 parent 571c668 commit 356059a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
28 changes: 19 additions & 9 deletions assets/js/static/pickup-locations.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ window.germanized.shipments_pickup_locations = window.germanized.shipments_picku
$form = $( this ),
params = $form.serialize(),
$pickupSelect = self.getPickupLocationSelect(),
current = $pickupSelect.val();
current = $pickupSelect.val(),
oldLocations = self.pickupLocations;

$( '#wc-gzd-shipments-pickup-location-search-form' ).block({
message: null,
Expand All @@ -161,7 +162,7 @@ window.germanized.shipments_pickup_locations = window.germanized.shipments_picku
success: function( data ) {
if ( data.success ) {
self.pickupLocations = data.locations;
self.updatePickupLocationSelect();
self.updatePickupLocationSelect( oldLocations );

$( '#wc-gzd-shipments-pickup-location-search-form' ).unblock();
}
Expand All @@ -173,7 +174,7 @@ window.germanized.shipments_pickup_locations = window.germanized.shipments_picku
return false;
},

updatePickupLocationSelect: function() {
updatePickupLocationSelect: function( oldLocations = [] ) {
var self = germanized.shipments_pickup_locations,
$pickupSelect = self.getPickupLocationSelect(),
current = $pickupSelect.val();
Expand All @@ -189,13 +190,21 @@ window.germanized.shipments_pickup_locations = window.germanized.shipments_picku
$pickupSelect.append( $( "<option></option>" ).attr("value", code ).text( label ) );
});

var currentLocation = self.getPickupLocation( current );
var currentLocation = self.getPickupLocation( current, false );

if ( currentLocation ) {
$pickupSelect.find( 'option[value="' + currentLocation.code + '"' )[0].selected = true;
}

$pickupSelect.trigger( 'change' );
/**
* Do only trigger select change if available pickup locations really changed
* to prevent possible incompatibilities with other extensions.
*/
try {
if ( JSON.stringify( self.pickupLocations ) !== JSON.stringify( oldLocations ) ) {
$pickupSelect.trigger( 'change' );
}
} catch (e) {}
},

onSelectDifferentShipping: function() {
Expand Down Expand Up @@ -282,12 +291,12 @@ window.germanized.shipments_pickup_locations = window.germanized.shipments_picku
return $( '#pickup_location' );
},

getPickupLocation: function( locationCode ) {
getPickupLocation: function( locationCode, allowFallback = true ) {
var self = germanized.shipments_pickup_locations;

if ( self.pickupLocations.hasOwnProperty( locationCode ) ) {
return self.pickupLocations[ locationCode ];
} else {
} else if ( allowFallback ) {
var $select = $( '#current_pickup_location' );

if ( $select.attr( 'data-current-location' ) ) {
Expand All @@ -306,7 +315,8 @@ window.germanized.shipments_pickup_locations = window.germanized.shipments_picku

afterRefreshCheckout: function( e, ajaxData ) {
var self = germanized.shipments_pickup_locations,
supportsPickupLocationDelivery = false;
supportsPickupLocationDelivery = false,
oldLocations = self.pickupLocations;

ajaxData = ( typeof ajaxData === 'undefined' ) ? {
'fragments': {
Expand All @@ -322,7 +332,7 @@ window.germanized.shipments_pickup_locations = window.germanized.shipments_picku
if ( ajaxData.fragments.hasOwnProperty( '.gzd-shipments-pickup-locations' ) && Object.keys( self.pickupLocations ).length <= 0 ) {
self.pickupLocations = JSON.parse( ajaxData.fragments['.gzd-shipments-pickup-locations'] );

self.updatePickupLocationSelect();
self.updatePickupLocationSelect( oldLocations );
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Compatibility/Bundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ public static function apply_bundle_hierarchy( $shipment ) {
}

/**
* @param Product $product
* @param Product|null $product
* @param \WC_Order_Item_Product $item
*
* @return Product
*/
public static function get_product_from_item( $product, $item ) {
if ( ! $order = $item->get_order() ) {
if ( ! $order = $item->get_order() || ! $product ) {
return $product;
}

Expand Down

0 comments on commit 356059a

Please sign in to comment.