99namespace Magefan \ProductLabel \Model \Parser ;
1010
1111use Magefan \ProductLabel \Model \GetLabels ;
12+ use Magefan \ProductLabel \Model \Config ;
1213
1314class Html
1415{
1516 const COMMENT_PREFIX = '<!--mf_product_label_comment_ ' ;
1617 const COMMENT_PREFIX_GALLERY = '<!--mf_product_label_gallery_comment_ ' ;
1718 const COMMENT_SUFFIX = '--> ' ;
1819
20+
1921 /**
2022 * @var GetLabels
2123 */
2224 protected $ getLabels ;
25+ protected $ mapProductToCustomPosition = [];
2326
2427 public function __construct (
2528 GetLabels $ getLabels
@@ -35,8 +38,8 @@ public function __construct(
3538 public function execute (string $ output ): string
3639 {
3740 $ isOutputIsJson = $ this ->json_validate ($ output );
38-
3941 $ productIds = $ this ->getProductIds ($ output );
42+
4043 $ currentPageProductId = $ this ->getCurrentPageProductId ($ output );
4144 $ productIdsForProductPage = [];
4245
@@ -50,6 +53,9 @@ public function execute(string $output): string
5053 foreach ($ replaceMap as $ productId => $ replace ) {
5154 $ replace = $ isOutputIsJson ? trim (json_encode ($ replace ),'" ' ) : $ replace ;
5255
56+ // should be above regular replace
57+ $ this ->replaceForCustomPosition ($ output , $ replace , $ productId );
58+
5359 $ output = ($ currentPageProductId && $ currentPageProductId == $ productId )
5460 ? str_replace (self ::COMMENT_PREFIX_GALLERY . $ productId . self ::COMMENT_SUFFIX , $ replace , $ output )
5561 : str_replace (self ::COMMENT_PREFIX . $ productId . self ::COMMENT_SUFFIX , $ replace , $ output );
@@ -58,6 +64,36 @@ public function execute(string $output): string
5864 return $ output ;
5965 }
6066
67+ /**
68+ * @param string $output
69+ * @param string $replace
70+ * @param $productId
71+ * @return void
72+ */
73+ private function replaceForCustomPosition (string &$ output , string &$ replace , $ productId )
74+ {
75+ $ customPositions = $ this ->mapProductToCustomPosition [$ productId ] ?? [];
76+
77+ if (strpos ($ replace , Config::SPLITTERS_FOR_CUSTOM_POSITIONS ) !== false ) {
78+ if ($ customPositions ) {
79+ $ customPositionsLabels = explode (Config::SPLITTERS_FOR_CUSTOM_POSITIONS , $ replace );
80+ $ replace = $ customPositionsLabels [0 ];
81+ unset($ customPositionsLabels [0 ]);
82+
83+ foreach ($ customPositionsLabels as $ label ) {
84+ foreach ($ customPositions as $ customPosition ) {
85+ if (strpos ($ label , $ customPosition ) !== false ) {
86+ $ output = str_replace (self ::COMMENT_PREFIX . $ productId . '____ ' . $ customPosition . self ::COMMENT_SUFFIX , $ label , $ output );
87+ }
88+ }
89+ }
90+ } else {
91+ // leave only labels with regular positions
92+ $ replace = explode (Config::SPLITTERS_FOR_CUSTOM_POSITIONS , $ replace );
93+ $ replace = $ replace [0 ];
94+ }
95+ }
96+ }
6197
6298 /**
6399 * @param string $html
@@ -68,7 +104,6 @@ private function getCurrentPageProductId(string $html): int
68104 $ pattern = '/ ' . self ::COMMENT_PREFIX_GALLERY . '(.*?) ' . self ::COMMENT_SUFFIX . '/ ' ;
69105 preg_match_all ($ pattern , $ html , $ matches );
70106
71-
72107 foreach ($ matches [1 ] as $ commentData ) {
73108 $ productId = (int )$ commentData ;
74109
@@ -91,9 +126,18 @@ private function getProductIds(string $html): array
91126 $ productIds = [];
92127
93128 foreach ($ matches [1 ] as $ commentData ) {
94- $ productId = (int )$ commentData ; //for now commentData=productId
129+ /* $commentData = '3____product_list' | '3'*/
130+
131+ if (is_numeric ($ commentData )) {
132+ $ productId = (int )$ commentData ;
133+ } else {
134+ [$ productId , $ customPositionName ] = explode ('____ ' , $ commentData );
135+ $ productId = (int )$ productId ;
136+ $ this ->mapProductToCustomPosition [$ productId ][$ customPositionName ] = $ customPositionName ;
137+ }
138+
95139 if ($ productId ) {
96- $ productIds [] = $ productId ;
140+ $ productIds [$ productId ] = $ productId ;
97141 }
98142 }
99143
0 commit comments