diff --git a/InventoryInStorePickupFrontend/view/frontend/web/js/model/pickup-locations-service.js b/InventoryInStorePickupFrontend/view/frontend/web/js/model/pickup-locations-service.js index f34c3e8b9444..03e464b9b322 100644 --- a/InventoryInStorePickupFrontend/view/frontend/web/js/model/pickup-locations-service.js +++ b/InventoryInStorePickupFrontend/view/frontend/web/js/model/pickup-locations-service.js @@ -74,6 +74,46 @@ define([ }.bind(this)); }, + /** + * Sole owner of the nearby search criteria shape. Requests issued from different components + * must be structurally identical, otherwise they miss each other in locationsCache. + * + * @param {String} searchTerm + * @param {Number} radius + * @param {Number} pageSize + * @returns {Object} + */ + getNearbyLocationsCriteria: function (searchTerm, radius, pageSize) { + var criteria, + // Composite items are skipped - their children carry the salable skus. + productsInfo = _.chain(quote.getItems()) + .filter(function (item) { + return item['qty_options'] === undefined || item['qty_options'].length === 0; + }) + .map(function (item) { + return { + sku: item.sku + }; + }) + .value(); + + criteria = { + extensionAttributes: { + productsInfo: productsInfo + }, + pageSize: pageSize + }; + + if (searchTerm) { + criteria.area = { + radius: radius, + searchTerm: searchTerm + }; + } + + return criteria; + }, + /** * Get nearby pickup locations based on given search criteria. * diff --git a/InventoryInStorePickupFrontend/view/frontend/web/js/view/store-pickup.js b/InventoryInStorePickupFrontend/view/frontend/web/js/view/store-pickup.js index 3c2ff876fd43..a5e23833dc7f 100644 --- a/InventoryInStorePickupFrontend/view/frontend/web/js/view/store-pickup.js +++ b/InventoryInStorePickupFrontend/view/frontend/web/js/view/store-pickup.js @@ -232,9 +232,7 @@ define([ selectedSource, selectedSourceCode, nearestLocation, - self = this, - productsInfo = [], - items = quote.getItems(); + self = this; if (!this.isStorePickupSelected()) { return; @@ -279,27 +277,15 @@ define([ pickupLocationsService.selectForShipping(location); }); } else if (shippingAddress.city && shippingAddress.postcode) { - _.each(items, function (item) { - if (item['qty_options'] === undefined || item['qty_options'].length === 0) { - productsInfo.push( - { - sku: item.sku - } - ); - } - }); pickupLocationsService - .getNearbyLocations({ - area: { - radius: this.nearbySearchRadius, - searchTerm: shippingAddress.postcode + this.delimiter + - shippingAddress.countryId || this.defaultCountry - }, - extensionAttributes: { - productsInfo: productsInfo - }, - pageSize: this.nearbySearchLimit - }) + .getNearbyLocations( + pickupLocationsService.getNearbyLocationsCriteria( + shippingAddress.postcode + this.delimiter + + (shippingAddress.countryId || this.defaultCountry), + this.nearbySearchRadius, + this.nearbySearchLimit + ) + ) .then(function (locations) { nearestLocation = locations[0]; diff --git a/InventoryInStorePickupFrontend/view/frontend/web/js/view/store-selector.js b/InventoryInStorePickupFrontend/view/frontend/web/js/view/store-selector.js index 2f737ac5f4db..729ee4567f6a 100644 --- a/InventoryInStorePickupFrontend/view/frontend/web/js/view/store-selector.js +++ b/InventoryInStorePickupFrontend/view/frontend/web/js/view/store-selector.js @@ -126,7 +126,8 @@ define([ * @returns void */ openPopup: function () { - var shippingAddress = quote.shippingAddress(), + var self = this, + shippingAddress = quote.shippingAddress(), country = shippingAddress.countryId ? shippingAddress.countryId : this.defaultCountryId, searchTerm = ''; @@ -137,7 +138,11 @@ define([ searchTerm = this.getSearchTerm(shippingAddress.postcode, country); } - this.updateNearbyLocations(searchTerm); + this.updateNearbyLocations(searchTerm).done(function () { + if (searchTerm && !self.searchQuery() && !self.nearbyLocations().length) { + self.updateNearbyLocations(''); + } + }); }, /** @@ -162,37 +167,16 @@ define([ * @returns {*} */ updateNearbyLocations: function (searchQuery) { - var self = this, - productsInfo = [], - items = quote.getItems(), - searchCriteria; - - _.each(items, function (item) { - if (item['qty_options'] === undefined || item['qty_options'].length === 0) { - productsInfo.push( - { - sku: item.sku - } - ); - } - }); - - searchCriteria = { - extensionAttributes: { - productsInfo: productsInfo - }, - pageSize: this.nearbySearchLimit - }; - - if (searchQuery) { - searchCriteria.area = { - radius: this.nearbySearchRadius, - searchTerm: searchQuery - }; - } + const self = this; return pickupLocationsService - .getNearbyLocations(searchCriteria) + .getNearbyLocations( + pickupLocationsService.getNearbyLocationsCriteria( + searchQuery, + this.nearbySearchRadius, + this.nearbySearchLimit + ) + ) .then(function (locations) { self.nearbyLocations(locations); })