Alejandro Troncoso
2021-08-05T21:18:03Z

Add Estimations in Checkout form.js

| function cleanEstimates() {
| // remove all prices and errors
| $('#shipping_options li').each(function() {
| $(this).children().last().detach();
| });
| // add empty messages - placeholders
| $('#shipping_options li').each(function() {
| $(this).append('')
| });
| }
|
| function shippingEstimates() {
| cleanEstimates();
| if ($('#order_shipping_address_country').val() != "" && $('#order_shipping_address_region').val() != "") {
| $.ajax({
| method: "POST",
| url: "/checkout/shipping_estimate",
| data: {
| estimate: {
| country: $('#order_shipping_address_country').val(),
| region: $('#order_shipping_address_region').val(),
| municipality: $('#order_shipping_address_municipality').val(),
| postal: $('#order_shipping_address_postal').val(),
| city: $('#order_shipping_address_city').val(),
| address: $('#order_shipping_address_address').val(),
| }
| }
| }).done(function(data) {
| for (var i = 0; i < data.length; i++) {
| let amount;
| let old_currency;
| let new_currency;
| let currency_amount;
| if (typeof(accounting) != "undefined") {
| old_currency = sessionStorage.getItem('store_currency');
| new_currency = $.trim(sessionStorage.getItem('global_currency'));
| if (old_currency == new_currency) {
| currency_amount = data[i].table.price;
| } else {
| amount = accounting.unformat(data[i].table.price, i18n_decimal_mark);
| currency_amount = accounting.formatMoney(fx.convert(amount, {
| from: old_currency,
| to: new_currency
| }), {
| symbol: {
| EUR: "€",
| GBP: "₤"
| } [new_currency]
| })
| }
| }
| let shipping_method = $('#shipping_options #order_shipping_method_' + data[i].table.id);
| // remove any previous messages & placeholders
| shipping_method.parent().children().last().detach();
| if (data[i].table.error) {
| // disable options with errors
| shipping_method.attr('disabled', 'disabled');
| shipping_method.prop('checked', false);
| // add error messages
| shipping_method.parent().append(" " + data[i].table.error_message + " ")
| } else {
| // enable options
| shipping_method.attr('disabled', false);
| if ($("#shipping_options").find("input[type='radio']:checked").not("[disabled]").length == 0) {
| shipping_method.prop('checked', true);
| }
| // add formatted shipping prices
| shipping_method.parent().append(" " + (typeof(accounting) != "undefined" ? currency_amount : data[i].table.price) + " ")
| }
| }
| // Disable Review Order Button if Invalid Shipping Method
| function CheckShippingMethods() {
| if ($("#shipping_options").find("input[type='radio']:checked").not("[disabled]").length > 0) {
| $("#submit_review_order_2").prop("disabled", false);
| } else {
| $("#submit_review_order_2").prop("disabled", true);
| }
| }
| $(document).ready(CheckShippingMethods);
| $("#shipping_options input[type='radio']").on('change', CheckShippingMethods);
| });
| } else {
| // no Country or Region filled, clear shipping estimate info
| cleanEstimates();
| }
| }
| /* LISTENERS */
| var debounceTimer = null; // so it does only a single request instead of lots of them.
| $('#order_shipping_address_country, #order_shipping_address_region, #order_shipping_address_municipality, #order_shipping_address_city, #order_shipping_address_postal, #order_shipping_address_address').change(function() {
| clearTimeout(debounceTimer);
| debounceTimer = setTimeout(function() {
| shippingEstimates();
| }, 300);
| });
| // END OF LISTENERS
| $(document).ready(function() {
| // add empty messages - placeholders
| $('#shipping_options li').each(function() {
| $(this).append('')
| });
| shippingEstimates();
| })