Skip to content

Instantly share code, notes, and snippets.

@vensires
Created January 10, 2018 12:04
Show Gist options
  • Save vensires/31d8823e4c02c31884a3006c81a6426b to your computer and use it in GitHub Desktop.
Save vensires/31d8823e4c02c31884a3006c81a6426b to your computer and use it in GitHub Desktop.
Contains various general fixes for the Drupal Commerce checkout page. I usually attach the JS files in the commerce checkout page using a single-page checkout.
(function($) {
/*
* Disable the Checkout Continue button when an AJAX request is pending but
* reenable it when all AJAX requests have ended.
*/
Drupal.behaviors.commerceOverridesCheckoutContinueButton = {
attach: function (context, settings) {
var submitButton = $('body.page-checkout-checkout form[id^="commerce-checkout-form-checkout"] input.checkout-continue');
if (submitButton.length) {
// Disable the submit button when an AJAX request starts.
$(document).ajaxStart(function () {
submitButton.attr('disabled', 'disabled');
});
// Re-enable the submit button when all AJAX requests have ended.
$(document).ajaxStop(function () {
submitButton.removeAttr('disabled');
});
}
}
};
})(jQuery);
(function($) {
/**
* Drupal.behaviors.commerceShipping triggers the recalculation button on
* change event. According to jQuery's documentation "For select boxes,
* checkboxes, and radio buttons, the event is fired immediately when the user
* makes a selection with the mouse, but for the other element types the event
* is deferred until the element loses focus". To fix that, we set a timeout
* in the near future when the keyup event of a textfield is triggered.
*/
Drupal.behaviors.commerceOverridesRefreshWhenShippingIsFilled = {
attach: function(context, settings) {
if (typeof $.fn.commerceCheckShippingRecalculation === "function") {
var timeoutID;
$('[id^="edit-customer-profile-"] .form-item', context).children('.form-text').keyup(function() {
var _ = $(this);
if (timeoutID) {
clearTimeout(timeoutID);
}
timeoutID = setTimeout(function() {
_.trigger('change');
}, 1500);
});
}
}
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment