commerce_shipping.js
1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
(function($) {
/**
* Adds a shipping quote recalculation change callback to customer profile fields.
*/
Drupal.behaviors.commerceShipping = {
attach: function (context, settings) {
$('[id^="edit-customer-profile-"] .form-item', context).children('.form-select, .form-text, .form-radio, .form-checkbox:not([name*="[commerce_customer_profile_copy]"])').filter(':not(.shipping-recalculation-processed)').addClass('shipping-recalculation-processed').change(function() {
return $.fn.commerceCheckShippingRecalculation();
});
$(window).load(function() {
return $.fn.commerceCheckShippingRecalculation();
});
}
}
/**
* Checks to see if we can recalculate shipping rates and dispatches the command.
*/
$.fn.commerceCheckShippingRecalculation = function() {
var recalculate = true;
// Define the callback used with setTimeout to click the recalculation button
// if there is ongoing AJAX operation.
var recalculateCallback = function() {
if ($('[id^="edit-customer-profile-"]').find('.ajax-progress').length) {
return setTimeout($.fn.commerceCheckShippingRecalculation, 100);
}
// Trigger the click event on the shipping recalculation button.
$('[id^="edit-commerce-shipping-recalculate"]').trigger('click');
};
// If other ajax logic is still running we can ignore the empty field check,
// as we can expect the forms to change and we want to see what the backend
// think we should do.
if (!$('[id^="edit-customer-profile-"]').find('.ajax-progress').length) {
$('[id^="edit-customer-profile-shipping"] .form-item').children('.required').each(function() {
if (!$(this).val() || $(this).val() == '' || $(this).val() == null) {
recalculate = false;
}
});
}
if (recalculate == true) {
return setTimeout(recalculateCallback, 100);
}
}
})(jQuery);