Skip to content
This repository was archived by the owner on Feb 8, 2023. It is now read-only.

Commit 6cdb90d

Browse files
author
giaphn
committed
- Update HTML Sitemap
1 parent 6b66b33 commit 6cdb90d

File tree

3 files changed

+63
-10
lines changed

3 files changed

+63
-10
lines changed

Block/Sitemap.php

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
namespace Mageplaza\Sitemap\Block;
2323

24+
use Exception;
2425
use Magento\Catalog\Helper\Category;
2526
use Magento\Catalog\Model\CategoryRepository;
2627
use 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
*

etc/adminhtml/system.xml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<label>Exclude Category Page</label>
4242
<comment>
4343
<![CDATA[
44-
Display Category Page pages in HTML SiteMap except those with the above string selected. Separated by a line break.
44+
Display Category Pages in HTML Sitemap except those with the above selected string. Separated by a line break.
4545
E.g: Do not display with the category pages including /men/ such as:
4646
/men/top
4747
/men/top/t-shirt
@@ -57,6 +57,7 @@
5757
<field id="display_type" translate="label" type="select" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
5858
<label>Display Style</label>
5959
<source_model>Mageplaza\Sitemap\Model\Source\DisplayType</source_model>
60+
<comment>Choose style to display categories in HTML Sitemap.</comment>
6061
<depends>
6162
<field id="enable">1</field>
6263
<field id="category">1</field>
@@ -73,23 +74,23 @@
7374
<label>Sort Products in HTML Sitemap by</label>
7475
<source_model>Mageplaza\Sitemap\Model\Source\SortProduct</source_model>
7576
<depends>
76-
<field id="enable">1</field>
77+
<field id="product">1</field>
7778
</depends>
7879
<comment>Choose how to arrange and display products in HTML Site Map.</comment>
7980
</field>
8081
<field id="product_sorting_dir" translate="label" type="select" sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
8182
<label>Default Sort Direction</label>
8283
<source_model>Mageplaza\Sitemap\Model\Source\SortDirection</source_model>
8384
<depends>
84-
<field id="enable">1</field>
85+
<field id="product">1</field>
8586
</depends>
8687
</field>
8788
<field id="out_of_stock_products" translate="label" type="select" sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
8889
<label>Hide Out Of Stock Products</label>
8990
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
9091
<comment>If yes, will hide out of stock products in HTML Site Map.</comment>
9192
<depends>
92-
<field id="enable">1</field>
93+
<field id="product">1</field>
9394
</depends>
9495
</field>
9596
<field id="product_limit" translate="label comment" type="text" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">

view/frontend/templates/sitemap.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use Mageplaza\Sitemap\Block\Sitemap;
3838
var elementClass = $(this).attr('class'),
3939
level, margin;
4040
if (elementClass.trim()) {
41-
level = (elementClass.slice(-1) - 1) * 5;
41+
level = (elementClass.slice(-1) - 2) * 5;
4242
margin = level + '%';
4343
$(this).css('margin-left', margin);
4444
}

0 commit comments

Comments
 (0)