Skip to content
Permalink
Browse files

[IMP] website_sale_delivery: load carrier price async

Before this commit:
User landing on checkout payment step had to select a carrier.
These carriers only showed 'Select to compute price'. This was done to
prevent the page to take seconds to load, since it would compute all the
carrier price by querying these carrier API then display the page.
The users would still click on every carrier anyway to check all the prices and
select the cheapest one.

Now:
Once the page is loaded, we will asynchronously retrieve every carrier prices.
That way, the page rendering page will not be affected and the user will be
able to see the price of every carrier after a few seconds without having to do
anything.

task-1867265
  • Loading branch information...
rdeodoo committed Mar 14, 2019
1 parent 8c3bf5e commit 0dac0591781c82cb64a77a01323aa1300f3e7a59
@@ -1105,6 +1105,22 @@ def country_infos(self, country, mode, **kw):
phone_code=country.phone_code
)

@http.route(['/shop/carrier_rate_shipment'], type='json', auth='public', methods=['POST'], website=True)
def cart_carrier_rate_shipment(self, carrier_id, **kw):
order = request.website.sale_get_order(force_create=True)
carrier = request.env['delivery.carrier'].browse(int(carrier_id))
rate = carrier.rate_shipment(order)
res = {'carrier_id': carrier_id}
if rate.get('success'):
res['status'] = True
res['new_amount_delivery'] = rate['price']
res['error_message'] = rate['warning_message']
else:
res['status'] = False
res['new_amount_delivery'] = 0.0
res['error_message'] = rate['error_message']
return res

@http.route(['/shop/update_carrier'], type='json', auth='public', methods=['POST'], website=True, csrf=False)
def update_eshop_carrier(self, **post):
results = {}
@@ -19,20 +19,41 @@ publicWidget.registry.websiteSaleDelivery = publicWidget.Widget.extend({
* @override
*/
start: function () {
var self = this;
var $carriers = $('#delivery_carrier input[name="delivery_type"]');
// Workaround to:
// - update the amount/error on the label at first rendering
// - prevent clicking on 'Pay Now' if the shipper rating fails
if ($carriers.length > 0) {
$carriers.filter(':checked').click();
}

// Asynchronously retrieve every carrier price
_.each($carriers, function (carrierInput, k) {
self._showLoading($(carrierInput));
self._rpc({
route: '/shop/carrier_rate_shipment',
params: {
carrier_id: carrierInput.value,
},
}).then(self._handleCarrierUpdateResultBadge);
});

return this._super.apply(this, arguments);
},

//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------

/**
* @private
* @param {jQuery} $carrierInput
*/
_showLoading: function ($carrierInput) {
$carrierInput.siblings('.o_delivery_badge_price').addClass('d-none');
$carrierInput.siblings('.o_delivery_compute').removeClass('d-none').html('<span class="fa fa-spinner fa-spin"/>');
},
/**
* @private
* @param {Object} result
@@ -102,6 +123,7 @@ publicWidget.registry.websiteSaleDelivery = publicWidget.Widget.extend({
*/
_onCarrierClick: function (ev) {
var $radio = $(ev.currentTarget).find('input[type="radio"]');
this._showLoading($radio);
$radio.prop("checked", true);
var $payButton = $('#o_payment_form_pay');
$payButton.prop('disabled', true);
@@ -31,7 +31,7 @@
<span t-else="" t-att-class="badge_class">Free</span>
</t>
<t t-else="">
<span t-attf-class="#{badge_class} d-none" t-field="delivery.fixed_price" t-options='{"widget": "monetary", "from_currency": delivery.product_id.company_id.currency_id, "display_currency": website_sale_order.currency_id}'/>
<span t-attf-class="#{badge_class} o_delivery_badge_price d-none" t-field="delivery.fixed_price" t-options='{"widget": "monetary", "from_currency": delivery.product_id.company_id.currency_id, "display_currency": website_sale_order.currency_id}'/>
<span t-attf-class="#{badge_class} o_delivery_compute">Select to compute delivery rate</span>
</t>
<t t-if="delivery.website_description">

0 comments on commit 0dac059

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.