Skip to content

Commit 6a98afc

Browse files
Miroslavbojanz
authored andcommitted
Issue #2896303 by Dom., sorabh.v6, bojanz: Auto-submit coupon on press enter and avoid other form submit collision
1 parent 26ef9ff commit 6a98afc

3 files changed

Lines changed: 35 additions & 0 deletions

File tree

modules/promotion/commerce_promotion.libraries.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,11 @@ form:
33
css:
44
theme:
55
css/promotion.form.css: {}
6+
7+
coupon_redemption_form:
8+
version: VERSION
9+
js:
10+
js/commerce_promotion.coupon_redemption_form.js: {}
11+
dependencies:
12+
- core/jquery
13+
- core/drupal
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @file
3+
* Defines Javascript behaviors for the coupon redemption form.
4+
*/
5+
6+
(function ($, Drupal, drupalSettings) {
7+
'use strict';
8+
9+
Drupal.behaviors.commercePromotionCouponRedemptionForm = {
10+
attach: function (context) {
11+
// Trigger the "Apply" button when Enter is pressed in a code field.
12+
$('input[name$="[code]"]', context)
13+
.once('coupon-redemption-code')
14+
.keydown(function (event) {
15+
if (event.keyCode === 13) {
16+
// Prevent the browser default from being triggered.
17+
// That is usually the "Next" checkout button.
18+
event.preventDefault();
19+
$(':input[name="apply_coupon"]', context).trigger('mousedown');
20+
}
21+
});
22+
}
23+
};
24+
})(jQuery, Drupal, drupalSettings);

modules/promotion/src/Element/CouponRedemptionForm.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ public static function processForm(array $element, FormStateInterface $form_stat
8989

9090
$element = [
9191
'#tree' => TRUE,
92+
'#attached' => [
93+
'library' => ['commerce_promotion/coupon_redemption_form'],
94+
],
9295
'#theme' => 'commerce_coupon_redemption_form',
9396
'#prefix' => '<div id="' . $wrapper_id . '">',
9497
'#suffix' => '</div>',

0 commit comments

Comments
 (0)