'use strict'; (function () { var config = {"id":"product-shipping-estimator","sku":"V516-ASTE16019184110","title":"Postcode Shipping Calculator","apiHost":"https://edge.paydaydeals.com.au","currency":"AUD"}; var html = "
Postcode Shipping Calculator
"; var css = " .advcal__estimator { margin: 20px 0; } .advcal__estimator-title { font-size: 18px; } .advcal__estimator-input-wrapper { width: 49%; float: left; } .advcal__estimator-button { width: 49%; float: left; margin-left: 2%; } .advcal__estimator-error { color: #ff0033; } .advcal__estimator-results { clear: both; } .advcal__estimator__rate-service-name--free { font-weight: 500; color: #090; } .advcal__estimator__rate-service-cost { font-weight: 500; } .advcal__estimator-rates { overflow: hidden; } .advcal__estimator-rate { margin-top: 10px; } @media screen and (min-width: 768px) { .advcal__estimator-input-wrapper { width: 73%; } .advcal__estimator-button { width: 25%; } } "; var element = document.getElementById(config.id); if (!element) { console.error('Cannot find element with ID ' + config.id); return null; } function inel() { element.innerHTML = html; var head = document.head || document.getElementsByTagName('head')[0]; var style = document.createElement('style'); style.type = 'text/css'; if (style.styleSheet) { style.styleSheet.cssText = css; } else { style.appendChild(document.createTextNode(css)); } head.appendChild(style); }; function wue() { var eleInput = document.querySelector("#" + config.id + " .advcal__estimator-input"); var eleButton = document.querySelector("#" + config.id + " .advcal__estimator-button"); var eleResults = document.querySelector("#" + config.id + " .advcal__estimator-results"); var eleRates = document.querySelector("#" + config.id + " .advcal__estimator-rates"); var eleError = document.querySelector("#" + config.id + " .advcal__estimator-error"); var showError = function showError(error) { console.log(eleError.innerHTML, error); eleError.innerHTML = error; }; eleButton.onclick = async function () { eleRates.innerHTML = ''; eleError.innerHTML = ''; const response = await fetch(config.apiHost + '/shipping/estimate', { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, method: 'POST', body: JSON.stringify({ items: [{ sku: config.sku, quantity: 1 }], postcode: eleInput.value, currency: config.currency }) }); const estimated = await response.json(); if (!estimated || !estimated.rates) { return showError('No estimation available to your destination'); } const r = estimated.rates.filter(r => r.sku === config.sku && r.services && r.services.length > 0); if (r.length === 0) { return showError('No shipping method available to your destination'); } r.map(function (rate) { rate.services.map(function (svc) { html = (rate.total_price < 0.01) ? '' + svc.name + '' : '' + svc.name + ': $' + (svc.amount / 100).toFixed(2) + ' ' + config.currency + ''; var itemEle = document.createElement('li'); itemEle.className = 'advcal__estimator-rate advcal__estimator-rate--' + (svc.name.toLowerCase().replace(/s/g,'-')); itemEle.innerHTML = html; eleRates.appendChild(itemEle); }) }); }; }; inel(); wue(); })();