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

Commit 6b66b33

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

File tree

7 files changed

+510
-203
lines changed

7 files changed

+510
-203
lines changed

Block/Sitemap.php

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,17 @@
2626
use Magento\Catalog\Model\Product\Visibility as ProductVisibility;
2727
use Magento\Catalog\Model\ResourceModel\Category\Collection;
2828
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
29-
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
29+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollection;
3030
use Magento\CatalogInventory\Helper\Stock;
3131
use Magento\Cms\Model\Page;
3232
use Magento\Cms\Model\ResourceModel\Page\Collection as PageCollection;
33-
use Magento\Framework\Data\Tree\Node\Collection as TreeCollection;
33+
use Magento\Framework\Data\Collection\AbstractDb;
34+
use Magento\Framework\Exception\LocalizedException;
3435
use Magento\Framework\Exception\NoSuchEntityException;
3536
use Magento\Framework\View\Element\Template;
3637
use Magento\Framework\View\Element\Template\Context;
3738
use Mageplaza\Sitemap\Helper\Data as HelperConfig;
39+
use Mageplaza\Sitemap\Model\Source\SortProduct;
3840

3941
/**
4042
* Class Sitemap
@@ -136,28 +138,61 @@ public function __construct(
136138
public function getProductCollection()
137139
{
138140
$limit = $this->_helper->getProductLimit() ?: self::DEFAULT_PRODUCT_LIMIT;
139-
$collection = $this->productCollection
141+
$collection = $this->productCollection->create()
140142
->setVisibility($this->productVisibility->getVisibleInCatalogIds())
141143
->addMinimalPrice()
142144
->addFinalPrice()
143145
->addTaxPercents()
144146
->setPageSize($limit)
145147
->addAttributeToSelect('*');
146-
if (!$this->_helper->getConfigValue('cataloginventory/options/show_out_of_stock')) {
148+
149+
$sortProductBy = $this->_helper->getHtmlSitemapConfig('product_sorting');
150+
$sortProductDir = $this->_helper->getHtmlSitemapConfig('product_sorting_dir');
151+
152+
switch ($sortProductBy) {
153+
case SortProduct::PRODUCT_NAME:
154+
$collection->setOrder('name', $sortProductDir);
155+
break;
156+
case SortProduct::PRICE:
157+
$collection->setOrder('price', $sortProductDir);
158+
break;
159+
default:
160+
$collection->setOrder('entity_id', $sortProductDir);
161+
break;
162+
}
163+
164+
if (!$this->_helper->getHtmlSitemapConfig('out_of_stock_products')) {
147165
$this->_stockFilter->addInStockFilterToCollection($collection);
148166
}
149167

150168
return $collection;
151169
}
152170

153171
/**
154-
* Get category collection
155-
*
156-
* @return TreeCollection
172+
* @return Collection|AbstractDb
173+
* @throws NoSuchEntityException
174+
* @throws LocalizedException
157175
*/
158176
public function getCategoryCollection()
159177
{
160-
return $this->_categoryHelper->getStoreCategories(false, true);
178+
$categoryCollection = $this->_categoryCollection->create()->addAttributeToSelect('*')
179+
->setStoreId($this->_storeManager->getStore()->getId())
180+
->addFieldToFilter('is_active', 1)
181+
->addFieldToFilter('include_in_menu', 1)
182+
->addFieldToFilter('entity_id', ['nin' => [1, 2]])->setOrder('path');
183+
$excludeCategories = $this->_helper->getHtmlSitemapConfig('category_page');
184+
if (!empty($excludeCategories)) {
185+
$excludeCategories = array_map('trim', explode(
186+
"\n",
187+
$excludeCategories
188+
));
189+
190+
foreach ($excludeCategories as $excludeCategory) {
191+
$categoryCollection->addFieldToFilter('url_path', ['nlike' => $excludeCategory . '%']);
192+
}
193+
}
194+
195+
return $categoryCollection;
161196
}
162197

163198
/**
@@ -223,16 +258,15 @@ public function getAdditionLinksCollection()
223258
}
224259

225260
/**
226-
* Render link element
227-
*
228-
* @param string $link
229-
* @param string $title
261+
* @param $link
262+
* @param $title
263+
* @param $level
230264
*
231265
* @return string
232266
*/
233-
public function renderLinkElement($link, $title)
267+
public function renderLinkElement($link, $title, $level = null)
234268
{
235-
return '<li><a href="' . $link . '">' . __($title) . '</a></li>';
269+
return '<li><a class="level-' . $level . '" href="' . $link . '">' . __($title) . '</a></li>';
236270
}
237271

238272
// phpcs:disable Generic.Metrics.NestingLevel
@@ -260,7 +294,8 @@ public function renderSection($section, $config, $title, $collection)
260294
if (!$category->getData('mp_exclude_sitemap')) {
261295
$html .= $this->renderLinkElement(
262296
$this->getCategoryUrl($item->getId()),
263-
$item->getName()
297+
$item->getName(),
298+
$item->getLevel()
264299
);
265300
}
266301
break;
@@ -334,4 +369,12 @@ public function isEnableHtmlSitemap()
334369
{
335370
return $this->_helper->isEnableHtmlSiteMap();
336371
}
372+
373+
/**
374+
* @return array|bool|mixed
375+
*/
376+
public function getCategoryDisplayType()
377+
{
378+
return $this->_helper->getHtmlSitemapConfig('display_type');
379+
}
337380
}

Model/Source/DisplayType.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Mageplaza
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Mageplaza.com license that is
8+
* available through the world-wide-web at this URL:
9+
* https://www.mageplaza.com/LICENSE.txt
10+
*
11+
* DISCLAIMER
12+
*
13+
* Do not edit or add to this file if you wish to upgrade this extension to newer
14+
* version in the future.
15+
*
16+
* @category Mageplaza
17+
* @package Mageplaza_Sitemap
18+
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
19+
* @license https://www.mageplaza.com/LICENSE.txt
20+
*/
21+
22+
namespace Mageplaza\Sitemap\Model\Source;
23+
24+
use Magento\Framework\Option\ArrayInterface;
25+
26+
/**
27+
* Class DisplayType
28+
* @package Mageplaza\Sitemap\Model\Config\Source
29+
*/
30+
class DisplayType implements ArrayInterface
31+
{
32+
const LIST = 'list';
33+
const DROPDOWN = 'dropdown';
34+
35+
/**
36+
* @return array
37+
*/
38+
public function toOptionArray()
39+
{
40+
$options = [];
41+
foreach ($this->toArray() as $value => $label) {
42+
$options[] = [
43+
'value' => $value,
44+
'label' => $label
45+
];
46+
}
47+
48+
return $options;
49+
}
50+
51+
/**
52+
* Get options in "key-value" format
53+
*
54+
* @return array
55+
*/
56+
public function toArray()
57+
{
58+
return [
59+
self::LIST => __('List'),
60+
self::DROPDOWN => __('Dropdown'),
61+
];
62+
}
63+
}

Model/Source/SortDirection.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Mageplaza
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Mageplaza.com license that is
8+
* available through the world-wide-web at this URL:
9+
* https://www.mageplaza.com/LICENSE.txt
10+
*
11+
* DISCLAIMER
12+
*
13+
* Do not edit or add to this file if you wish to upgrade this extension to newer
14+
* version in the future.
15+
*
16+
* @category Mageplaza
17+
* @package Mageplaza_Sitemap
18+
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
19+
* @license https://www.mageplaza.com/LICENSE.txt
20+
*/
21+
22+
namespace Mageplaza\Sitemap\Model\Source;
23+
24+
use Magento\Framework\Option\ArrayInterface;
25+
26+
/**
27+
* Class SortDirection
28+
* @package Mageplaza\Sitemap\Model\Config\Source
29+
*/
30+
class SortDirection implements ArrayInterface
31+
{
32+
const ASC = 'asc';
33+
const DESC = 'desc';
34+
35+
/**
36+
* @return array
37+
*/
38+
public function toOptionArray()
39+
{
40+
$options = [];
41+
foreach ($this->toArray() as $value => $label) {
42+
$options[] = [
43+
'value' => $value,
44+
'label' => $label
45+
];
46+
}
47+
48+
return $options;
49+
}
50+
51+
/**
52+
* Get options in "key-value" format
53+
*
54+
* @return array
55+
*/
56+
public function toArray()
57+
{
58+
return [
59+
self::ASC => __('Ascending'),
60+
self::DESC => __('Descending'),
61+
];
62+
}
63+
}

Model/Source/SortProduct.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* Mageplaza
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Mageplaza.com license that is
8+
* available through the world-wide-web at this URL:
9+
* https://www.mageplaza.com/LICENSE.txt
10+
*
11+
* DISCLAIMER
12+
*
13+
* Do not edit or add to this file if you wish to upgrade this extension to newer
14+
* version in the future.
15+
*
16+
* @category Mageplaza
17+
* @package Mageplaza_Sitemap
18+
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
19+
* @license https://www.mageplaza.com/LICENSE.txt
20+
*/
21+
22+
namespace Mageplaza\Sitemap\Model\Source;
23+
24+
use Magento\Framework\Option\ArrayInterface;
25+
26+
/**
27+
* Class SortProduct
28+
* @package Mageplaza\Sitemap\Model\Config\Source
29+
*/
30+
class SortProduct implements ArrayInterface
31+
{
32+
const PRODUCT_IDS = 'product_ids';
33+
const PRODUCT_NAME = 'product_name';
34+
const PRICE = 'price';
35+
36+
/**
37+
* @return array
38+
*/
39+
public function toOptionArray()
40+
{
41+
$options = [];
42+
foreach ($this->toArray() as $value => $label) {
43+
$options[] = [
44+
'value' => $value,
45+
'label' => $label
46+
];
47+
}
48+
49+
return $options;
50+
}
51+
52+
/**
53+
* Get options in "key-value" format
54+
*
55+
* @return array
56+
*/
57+
public function toArray()
58+
{
59+
return [
60+
self::PRODUCT_IDS => __('Product IDs'),
61+
self::PRODUCT_NAME => __('Product Name'),
62+
self::PRICE => __('Price')
63+
];
64+
}
65+
}

0 commit comments

Comments
 (0)