Skip to content

Commit fcfd38b

Browse files
author
b__b
committed
12273 hyva compatibility
1 parent 924ee46 commit fcfd38b

File tree

4 files changed

+112
-23
lines changed

4 files changed

+112
-23
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magefan (support@magefan.com). All rights reserved.
5+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
6+
*/
7+
-->
8+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
9+
<body>
10+
<referenceContainer name="after.body.start">
11+
<block name="mf.product.labels.js" template="Magefan_ProductLabel::hyva/js.phtml" />
12+
</referenceContainer>
13+
</body>
14+
</page>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan (support@magefan.com). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*/
6+
?>
7+
<script>
8+
/* copy of Magefan_ProductLabel::js/label */
9+
var MagefanPL = {
10+
processConfigurableProductLabel: function (labelEl, maintProductID, selectedProductId, forProductPage = 0) {
11+
var self = this;
12+
13+
if (!window.mfLabelProcessed) {
14+
window.mfLabelProcessed = {};
15+
}
16+
17+
if (!window.mfLabelProcessed[maintProductID]) {
18+
const url = `${BASE_URL}mfpl/label/get?product_ids=${maintProductID}&get_children=1&product_page=${forProductPage}`;
19+
20+
MagefanJs.ajax({'url':url, 'type': 'GET',
21+
success: function(response) {
22+
response = JSON.parse(response)
23+
window.mfLabelProcessed[maintProductID] = response.labels;
24+
self.replaceLabel(labelEl, maintProductID, selectedProductId)
25+
}
26+
});
27+
} else {
28+
self.replaceLabel(labelEl, maintProductID, selectedProductId)
29+
}
30+
},
31+
32+
replaceLabel: function (labelEl, maintProductID, selectedProductId) {
33+
34+
let labelHtml = window.mfLabelProcessed[maintProductID] && window.mfLabelProcessed[maintProductID][selectedProductId]
35+
? window.mfLabelProcessed[maintProductID][selectedProductId] : '';
36+
37+
if (labelHtml) {
38+
const tempDiv = document.createElement('div');
39+
tempDiv.innerHTML = labelHtml;
40+
const newLabel = tempDiv.firstElementChild;
41+
42+
if (newLabel) {
43+
labelEl.replaceWith(newLabel);
44+
}
45+
}
46+
}
47+
};
48+
49+
window.addEventListener('configurable-selection-changed', function (e) {
50+
let labelEl = document.querySelector('.mfpl-product-page'),
51+
forProductPage = 1;
52+
53+
if (labelEl) {
54+
MagefanPL.processConfigurableProductLabel(
55+
labelEl,
56+
e.detail.productId,
57+
e.detail.productIndex,
58+
forProductPage
59+
);
60+
}
61+
});
62+
63+
window.addEventListener('listing-configurable-selection-changed', function (e) {
64+
let labelEl = document.querySelector('input[name="product"][value="'+ e.detail.productId +'"]').closest('form').querySelector('.mf-label-container'),
65+
forProductPage = 0;
66+
67+
if (labelEl) {
68+
MagefanPL.processConfigurableProductLabel(
69+
labelEl,
70+
e.detail.productId,
71+
e.detail.productIndex,
72+
forProductPage
73+
);
74+
}
75+
});
76+
77+
</script>

view/frontend/web/js/label.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,40 @@
44
*/
55

66
var MagefanPL = {
7-
processConfigurableProductLabel: function (lableEl, maintProductID, selectedProductId) {
7+
processConfigurableProductLabel: function (labelEl, maintProductID, selectedProductId, forProductPage = 0) {
88
var self = this;
9-
console.log({maintProductID})
10-
console.log({selectedProductId})
11-
129

1310
if (!window.mfLabelProcessed) {
1411
window.mfLabelProcessed = {};
1512
}
1613

1714
if (!window.mfLabelProcessed[maintProductID]) {
18-
let url = BASE_URL + 'mfpl/label/get?product_ids=' + maintProductID + '&get_children=1&product_page=' + (this.inProductList ? '0' : '1');
15+
const url = `${BASE_URL}mfpl/label/get?product_ids=${maintProductID}&get_children=1&product_page=${forProductPage}`;
1916

2017
MagefanJs.ajax({'url':url, 'type': 'GET',
2118
success: function(response) {
2219
response = JSON.parse(response)
23-
console.log(response);
2420
window.mfLabelProcessed[maintProductID] = response.labels;
25-
self.replaceLabel(lableEl, maintProductID, selectedProductId)
21+
self.replaceLabel(labelEl, maintProductID, selectedProductId)
2622
}
2723
});
2824
} else {
29-
self.replaceLabel(lableEl, maintProductID, selectedProductId)
25+
self.replaceLabel(labelEl, maintProductID, selectedProductId)
3026
}
3127
},
3228

33-
replaceLabel: function (lableEl, maintProductID, selectedProductId) {
29+
replaceLabel: function (labelEl, maintProductID, selectedProductId) {
3430

3531
let labelHtml = window.mfLabelProcessed[maintProductID] && window.mfLabelProcessed[maintProductID][selectedProductId]
3632
? window.mfLabelProcessed[maintProductID][selectedProductId] : '';
37-
console.log(labelHtml);
3833

3934
if (labelHtml) {
4035
const tempDiv = document.createElement('div');
4136
tempDiv.innerHTML = labelHtml;
42-
const newLable = tempDiv.firstElementChild;
37+
const newLabel = tempDiv.firstElementChild;
4338

44-
if (newLable) {
45-
lableEl.replaceWith(newLable);
39+
if (newLabel) {
40+
labelEl.replaceWith(newLabel);
4641
}
4742
}
4843
}

view/frontend/web/js/swatch-renderer-mixin.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,35 @@ define([
1414
$.widget('mage.SwatchRenderer', SwatchRenderer, {
1515
_OnClick: function ($this, $widget) {
1616
this._super($this, $widget);
17-
this.processLable();
17+
this.processLabel();
1818
},
1919

2020
_OnChange: function ($this, $widget) {
2121
this._super($this, $widget);
22-
this.processLable();
22+
this.processLabel();
2323
},
2424

25-
processLable: function () {
25+
processLabel: function () {
2626
if (this.options.jsonConfig.productId) {
27-
let lableEl = null;
27+
let labelEl = null;
2828

2929
if (this.inProductList) {
3030
const listItem = this.element.closest('li.item').get(0); // Convert jQuery object to a native DOM element
3131

3232
if (listItem) {
33-
lableEl = listItem.querySelector('.mf-label-container');
33+
labelEl = listItem.querySelector('.mf-label-container');
3434
}
3535
} else {
36-
lableEl = document.querySelector('.mfpl-product-page');
36+
labelEl = document.querySelector('.mfpl-product-page');
3737
}
3838

39-
console.log({lableEl});
40-
41-
if (lableEl) {
42-
MagefanPL.processConfigurableProductLabel(lableEl, this.options.jsonConfig.productId, this.getProductId());
39+
if (labelEl) {
40+
MagefanPL.processConfigurableProductLabel(
41+
labelEl,
42+
this.options.jsonConfig.productId,
43+
this.getProductId(),
44+
this.inProductList ? 0 : 1
45+
);
4346
}
4447
}
4548
}

0 commit comments

Comments
 (0)