PageSourceSearch

https://www.drumchannel.com/b4744271-64d1-4a48-a4c3-9d7fe290edfb/1…/basic-affiliate/js/ba-coupon-auto-apply.js

js drumchannel.com collected 2026-10-02 09:54:30 UTC 2,314 bytes, 71 lines download raw bytes

1(function() {
2  'use strict';
3
4  function getCookie(name) {
5    var match = document.cookie.match(new RegExp('(?:^|; )' + name + '=([^;]*)'));
6    return match ? decodeURIComponent(match[1]) : '';
7  }
8
9  function applyCouponToField(couponField, couponCode) {
10    // Don't override if coupon already has a value
11    if (couponField.value.trim() !== '') return;
12
13    // Show the coupon container (remove mepr-hidden)
14    var container = couponField.closest('.mepr_coupon');
15    if (container) {
16      container.classList.remove('mepr-hidden');
17    }
18
19    // Hide the "Have a coupon?" link for this form
20    var form = couponField.closest('.mepr-signup-form') || couponField.closest('form');
21    if (form) {
22      var couponLink = form.querySelector('.have-coupon-link');
23      if (couponLink) {
24        couponLink.style.display = 'none';
25      }
26    }
27
28    // Set the value and trigger MemberPress validation via blur
29    couponField.value = couponCode;
30    couponField.dispatchEvent(new Event('blur', { bubbles: true }));
31  }
32
33  function init() {
34    // The server already applied the coupon for this request (see
35    // MemberpressIntegration::maybe_apply_coupon_server_side). It also
36    // fixed the things this script can't reach - tab labels, terms, the
37    // invoice - so there is nothing left to do here.
38    if (typeof baCouponAutoApply !== 'undefined' && baCouponAutoApply.server_applied) return;
39
40    var couponFields = document.querySelectorAll('input.mepr-coupon-code');
41    if (!couponFields.length) return;
42
43    var affiliateCode = getCookie('dc_ba_aff_c');
44    if (!affiliateCode) return;
45
46    var data = new FormData();
47    data.append('action', 'ba_get_affiliate_coupon');
48    data.append('code', affiliateCode);
49    data.append('nonce', baCouponAutoApply.nonce);
50
51    fetch(baCouponAutoApply.ajax_url, {
52      method: 'POST',
53      credentials: 'same-origin',
54      body: data
55    })
56    .then(function(response) { return response.json(); })
57    .then(function(result) {
58      if (!result.success || !result.data.coupon_code) return;
59
60      couponFields.forEach(function(field) {
61        applyCouponToField(field, result.data.coupon_code);
62      });
63    });
64  }
65
66  if (document.readyState === 'loading') {
67    document.addEventListener('DOMContentLoaded', init);
68  } else {
69    init();
70  }
71})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.