1919class 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 ;
0 commit comments