= $form->getShipmentOptionsHumanMap()[$shipmentOption] ?? $shipmentOption ?>
+ getShipmentOptionsExplanationMap()[$shipmentOption] ?? '')) {
+ echo '';
+ } ?>
diff --git a/view/adminhtml/web/js/delivery-costs-matrix.js b/view/adminhtml/web/js/delivery-costs-matrix.js
new file mode 100644
index 00000000..cbc853c3
--- /dev/null
+++ b/view/adminhtml/web/js/delivery-costs-matrix.js
@@ -0,0 +1,12 @@
+define(
+ function() {
+ 'use strict';
+
+ return function Matrix(options) {
+ console.warn('Matrix loaded', options);
+ console.log(JSON.parse(options.carriers));
+ console.log(JSON.parse(options.packageTypes));
+ console.log(JSON.parse(options.countryParts));
+ };
+ }
+);
diff --git a/view/frontend/web/js/model/checkout.js b/view/frontend/web/js/model/checkout.js
index 0921fc2f..a4bc8807 100644
--- a/view/frontend/web/js/model/checkout.js
+++ b/view/frontend/web/js/model/checkout.js
@@ -44,7 +44,7 @@ function(
) {
'use strict';
- var Model = {
+ const Model = {
configuration: ko.observable(null),
/**
@@ -71,24 +71,30 @@ function(
* Best package type.
*/
bestPackageType: null,
+ carrierCode: 'myparcel', // default, may be overridden by carrierCode in configuration
/**
* Initialize by requesting the MyParcel settings configuration from Magento.
*/
initialize: function() {
+ console.error(' Joeri debugging 2 ');
Model.compute = ko.computed(function() {
- var configuration = Model.configuration();
- var rates = Model.rates();
+ const configuration = Model.configuration();
+ const rates = Model.rates();
if (!configuration || !rates.length) {
return false;
}
+ // necessary vor updateAllowedShippingMethods()
+ if (configuration.carrierCode) Model.carrierCode = configuration.carrierCode;
+
+ // the object with information that we need
return {configuration: configuration, rates: rates};
});
- Model.compute.subscribe(function(a) {
- if (!a) {
+ Model.compute.subscribe(function(objectWithInformation) {
+ if (!objectWithInformation) {
return;
}
@@ -102,7 +108,7 @@ function(
doRequest(Model.getDeliveryOptionsConfig, {onSuccess: Model.onInitializeSuccess});
function reloadConfig() {
- var shippingAddress = quote.shippingAddress();
+ const shippingAddress = quote.shippingAddress();
if (shippingAddress.countryId !== Model.countryId()) {
doRequest(Model.getDeliveryOptionsConfig, {onSuccess: Model.onReFetchDeliveryOptionsConfig});
@@ -115,7 +121,7 @@ function(
},
onReFetchDeliveryOptionsConfig: function(response) {
- var configuration = response[0].data;
+ const configuration = response[0].data;
Model.bestPackageType = configuration.config.packageType;
Model.setDeliveryOptionsConfig(configuration);
},
@@ -130,19 +136,6 @@ function(
Model.hideShippingMethods();
},
- /**
- * Search the rates for the given method code.
- *
- * @param {string} methodCode - Method code to search for.
- *
- * @returns {Object} - The found rate, if any.
- */
- findRateByMethodCode: function(methodCode) {
- return Model.rates().find(function(rate) {
- return rate.method_code === methodCode;
- });
- },
-
/**
* Search the rates for the given carrier code.
*
@@ -152,9 +145,7 @@ function(
*/
findOriginalRateByCarrierCode: function(carrierCode) {
return Model.rates().find(function(rate) {
- if (-1 === rate.method_code.indexOf('myparcel')) {
- return rate.carrier_code === carrierCode;
- }
+ return rate.carrier_code === carrierCode;
});
},
@@ -162,32 +153,22 @@ function(
* Hide the shipping methods the delivery options should replace.
*/
hideShippingMethods: function() {
- var rowsToHide = [];
+ const row = Model.rowElement();
- Model.rates().forEach(function(rate) {
- const hasDeliveryOptions = Model.hasDeliveryOptions();
- const myParcelMethods = hasDeliveryOptions ? Model.configuration().methods || [] : [];
- const cell = document.getElementById('label_method_' + rate.method_code + '_' + rate.carrier_code) || null;
-
- if (!rate.available || !cell) {
- return;
- }
+ if (row && Model.hasDeliveryOptions) {
+ row.style.display = 'none';
+ }
+ },
- const row = cell.parentElement;
+ rowElement: function() {
+ const rate = Model.findOriginalRateByCarrierCode(Model.carrierCode) || {},
+ cell = document.getElementById(`label_method_${rate.method_code}_${rate.carrier_code}`);
- /**
- * Hide MyParcel-specific methods, and the parent methods delivery options are bound to
- */
- if (rate.method_code.indexOf('myparcel') !== -1) {
- rowsToHide.push(row);
- } else if (myParcelMethods.includes(rate.carrier_code)) {
- rowsToHide.push(row);
- }
- });
+ if (!cell) {
+ return null;
+ }
- rowsToHide.forEach(function(row) {
- row.style.display = 'none';
- });
+ return cell.parentElement; // or cell.closest('tr');? who knows how this is structured in different checkouts?
},
/**
@@ -211,11 +192,11 @@ function(
* @returns {XMLHttpRequest}
*/
calculatePackageType: function(carrier) {
- var list = document.querySelector('[name="country_id"]');
function isVisible(el) {
return !!(el.offsetWidth || el.offsetHeight || el.getClientRects().length);
}
- var countryId = (list && isVisible(list)) ? list.options[list.selectedIndex].value : Model.countryId();
+ const list = document.querySelector('[name="country_id"]'),
+ countryId = (list && isVisible(list)) ? list.options[list.selectedIndex].value : Model.countryId();
return sendRequest(
'rest/V1/package_type',
'GET',
@@ -275,14 +256,13 @@ function(
* Filter the allowed shipping methods by checking if they are actually present in the checkout. If not they will
* be left out.
*/
- Model.allowedShippingMethods(Model.configuration().methods.filter(function(carrierCode) {
- return !!Model.findOriginalRateByCarrierCode(carrierCode);
- }));
+ const row = Model.rowElement();
+ if (!row) setTimeout(updateAllowedShippingMethods, 151);
+ Model.allowedShippingMethods([Model.carrierCode]);
}
function updateHasDeliveryOptions() {
let isAllowed = false;
- const shippingCountry = quote.shippingAddress().countryId;
Model.allowedShippingMethods().forEach(function(carrierCode) {
const rate = Model.findOriginalRateByCarrierCode(carrierCode);
@@ -290,7 +270,6 @@ function(
isAllowed = true;
}
});
-
Model.hasDeliveryOptions(isAllowed);
Model.hideShippingMethods();
}
@@ -319,7 +298,7 @@ function(
};
request().onload = function() {
- var response = JSON.parse(this.response);
+ const response = JSON.parse(this.response);
if (this.status >= STATUS_SUCCESS && this.status < STATUS_ERROR) {
handlers.doHandler('onSuccess', response);
@@ -341,15 +320,15 @@ function(
* @returns {XMLHttpRequest}
*/
function sendRequest(endpoint, method, options) {
- var url = mageUrl.build(endpoint);
- var request = new XMLHttpRequest();
- var query = [];
+ let url = mageUrl.build(endpoint);
+ const query = [],
+ request = new XMLHttpRequest();
method = method || 'GET';
options = options || {};
if (method === 'GET') {
- for (var key in options) {
+ for (const key in options) {
query.push(key + '=' + encodeURIComponent(options[key]));
}
}
diff --git a/view/frontend/web/js/model/shipping-save-processor-default.js b/view/frontend/web/js/model/shipping-save-processor-default.js
old mode 100755
new mode 100644
diff --git a/view/frontend/web/js/view/delivery-options.js b/view/frontend/web/js/view/delivery-options.js
index 82366ffd..efbc7397 100644
--- a/view/frontend/web/js/view/delivery-options.js
+++ b/view/frontend/web/js/view/delivery-options.js
@@ -27,9 +27,7 @@ define(
) {
'use strict';
- var deliveryOptions;
-
- deliveryOptions = {
+ const deliveryOptions = {
rendered: ko.observable(false),
splitStreetRegex: /(.*?)\s?(\d{1,5})[/\s-]{0,2}([A-z]\d{1,3}|-\d{1,4}|\d{2}\w{1,2}|[A-z][A-z\s]{0,3})?$/,
@@ -57,48 +55,6 @@ define(
*/
hiddenDataInput: '[name="myparcel_delivery_options"]',
- methodCodeStandardDelivery: 'myparcelnl_magento_postnl_settings/delivery',
-
- /**
- * Maps shipping method codes to prices in the delivery options config.
- */
- methodCodeDeliveryOptionsConfigMap: {
- 'myparcelnl_magento_postnl_settings/delivery': 'config.carrierSettings.postnl.priceStandardDelivery',
- 'myparcelnl_magento_postnl_settings/mailbox': 'config.carrierSettings.postnl.pricePackageTypeMailbox',
- 'myparcelnl_magento_postnl_settings/package_small': 'config.carrierSettings.postnl.pricePackageTypePackageSmall',
- 'myparcelnl_magento_postnl_settings/digital_stamp': 'config.carrierSettings.postnl.pricePackageTypeDigitalStamp',
- 'myparcelnl_magento_postnl_settings/morning': 'config.carrierSettings.postnl.priceMorningDelivery',
- 'myparcelnl_magento_postnl_settings/evening': 'config.carrierSettings.postnl.priceEveningDelivery',
- 'myparcelnl_magento_postnl_settings/morning/only_recipient': 'config.carrierSettings.postnl.priceMorningDelivery',
- 'myparcelnl_magento_postnl_settings/evening/only_recipient': 'config.carrierSettings.postnl.priceEveningDelivery',
- 'myparcelnl_magento_postnl_settings/pickup': 'config.carrierSettings.postnl.pricePickup',
- 'myparcelnl_magento_postnl_settings/morning/only_recipient/signature': 'config.carrierSettings.postnl.priceMorningSignature',
- 'myparcelnl_magento_postnl_settings/evening/only_recipient/signature': 'config.carrierSettings.postnl.priceEveningSignature',
- 'myparcelnl_magento_postnl_settings/delivery/only_recipient/signature': 'config.carrierSettings.postnl.priceSignatureAndOnlyRecipient',
- 'myparcelnl_magento_dhlforyou_settings/delivery': 'config.carrierSettings.dhlforyou.priceStandardDelivery',
- 'myparcelnl_magento_dhlforyou_settings/mailbox': 'config.carrierSettings.dhlforyou.pricePackageTypeMailbox',
- 'myparcelnl_magento_dhlforyou_settings/pickup': 'config.carrierSettings.dhlforyou.pricePickup',
- 'myparcelnl_magento_dhlforyou_settings/delivery/same_day_delivery': 'config.carrierSettings.dhlforyou.priceSameDayDelivery',
- 'myparcelnl_magento_dhlforyou_settings/delivery/only_recipient/same_day_delivery': 'config.carrierSettings.dhlforyou.priceSameDayDeliveryAndOnlyRecipient',
- 'myparcelnl_magento_dhleuroplus_settings/delivery': 'config.carrierSettings.dhleuroplus.priceStandardDelivery',
- 'myparcelnl_magento_dhlparcelconnect_settings/delivery': 'config.carrierSettings.dhlparcelconnect.priceStandardDelivery',
- 'myparcelnl_magento_ups_settings/delivery': 'config.carrierSettings.ups.priceStandardDelivery',
- 'myparcelnl_magento_dpd_settings/delivery': 'config.carrierSettings.dpd.priceStandardDelivery',
- 'myparcelnl_magento_dpd_settings/pickup': 'config.carrierSettings.dpd.pricePickup',
- 'myparcelnl_magento_dpd_settings/mailbox': 'config.carrierSettings.dpd.pricePackageTypeMailbox',
- },
-
- /**
- * Maps shipping method codes to prices in the delivery options config.
- */
- methodCodeShipmentOptionsConfigMap: {
- 'myparcelnl_magento_postnl_settings/delivery/signature': 'config.carrierSettings.postnl.priceSignature',
- 'myparcelnl_magento_postnl_settings/delivery/only_recipient': 'config.carrierSettings.postnl.priceOnlyRecipient',
- 'myparcelnl_magento_dhlforyou_settings/delivery/only_recipient': 'config.carrierSettings.dhlforyou.priceOnlyRecipient',
- 'myparcelnl_magento_dhlforyou_settings/delivery/same_day_delivery': 'config.carrierSettings.dhlforyou.priceSameDayDelivery',
- 'myparcelnl_magento_dhlforyou_settings/delivery/only_recipient/same_day_delivery': 'config.carrierSettings.dhlforyou.priceSameDayDeliveryAndOnlyRecipient',
- },
-
/**
* Initialize the script. Render the delivery options div, request the plugin settings, then initialize listeners.
*/
@@ -115,14 +71,14 @@ define(
},
setToRenderWhenVisible: function() {
- var shippingMethodDiv = document.getElementById('checkout-shipping-method-load');
+ const shippingMethodDiv = document.getElementById('checkout-shipping-method-load');
/**
- * Sometimes the shipping method div doesn't exist yet. Retry in 100ms if it happens.
+ * Sometimes the shipping method div doesn't exist yet. Retry in 151ms if it happens.
*/
if (!shippingMethodDiv) {
setTimeout(function() {
deliveryOptions.setToRenderWhenVisible();
- }, 100);
+ }, 151);
return;
}
@@ -155,24 +111,21 @@ define(
deliveryOptions.triggerEvent(deliveryOptions.hideDeliveryOptionsEvent);
},
- /**
- * Create the div the delivery options will be rendered in, if it doesn't exist yet.
- */
render: function() {
- var hasUnrenderedDiv = document.querySelector('#myparcel-delivery-options');
- var hasRenderedDeliveryOptions = document.querySelector('.myparcel-delivery-options__table');
- var shippingMethodDiv = document.getElementById('checkout-shipping-method-load');
- var deliveryOptionsDiv = document.createElement('div');
-
+ const deliveryOptionsDiv = document.getElementById('myparcel-delivery-options'),
+ shippingMethodDiv = document.getElementById('checkout-shipping-method-load');
checkout.hideShippingMethods();
deliveryOptions.rendered(false);
- if (hasUnrenderedDiv || hasRenderedDeliveryOptions) {
+ if (deliveryOptionsDiv) {
deliveryOptions.triggerEvent(deliveryOptions.updateDeliveryOptionsEvent);
- } else if (!hasUnrenderedDiv) {
- deliveryOptionsDiv.setAttribute('id', 'myparcel-delivery-options');
- shippingMethodDiv.insertBefore(deliveryOptionsDiv, shippingMethodDiv.firstChild);
- deliveryOptions.triggerEvent(deliveryOptions.renderDeliveryOptionsEvent);
+ } else {
+ const newDeliveryOptionsDiv = document.createElement('div');
+ newDeliveryOptionsDiv.setAttribute('id', 'myparcel-delivery-options');
+ shippingMethodDiv.insertAdjacentElement('afterbegin', newDeliveryOptionsDiv);
+ requestAnimationFrame(function() { // wait for the element to actually be added to the DOM
+ deliveryOptions.triggerEvent(deliveryOptions.renderDeliveryOptionsEvent)
+ });
}
deliveryOptions.rendered(true);
@@ -200,8 +153,8 @@ define(
* @returns {integer|null} - The house number, if found. Otherwise null.
*/
getHouseNumber: function(address) {
- var result = deliveryOptions.splitStreetRegex.exec(address);
- var numberIndex = 2;
+ const result = deliveryOptions.splitStreetRegex.exec(address),
+ numberIndex = 2;
return result ? parseInt(result[numberIndex]) : null;
},
@@ -211,9 +164,7 @@ define(
* @param {string} identifier - Name of the event.
*/
triggerEvent: function(identifier) {
- var event = document.createEvent('HTMLEvents');
- event.initEvent(identifier, true, false);
- document.querySelector('body').dispatchEvent(event);
+ document.body.dispatchEvent(new Event(identifier, { bubbles: true, cancelable: false }));
},
/**
@@ -260,8 +211,8 @@ define(
* @param {CustomEvent} event - The event that was sent.
*/
onUpdatedDeliveryOptions: function(event) {
- var element = document.getElementsByClassName('checkout-shipping-method').item(0);
- var displayStyle = window.getComputedStyle(element, null).display;
+ const element = document.querySelector('.checkout-shipping-method'),
+ displayStyle = window.getComputedStyle(element, null).display;
if ('none' === displayStyle) {
return;
@@ -295,21 +246,7 @@ define(
if (!response.length) {
return;
}
-
- /**
- * For the cart summary to display the correct shipping method name on the
- * second page of the standard checkout, we need to update the storage.
- */
- var cacheObject = JSON.parse(localStorage.getItem('mage-cache-storage'));
- if (cacheObject.hasOwnProperty('checkout-data')) {
- cacheObject['checkout-data']['selectedShippingRate'] = response[0].element_id;
- localStorage.setItem('mage-cache-storage', JSON.stringify(cacheObject));
- }
- /**
- * Set the method to null first, for the price of options to update in the cart summary.
- */
- selectShippingMethodAction(null);
- selectShippingMethodAction(deliveryOptions.getNewShippingMethod(response[0].element_id));
+ selectShippingMethodAction(response[0]);
},
});
},
@@ -319,8 +256,8 @@ define(
* Until there's a built in solution, there's the following workaround.
*/
disabledDeliveryPickupRadio: function() {
- var delivery = document.getElementById(deliveryOptions.disableDelivery);
- var pickup = document.getElementById(deliveryOptions.disablePickup);
+ const pickup = document.getElementById(deliveryOptions.disablePickup),
+ delivery = document.getElementById(deliveryOptions.disableDelivery);
if (delivery) {
delivery.disabled = false;
@@ -337,17 +274,17 @@ define(
* @param {Object} selectedShippingMethod - The shipping method that was selected.
*/
onShippingMethodUpdate: function(selectedShippingMethod) {
- var newShippingMethod = selectedShippingMethod || {};
- var available = newShippingMethod.available || false;
- var isMyParcelMethod = deliveryOptions.isMyParcelShippingMethod(newShippingMethod);
-
+ const newShippingMethod = selectedShippingMethod || {},
+ available = newShippingMethod.available || false,
+ carrierCode = newShippingMethod.carrier_code || '',
+ myparcelCarrierCode = checkout.carrierCode;
checkout.hideShippingMethods();
if (!checkout.hasDeliveryOptions() || !available) {
return;
}
- if (!isMyParcelMethod) {
+ if (carrierCode !== myparcelCarrierCode) {
deliveryOptions.triggerEvent(deliveryOptions.disableDeliveryOptionsEvent);
deliveryOptions.isUsingMyParcelMethod = false;
return;
@@ -357,44 +294,6 @@ define(
deliveryOptions.isUsingMyParcelMethod = true;
},
- /**
- * Get the new shipping method that should be saved.
- *
- * @param {string} methodCode - Method code to use to find a method.
- *
- * @returns {Object}
- */
- getNewShippingMethod: function(methodCode) {
- var newShippingMethod = [];
- var matchingShippingMethod = checkout.findRateByMethodCode(methodCode);
-
- if (matchingShippingMethod) {
- return matchingShippingMethod;
- } else {
- /**
- * If the method doesn't exist, loop through the allowed shipping methods and return the first one that
- * matches.
- */
- checkout.allowedShippingMethods().forEach(function(carrierCode) {
- var foundRate = checkout.findOriginalRateByCarrierCode(carrierCode);
-
- if (foundRate) {
- newShippingMethod.push(foundRate);
- }
- });
-
- return newShippingMethod.length ? newShippingMethod[0] : null;
- }
- },
-
- /**
- * @param {Object} shippingMethod
- * @returns {boolean}
- */
- isMyParcelShippingMethod: function(shippingMethod) {
- return shippingMethod.available && shippingMethod.method_code.indexOf('myparcel') !== -1;
- },
-
/**
* For use when magic decimals appear...
*
@@ -405,8 +304,7 @@ define(
* @see https://stackoverflow.com/a/10474209
*/
roundNumber: function(number, decimals) {
- var newNumber = Number(String(number)).toFixed(decimals);
- return parseFloat(newNumber);
+ return parseFloat(Number(String(number)).toFixed(decimals));
},
updateConfig: function() {
diff --git a/view/frontend/web/js/view/shipping.js b/view/frontend/web/js/view/shipping.js
index bde6fe5b..7d61ca50 100644
--- a/view/frontend/web/js/view/shipping.js
+++ b/view/frontend/web/js/view/shipping.js
@@ -34,7 +34,6 @@ define([
*/
checkout.hasDeliveryOptions.subscribe(function(enabled) {
checkout.hideShippingMethods();
-
if (enabled) {
deliveryOptions.initialize();
} else {