Skip to content

Commit b5bdaf7

Browse files
author
b__b
committed
12223 use product label for gallery only, and use data from listing for other produts on product page
1 parent e2063ac commit b5bdaf7

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

Model/Parser/Html.php

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
class Html
2020
{
2121
const COMMENT_PREFIX = '<!--mf_product_label_comment_';
22+
const COMMENT_PREFIX_GALLERY = '<!--mf_product_label_gallery_comment_';
2223

2324
const COMMENT_SUFFIX = '-->';
2425

@@ -95,8 +96,14 @@ public function __construct(
9596
public function execute(string $output): string
9697
{
9798
$isOutputIsJson = $this->json_validate($output);
98-
99+
99100
$productIds = $this->getProductIds($output);
101+
$currentPageProductId = $this->fan == 'catalog_product_view' ? $this->getCurrentPageProductId($output) : 0;
102+
103+
if ($currentPageProductId) {
104+
$productIds[] = $currentPageProductId;
105+
}
106+
100107
[$ruleIds, $productIdRuleIds] = $this->getProductIdsToRuleIdsMap->execute($productIds);
101108

102109
$rules = $this->ruleCollectionFactory->create()
@@ -108,7 +115,9 @@ public function execute(string $output): string
108115
$replaceMap = [];
109116

110117
foreach ($productIdRuleIds as $productId => $productRuleIds) {
111-
$productLabels = $this->getProductLabels($rules, $productRuleIds);
118+
$forProductPage = $currentPageProductId && $productId == $currentPageProductId;
119+
120+
$productLabels = $this->getProductLabels($rules, $productRuleIds, $forProductPage);
112121

113122
$htmlToReplace = $this->layout
114123
->createBlock(\Magefan\ProductLabel\Block\Label::class)
@@ -126,16 +135,17 @@ public function execute(string $output): string
126135

127136
foreach ($replaceMap as $productId => $replace) {
128137
$replace = $isOutputIsJson ? trim(json_encode($replace),'"') : $replace;
129-
$output = str_replace(self::COMMENT_PREFIX . $productId . self::COMMENT_SUFFIX, $replace, $output);
138+
139+
$output = ($currentPageProductId && $currentPageProductId == $productId)
140+
? str_replace(self::COMMENT_PREFIX_GALLERY . $productId . self::COMMENT_SUFFIX, $replace, $output)
141+
: str_replace(self::COMMENT_PREFIX . $productId . self::COMMENT_SUFFIX, $replace, $output);
130142
}
131143

132144
return $output;
133145
}
134146

135-
public function getProductLabels($rules, $productRuleIds): array
147+
public function getProductLabels($rules, $productRuleIds, $forProductPage = false): array
136148
{
137-
$forProductPage = $this->fan == 'catalog_product_view';
138-
139149
$productLabelsCount = 0;
140150
$productLabels = [];
141151

@@ -168,6 +178,27 @@ public function getProductLabels($rules, $productRuleIds): array
168178
return $productLabels;
169179
}
170180

181+
/**
182+
* @param string $html
183+
* @return int
184+
*/
185+
private function getCurrentPageProductId(string $html): int
186+
{
187+
$pattern = '/' . self::COMMENT_PREFIX_GALLERY . '(.*?)' . self::COMMENT_SUFFIX . '/';
188+
preg_match_all($pattern, $html, $matches);
189+
190+
191+
foreach ($matches[1] as $commentData) {
192+
$productId = (int)$commentData;
193+
194+
if ($productId) {
195+
return $productId;
196+
}
197+
}
198+
199+
return 0;
200+
}
201+
171202
/**
172203
* @param string $html
173204
* @return array
@@ -187,8 +218,8 @@ private function getProductIds(string $html): array
187218

188219
return $productIds;
189220
}
190-
191-
private function json_validate($json, $depth = 512, $flags = 0)
221+
222+
private function json_validate($json, $depth = 512, $flags = 0)
192223
{
193224
if (!is_string($json)) {
194225
return false;
@@ -199,7 +230,7 @@ private function json_validate($json, $depth = 512, $flags = 0)
199230
if ($trimmedJson[0] !== '{' && $trimmedJson[0] !== '[') {
200231
return false;
201232
}
202-
233+
203234
// Decode JSON and check for errors
204235
json_decode($json, false, $depth, $flags);
205236
return json_last_error() === JSON_ERROR_NONE;

Plugin/Frontend/Magento/Catalog/Block/Product/View/Gallery.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function __construct(
4444
public function afterToHtml($subject, $result)
4545
{
4646
if ($this->config->isEnabled() && ($product = $subject->getProduct())) {
47-
$mfPlComment = Html::COMMENT_PREFIX . $product->getId() . Html::COMMENT_SUFFIX;
47+
$mfPlComment = Html::COMMENT_PREFIX_GALLERY . $product->getId() . Html::COMMENT_SUFFIX;
4848

4949
if ($this->hyvaThemeDetection->execute()) {
5050
$result = $this->addMfLabelContainerToImageWrapperTag($result, (int)$product->getId());
@@ -76,7 +76,7 @@ private function addMfLabelContainerToImageWrapperTag(string $html, int $product
7676
$endOfTagWithWrapperClassPosition = strpos($html, '>', $wrapperClassPosition);
7777

7878
if ($endOfTagWithWrapperClassPosition !== false) {
79-
$mfPlContainer = Html::COMMENT_PREFIX . $productId . Html::COMMENT_SUFFIX;
79+
$mfPlContainer = Html::COMMENT_PREFIX_GALLERY . $productId . Html::COMMENT_SUFFIX;
8080

8181
$html = substr_replace($html, $mfPlContainer, $endOfTagWithWrapperClassPosition + 1, 0);
8282
}

0 commit comments

Comments
 (0)