2121
2222namespace Mageplaza \Sitemap \Block ;
2323
24+ use Exception ;
2425use Magento \Catalog \Helper \Category ;
2526use Magento \Catalog \Model \CategoryRepository ;
2627use Magento \Catalog \Model \Product \Visibility as ProductVisibility ;
@@ -143,18 +144,17 @@ public function getProductCollection()
143144 ->addMinimalPrice ()
144145 ->addFinalPrice ()
145146 ->addTaxPercents ()
146- ->setPageSize ($ limit )
147147 ->addAttributeToSelect ('* ' );
148148
149- $ sortProductBy = $ this ->_helper ->getHtmlSitemapConfig ('product_sorting ' );
149+ $ sortProductBy = $ this ->_helper ->getHtmlSitemapConfig ('product_sorting ' );
150150 $ sortProductDir = $ this ->_helper ->getHtmlSitemapConfig ('product_sorting_dir ' );
151151
152152 switch ($ sortProductBy ) {
153153 case SortProduct::PRODUCT_NAME :
154154 $ collection ->setOrder ('name ' , $ sortProductDir );
155155 break ;
156156 case SortProduct::PRICE :
157- $ collection ->setOrder ('price ' , $ sortProductDir );
157+ $ collection ->setOrder ('minimal_price ' , $ sortProductDir );
158158 break ;
159159 default :
160160 $ collection ->setOrder ('entity_id ' , $ sortProductDir );
@@ -165,6 +165,8 @@ public function getProductCollection()
165165 $ this ->_stockFilter ->addInStockFilterToCollection ($ collection );
166166 }
167167
168+ $ collection ->setPageSize ($ limit );
169+
168170 return $ collection ;
169171 }
170172
@@ -180,21 +182,71 @@ public function getCategoryCollection()
180182 ->addFieldToFilter ('is_active ' , 1 )
181183 ->addFieldToFilter ('include_in_menu ' , 1 )
182184 ->addFieldToFilter ('entity_id ' , ['nin ' => [1 , 2 ]])->setOrder ('path ' );
183- $ excludeCategories = $ this ->_helper ->getHtmlSitemapConfig ('category_page ' );
185+
186+ $ excludeCategories = $ this ->_helper ->getHtmlSitemapConfig ('category_page ' );
184187 if (!empty ($ excludeCategories )) {
185188 $ excludeCategories = array_map ('trim ' , explode (
186189 "\n" ,
187190 $ excludeCategories
188191 ));
189192
190193 foreach ($ excludeCategories as $ excludeCategory ) {
191- $ categoryCollection ->addFieldToFilter ('url_path ' , ['nlike ' => $ excludeCategory . '% ' ]);
194+ try {
195+ $ testRegex = preg_match ($ excludeCategory , '' );
196+ if ($ testRegex ) {
197+ $ excludeCategoriesIds = $ this ->filterCategoryWithRegex ($ excludeCategory );
198+ if (count ($ excludeCategoriesIds )) {
199+ $ categoryCollection ->addFieldToFilter ('entiry_id ' , ['nin ' => $ excludeCategoriesIds ]);
200+ }
201+ }
202+ } catch (Exception $ e ) {
203+ $ excludePath = $ this ->getExcludePath ($ excludeCategory );
204+ $ categoryCollection ->addFieldToFilter ('url_path ' , ['nlike ' => '% ' . $ excludePath . '% ' ]);
205+ }
192206 }
193207 }
194208
195209 return $ categoryCollection ;
196210 }
197211
212+ /**
213+ * @param $regex
214+ *
215+ * @return array
216+ * @throws LocalizedException
217+ * @throws NoSuchEntityException
218+ */
219+ protected function filterCategoryWithRegex ($ regex )
220+ {
221+ $ excludeCategoriesIds = [];
222+ $ categoryCollection = $ this ->_categoryCollection ->create ()->addAttributeToSelect ('* ' )
223+ ->setStoreId ($ this ->_storeManager ->getStore ()->getId ());
224+ foreach ($ categoryCollection as $ category ) {
225+ if (!preg_match ($ regex , $ category ->getUrlPath ())) {
226+ $ excludeCategoriesIds [] = $ category ->getId ();
227+ }
228+ }
229+
230+ return $ excludeCategoriesIds ;
231+ }
232+
233+ /**
234+ * @param $excludeCategory
235+ *
236+ * @return string
237+ */
238+ protected function getExcludePath ($ excludeCategory )
239+ {
240+ if ($ excludeCategory [0 ] == '/ ' ) {
241+ $ excludeCategory = substr ($ excludeCategory , 1 );
242+ }
243+ if ($ excludeCategory [-1 ] == '/ ' ) {
244+ $ excludeCategory = substr ($ excludeCategory , 0 , -1 );
245+ }
246+
247+ return $ excludeCategory ;
248+ }
249+
198250 /**
199251 * @param int $categoryId
200252 *
0 commit comments