Skip to content

Commit 5569413

Browse files
author
b__b
committed
12273 realized replace logic for child items
1 parent 1b92aca commit 5569413

File tree

5 files changed

+100
-17
lines changed

5 files changed

+100
-17
lines changed

Controller/Label/Get.php

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Magento\Framework\View\Result\PageFactory;
1313
use Magento\Catalog\Model\Product;
1414
use Magefan\ProductLabel\Model\GetLabels;
15+
use Magento\Catalog\Api\ProductRepositoryInterface;
16+
use Magento\Catalog\Model\Product\Attribute\Source\Status;
1517

1618
class Get extends \Magento\Framework\App\Action\Action
1719
{
@@ -25,27 +27,46 @@ class Get extends \Magento\Framework\App\Action\Action
2527
*/
2628
protected $getLabels;
2729

30+
/**
31+
* @var ProductRepositoryInterface
32+
*/
33+
protected $productRepository;
34+
2835
/**
2936
* Get constructor.
3037
* @param Context $context
3138
* @param JsonFactory $jsonResultFactory
32-
* @param PageFactory $resultPageFactory
3339
* @param GetLabels $getLabels
40+
* @param ProductRepositoryInterface $productRepository
3441
*/
3542
public function __construct(
3643
Context $context,
3744
JsonFactory $jsonResultFactory,
38-
GetLabels $getLabels
45+
GetLabels $getLabels,
46+
ProductRepositoryInterface $productRepository
3947
) {
4048
parent::__construct($context);
4149
$this->jsonResultFactory = $jsonResultFactory;
4250
$this->getLabels = $getLabels;
51+
$this->productRepository = $productRepository;
4352
}
4453

4554
public function execute()
4655
{
4756
$result = $this->jsonResultFactory->create();
48-
$replaceMap = $this->getLabels->execute($this->getProductIds(), $this->getProductIdsForProductPage());
57+
58+
$productIds = $this->getProductIds();
59+
$productIdsForProductPage = $this->getProductIdsForProductPage();
60+
61+
if ($this->getRequest()->getParam('get_children') && isset($this->getProductIds()[0])) {
62+
$productIds = $this->getChildProductIds($this->getProductIds()[0]);
63+
64+
if ($this->getRequest()->getParam('product_page')) {
65+
$productIdsForProductPage = $productIds;
66+
}
67+
}
68+
69+
$replaceMap = $this->getLabels->execute($productIds, $productIdsForProductPage);
4970

5071
$result->setData([
5172
'labels' => $replaceMap
@@ -98,4 +119,29 @@ private function getProductIdsForProductPage(): array
98119
{
99120
return $this->getProductIdsFromRequest('product_ids_for_product_page');
100121
}
122+
123+
/**
124+
* @param int $parentProductId
125+
* @return array
126+
*/
127+
private function getChildProductIds(int $parentProductId): array
128+
{
129+
$childProductIds = [];
130+
131+
$currentProduct = $this->productRepository->getById($parentProductId);
132+
133+
if ('configurable' != $currentProduct->getTypeId()) {
134+
return $childProductIds;
135+
}
136+
137+
$childProducts = $currentProduct->getTypeInstance()->getUsedProducts($currentProduct, null);
138+
139+
foreach ($childProducts as $childProduct) {
140+
if ((int) $childProduct->getStatus() === Status::STATUS_ENABLED) {
141+
$childProductIds[] = $childProduct->getId();
142+
}
143+
}
144+
145+
return $childProductIds;
146+
}
101147
}

Model/GetLabels.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public function execute(array $productIds, $productIdsForProductPage = []): arra
9595
$htmlToReplace = $this->layout
9696
->createBlock(\Magefan\ProductLabel\Block\Label::class)
9797
->setProductLabels($productLabels)
98+
->setAdditionalCssClass($forProductPage ? 'mfpl-product-page' : '')
9899
->toHtml();
99100

100101
if ($htmlToReplace) {

view/frontend/layout/default.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
<body>
1010
<referenceContainer name="after.body.start">
1111
<block class="Magefan\ProductLabel\Block\LabelCss" name="mf.label.css" template="Magefan_ProductLabel::label-css.phtml"/>
12+
13+
<block class="Magefan\Community\Block\JsScript" name="mf.ajax" template="Magefan_Community::js/ajax.phtml" />
1214
</referenceContainer>
1315
</body>
1416
</page>

view/frontend/templates/label-container.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
?>
1212

1313
<?php if ($productLabels) { ?>
14-
<div class="mf-label-container">
14+
<div class="mf-label-container <?= $block->getAdditionalCssClass() ?>">
1515
<?php foreach ($productLabels as $position => $labels) { ?>
1616
<div class="mf-label mf-label-position-<?= $escaper->escapeHtml($position) ?>">
1717
<?php foreach ($labels as $label) { ?>

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

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ define([
1111

1212
return function (SwatchRenderer) {
1313
$.widget('mage.SwatchRenderer', SwatchRenderer, {
14-
1514
_OnClick: function ($this, $widget) {
1615
this._super($this, $widget);
1716
this.dispatchItemSelect();
@@ -23,24 +22,59 @@ define([
2322
},
2423

2524
dispatchItemSelect: function () {
26-
console.log('---------- ' )
27-
console.log('mainProductId ' + this.options.jsonConfig.productId)
28-
console.log('selectedProductId ' + this.getProductId())
25+
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+
2939

30-
let previusActiveEl = document.querySelector('.mf-label-container.mf-pl-child.active');
31-
let newActiveEl = document.querySelector('.mf-label-container.mf-pl-child.mf-pl-' + this.getProductId());
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');
3246

33-
if (previusActiveEl) {
34-
previusActiveEl.classList.remove('active');
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)
53+
}
54+
});
55+
} else {
56+
self.replaceLabel(maintProductID, selectedProductId)
3557
}
58+
},
3659

37-
if (newActiveEl) {
38-
newActiveEl.classList.add('active');
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');
3968
}
4069

41-
$(document).trigger('mfChildItemSelected', {
42-
mainProductId: this.options.jsonConfig.productId,
43-
selectedProductId: this.getProductId()});
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);
77+
}
4478
}
4579
});
4680

0 commit comments

Comments
 (0)