| @@ -184,16 +184,46 @@ | ||
| 184 | 184 | this.forms[productId] = quoteForm; |
| 185 | 185 | }); |
| 186 | 186 | }; |
| 187 | 187 | |
| 188 | + /** | |
| 189 | + * Resolve the modal for a trigger: its own product's modal, or the shared modal of its parent | |
| 190 | + * (variations share the parent's modal so triggers inserted by AJAX still have one to open). | |
| 191 | + * Falls back to the enclosing pricing wrapper for themes overriding the button template. | |
| 192 | + */ | |
| 193 | + this.resolveForm = function ($trigger) { | |
| 194 | + const productId = $trigger.data('product-id'); | |
| 195 | + | |
| 196 | + if (this.forms[productId]) { | |
| 197 | + return this.forms[productId]; | |
| 198 | + } | |
| 199 | + | |
| 200 | + const parentId = $trigger.data('parent-id') || $trigger.closest('.tpt__tiered-pricing').data('product-id'); | |
| 201 | + | |
| 202 | + return parentId ? this.forms[parentId] : undefined; | |
| 203 | + }; | |
| 204 | + | |
| 205 | + this.openForTrigger = function ($trigger) { | |
| 206 | + const form = this.resolveForm($trigger); | |
| 207 | + | |
| 208 | + if (!form) { | |
| 209 | + return; | |
| 210 | + } | |
| 211 | + | |
| 212 | + const productId = $trigger.data('product-id'); | |
| 213 | + | |
| 214 | + // Opened from a variation's trigger: make the modal submit that variation. | |
| 215 | + if (productId && String(productId) !== String(form.productId)) { | |
| 216 | + form.updateState({ variationId: productId, parentId: form.productId }); | |
| 217 | + } | |
| 218 | + | |
| 219 | + form.open(); | |
| 220 | + }; | |
| 221 | + | |
| 188 | 222 | this.bindGlobalEvents = function () { |
| 189 | 223 | $(document).on('click', '.tpt-request-quote-trigger', (e) => { |
| 190 | 224 | e.preventDefault(); |
| 191 | - const productId = $(e.currentTarget).data('product-id'); | |
| 192 | - | |
| 193 | - if (this.forms[productId]) { | |
| 194 | - this.forms[productId].open(); | |
| 195 | - } | |
| 225 | + this.openForTrigger($(e.currentTarget)); | |
| 196 | 226 | }); |
| 197 | 227 | |
| 198 | 228 | $(document).on('tiered_price_update', (event, data) => { |
| 199 | 229 | if (!data || !data.__instance || !data.__instance.formatting) return; |
| @@ -229,16 +259,15 @@ | ||
| 229 | 259 | if (this.autoOpened || isNaN(qty)) return; |
| 230 | 260 | |
| 231 | 261 | let $trigger; |
| 232 | 262 | if (productId) { |
| 233 | - $trigger = $(`.tpt-request-quote-trigger[data-product-id="${productId}"]`).first(); | |
| 263 | + $trigger = $(`.tpt-request-quote-trigger[data-product-id="${productId}"], .tpt-request-quote-trigger[data-parent-id="${productId}"]`).first(); | |
| 234 | 264 | } else { |
| 235 | 265 | $trigger = $('.tpt-request-quote-trigger').first(); |
| 236 | 266 | } |
| 237 | 267 | |
| 238 | 268 | if ($trigger.length) { |
| 239 | - const targetProductId = $trigger.data('product-id'); | |
| 240 | - const form = this.forms[targetProductId]; | |
| 269 | + const form = this.resolveForm($trigger); | |
| 241 | 270 | |
| 242 | 271 | if (form) { |
| 243 | 272 | const triggerAttr = $trigger.attr('data-auto-open-quantity'); |
| 244 | 273 | let autoOpenQty = null; |