|
5 | 5 |
|
6 | 6 | define([ |
7 | 7 | 'jquery', |
8 | | - 'jquery-ui-modules/widget' |
| 8 | + 'jquery-ui-modules/widget', |
| 9 | + 'Magefan_ProductLabel/js/label' |
9 | 10 | ], function ($) { |
10 | 11 | 'use strict'; |
11 | 12 |
|
12 | 13 | return function (SwatchRenderer) { |
13 | 14 | $.widget('mage.SwatchRenderer', SwatchRenderer, { |
14 | 15 | _OnClick: function ($this, $widget) { |
15 | 16 | this._super($this, $widget); |
16 | | - this.dispatchItemSelect(); |
| 17 | + this.processLable(); |
17 | 18 | }, |
18 | 19 |
|
19 | 20 | _OnChange: function ($this, $widget) { |
20 | 21 | this._super($this, $widget); |
21 | | - this.dispatchItemSelect(); |
| 22 | + this.processLable(); |
22 | 23 | }, |
23 | 24 |
|
24 | | - dispatchItemSelect: function () { |
| 25 | + processLable: function () { |
25 | 26 | if (this.options.jsonConfig.productId) { |
26 | | - this.processProductLabel(this.options.jsonConfig.productId, this.getProductId()); |
27 | | - } |
28 | | - |
29 | | - $(document).trigger('mfChildItemSelected', { |
30 | | - mainProductId: this.options.jsonConfig.productId, |
31 | | - selectedProductId: this.getProductId()}); |
32 | | - }, |
33 | | - |
34 | | - processProductLabel: function (maintProductID, selectedProductId) { |
35 | | - var self = this; |
36 | | - console.log({maintProductID}) |
37 | | - console.log({selectedProductId}) |
38 | | - |
39 | | - |
40 | | - if (!window.mfLabelProcessed) { |
41 | | - window.mfLabelProcessed = {}; |
42 | | - } |
43 | | - |
44 | | - if (!window.mfLabelProcessed[maintProductID]) { |
45 | | - let url = BASE_URL + 'mfpl/label/get?product_ids=' + maintProductID + '&get_children=1&product_page=' + (this.inProductList ? '0' : '1'); |
| 27 | + let lableEl = null; |
| 28 | + |
| 29 | + if (this.inProductList) { |
| 30 | + const listItem = this.element.closest('li.item').get(0); // Convert jQuery object to a native DOM element |
46 | 31 |
|
47 | | - MagefanJs.ajax({'url':url, 'type': 'GET', |
48 | | - success: function(response) { |
49 | | - response = JSON.parse(response) |
50 | | - console.log(response); |
51 | | - window.mfLabelProcessed[maintProductID] = response.labels; |
52 | | - self.replaceLabel(maintProductID, selectedProductId) |
| 32 | + if (listItem) { |
| 33 | + lableEl = listItem.querySelector('.mf-label-container'); |
53 | 34 | } |
54 | | - }); |
55 | | - } else { |
56 | | - self.replaceLabel(maintProductID, selectedProductId) |
57 | | - } |
58 | | - }, |
59 | | - |
60 | | - replaceLabel: function (maintProductID, selectedProductId) { |
61 | | - let mainLableEl = null; |
62 | | - |
63 | | - if (this.inProductList) { |
64 | | - mainLableEl = this.element.closest('li.item') |
65 | | - .find('.mf-label-container'); |
66 | | - } else { |
67 | | - mainLableEl = $('.mfpl-product-page'); |
68 | | - } |
69 | | - |
70 | | - console.log({mainLableEl}); |
71 | | - let labelHtml = window.mfLabelProcessed[maintProductID] && window.mfLabelProcessed[maintProductID][selectedProductId] |
72 | | - ? window.mfLabelProcessed[maintProductID][selectedProductId] : ''; |
73 | | - console.log(labelHtml); |
74 | | - |
75 | | - if (labelHtml) { |
76 | | - mainLableEl.replaceWith(labelHtml); |
| 35 | + } else { |
| 36 | + lableEl = document.querySelector('.mfpl-product-page'); |
| 37 | + } |
| 38 | + |
| 39 | + console.log({lableEl}); |
| 40 | + |
| 41 | + if (lableEl) { |
| 42 | + MagefanPL.processConfigurableProductLabel(lableEl, this.options.jsonConfig.productId, this.getProductId()); |
| 43 | + } |
77 | 44 | } |
78 | 45 | } |
79 | 46 | }); |
|
0 commit comments