Price Detection in Booking Management
๐งช
Displays original and updated price when rescheduling a booking in the booking management page. The updated price is calculated based on the newly selected dates and the original time parts.
Instructions
See “Where do I put snippets?” in our documentation for installation instructions.
Code
Filename: gpb-price-detection-booking-management.php
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
/**
* Gravity Perks // Bookings // Price Detection in Booking Management
* https://gravitywiz.com/documentation/gravity-forms-bookings/
*
* Experimental Snippet ๐งช
*
* https://www.loom.com/share/56c0a90053ac4739981c08338f388087
*
* Displays original and updated price when rescheduling a booking in the booking management page.
* The updated price is calculated based on the newly selected dates and the original time parts.
*/
add_action( 'wp_footer', function () {
?>
<style>
.gpb-custom-reschedule-pricing {
padding: 1rem;
margin-top: 1rem;
font-size: 16px;
background: rgba(var(--gform-theme-color-danger-rgb, 192, 43, 10), 0.08);
border: 1px solid var(--gform-theme-color-danger, #c02b0a);
border-radius: 4px;
color: var(--gform-theme-color-danger, #c02b0a);
}
.gpb-custom-reschedule-pricing p {
margin: 0;
line-height: 1.6;
}
</style>
<script>
(function () {
if (!window.gpbBookingManagement || !Array.isArray(window.gpbBookingManagement.bookings)) {
return;
}
function groupBookings(bookings) {
const resourceGroups = new Map();
bookings.forEach((b) => {
if (b.objectType === 'resource' && b.parentBookingId) {
if (!resourceGroups.has(b.parentBookingId)) {
resourceGroups.set(b.parentBookingId, []);
}
resourceGroups.get(b.parentBookingId).push(b);
}
});
const groups = [];
resourceGroups.forEach((resources, parentId) => {
if (resources.length) {
const first = resources[0];
groups.push({
service: { ...first, id: parentId, objectType: 'service' },
resources,
});
}
});
bookings.forEach((b) => {
if (!(b.objectType === 'resource' && b.parentBookingId)) {
groups.push({ service: { ...b, objectType: 'service' }, resources: [] });
}
});
return groups;
}
function toDateTime(input) {
return (input || '').toString().replace('T', ' ').slice(0, 19);
}
function getTimePart(dateTime) {
const str = toDateTime(dateTime);
return str.split(' ')[1] || '00:00:00';
}
function formatMoney(value) {
const n = Number(value || 0);
const code =
window.gp_bookings_field_booking_time_strings?.currency?.code ||
window.gp_bookings_field_booking_strings?.currency?.code ||
'USD';
try {
return new Intl.NumberFormat(undefined, {
style: 'currency',
currency: code,
}).format(n);
} catch (e) {
return n.toFixed(2);
}
}
async function calcPrice(payload) {
const res = await fetch('/wp-json/gp-bookings/v1/calculate-price', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
});
if (!res.ok) {
throw new Error('Pricing request failed');
}
const json = await res.json();
return Number(json.price || 0);
}
function getSelectedRange(card) {
const startEl = card.querySelector('.rdp-day.rdp-range_start[data-day]');
const endEl = card.querySelector('.rdp-day.rdp-range_end[data-day]');
if (startEl && endEl) {
return {
startDate: startEl.getAttribute('data-day'),
endDate: endEl.getAttribute('data-day'),
};
}
// Fixed/single fallback
const selected = card.querySelectorAll('.rdp-day.rdp-selected[data-day]');
if (selected.length) {
const first = selected[0].getAttribute('data-day');
const last = selected[selected.length - 1].getAttribute('data-day');
return { startDate: first, endDate: last };
}
return null;
}
function ensureBox(rangeInfoEl) {
const wrap = rangeInfoEl.parentElement;
if (!wrap) return null;
let box = wrap.querySelector('.gpb-custom-reschedule-pricing');
if (!box) {
box = document.createElement('div');
box.className = 'gpb-custom-reschedule-pricing';
box.innerHTML = '<p>Original Price: โ<br>Updated Price: โ</p>';
}
// Always force correct order: blue range info first, red box immediately after.
if (rangeInfoEl.nextElementSibling !== box) {
rangeInfoEl.insertAdjacentElement('afterend', box);
}
return box;
}
const groups = groupBookings(window.gpbBookingManagement.bookings || []);
function bindCard(card, group) {
if (!group || card.dataset.gpbPricingBound === '1') return;
card.dataset.gpbPricingBound = '1';
let originalPrice = null;
let lastKey = '';
async function render() {
const rangeInfo = card.querySelector('.gpb-manage-booking-reschedule .gpb-booking-time-picker__range-info');
if (!rangeInfo) return;
const box = ensureBox(rangeInfo);
const sel = getSelectedRange(card);
if (!sel?.startDate || !sel?.endDate) return;
const service = group.service;
const resourceIds = (group.resources || []).map((r) => Number(r.objectId)).filter(Boolean);
const originalStart = toDateTime(service.start);
const originalEnd = toDateTime(service.end);
const startTime = getTimePart(originalStart);
const endTime = getTimePart(originalEnd);
const updatedStart = `${sel.startDate} ${startTime}`;
const updatedEnd = `${sel.endDate} ${endTime}`;
const key = `${updatedStart}|${updatedEnd}|${service.serviceId}|${resourceIds.join(',')}`;
if (key === lastKey) return;
lastKey = key;
try {
if (originalPrice === null) {
originalPrice = await calcPrice({
service_id: Number(service.serviceId),
resource_ids: resourceIds,
start_time: originalStart,
end_time: originalEnd,
quantity: 1,
});
}
const updatedPrice = await calcPrice({
service_id: Number(service.serviceId),
resource_ids: resourceIds,
start_time: updatedStart,
end_time: updatedEnd,
quantity: 1,
});
box.innerHTML =
`<p>Original Price: ${formatMoney(originalPrice)}<br>` +
`Updated Price: ${formatMoney(updatedPrice)}</p>`;
} catch (e) {
box.innerHTML = '<p>Original Price: โ<br>Updated Price: โ</p>';
}
}
const mo = new MutationObserver(() => { render(); });
mo.observe(card, { childList: true, subtree: true, attributes: true });
render();
}
function init() {
const cards = document.querySelectorAll('.gpb-manage-booking');
cards.forEach((card, i) => bindCard(card, groups[i]));
}
document.addEventListener('DOMContentLoaded', init);
const rootObserver = new MutationObserver(init);
rootObserver.observe(document.body, { childList: true, subtree: true });
})();
</script>
<?php
}, 99 );