|
21 | 21 |
|
22 | 22 | namespace Mageplaza\Sitemap\Block;
|
23 | 23 |
|
| 24 | +use Exception; |
24 | 25 | use Magento\Catalog\Helper\Category;
|
25 | 26 | use Magento\Catalog\Model\CategoryRepository;
|
26 | 27 | use Magento\Catalog\Model\Product\Visibility as ProductVisibility;
|
27 | 28 | use Magento\Catalog\Model\ResourceModel\Category\Collection;
|
28 | 29 | use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
|
29 |
| -use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection; |
| 30 | +use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollection; |
30 | 31 | use Magento\CatalogInventory\Helper\Stock;
|
31 | 32 | use Magento\Cms\Model\Page;
|
32 | 33 | use Magento\Cms\Model\ResourceModel\Page\Collection as PageCollection;
|
33 |
| -use Magento\Framework\Data\Tree\Node\Collection as TreeCollection; |
| 34 | +use Magento\Framework\Data\Collection\AbstractDb; |
| 35 | +use Magento\Framework\Exception\LocalizedException; |
34 | 36 | use Magento\Framework\Exception\NoSuchEntityException;
|
35 | 37 | use Magento\Framework\View\Element\Template;
|
36 | 38 | use Magento\Framework\View\Element\Template\Context;
|
37 | 39 | use Mageplaza\Sitemap\Helper\Data as HelperConfig;
|
| 40 | +use Mageplaza\Sitemap\Model\Source\SortProduct; |
38 | 41 |
|
39 | 42 | /**
|
40 | 43 | * Class Sitemap
|
@@ -136,28 +139,160 @@ public function __construct(
|
136 | 139 | public function getProductCollection()
|
137 | 140 | {
|
138 | 141 | $limit = $this->_helper->getProductLimit() ?: self::DEFAULT_PRODUCT_LIMIT;
|
139 |
| - $collection = $this->productCollection |
| 142 | + $collection = $this->productCollection->create() |
140 | 143 | ->setVisibility($this->productVisibility->getVisibleInCatalogIds())
|
141 | 144 | ->addMinimalPrice()
|
142 | 145 | ->addFinalPrice()
|
143 | 146 | ->addTaxPercents()
|
144 |
| - ->setPageSize($limit) |
145 | 147 | ->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('minimal_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')) { |
147 | 165 | $this->_stockFilter->addInStockFilterToCollection($collection);
|
148 | 166 | }
|
149 | 167 |
|
| 168 | + $collection->setPageSize($limit); |
| 169 | + |
150 | 170 | return $collection;
|
151 | 171 | }
|
152 | 172 |
|
153 | 173 | /**
|
154 |
| - * Get category collection |
155 |
| - * |
156 |
| - * @return TreeCollection |
| 174 | + * @return Collection|AbstractDb |
| 175 | + * @throws NoSuchEntityException |
| 176 | + * @throws LocalizedException |
157 | 177 | */
|
158 | 178 | public function getCategoryCollection()
|
159 | 179 | {
|
160 |
| - return $this->_categoryHelper->getStoreCategories(false, true); |
| 180 | + $storeRootCategoryId = $this->_storeManager->getStore()->getRootCategoryId(); |
| 181 | + $storeRootCategory = $this->categoryRepository->get($storeRootCategoryId); |
| 182 | + $categoryCollection = $this->_categoryCollection->create()->addAttributeToSelect('*') |
| 183 | + ->addFieldToFilter('entity_id', ['in' => $storeRootCategory->getAllChildren(true)]) |
| 184 | + ->addFieldToFilter('is_active', 1) |
| 185 | + ->addFieldToFilter('include_in_menu', 1) |
| 186 | + ->addFieldToFilter('entity_id', ['nin' => [$storeRootCategoryId]]); |
| 187 | + |
| 188 | + $excludeCategories = $this->_helper->getHtmlSitemapConfig('category_page'); |
| 189 | + if (!empty($excludeCategories)) { |
| 190 | + $excludeCategories = array_map('trim', explode( |
| 191 | + "\n", |
| 192 | + $excludeCategories |
| 193 | + )); |
| 194 | + |
| 195 | + $allExcludeIds = ''; |
| 196 | + foreach ($excludeCategories as $excludeCategory) { |
| 197 | + if (!empty($excludeCategory)) { |
| 198 | + try { |
| 199 | + $testRegex = preg_match($excludeCategory, ''); |
| 200 | + if ($testRegex) { |
| 201 | + $allExcludeIds .= '-' . $this->filterCategoryWithRegex($excludeCategory); |
| 202 | + } else { |
| 203 | + $excludePath = $this->getExcludePath($excludeCategory); |
| 204 | + $allExcludeIds .= '-' . $this->filterCategoryWithPath($excludePath, $categoryCollection); |
| 205 | + } |
| 206 | + } catch (Exception $e) { |
| 207 | + $excludePath = $this->getExcludePath($excludeCategory); |
| 208 | + $allExcludeIds .= '-' . $this->filterCategoryWithPath($excludePath, $categoryCollection); |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + $excludeIds = explode('-', $allExcludeIds); |
| 214 | + $categoryCollection->addFieldToFilter('entity_id', ['nin' => $excludeIds]); |
| 215 | + } |
| 216 | + |
| 217 | + return $this->_categoryCollection->create()->addAttributeToSelect('*') |
| 218 | + ->addFieldToFilter('entity_id', ['in' => $categoryCollection->getAllIds()])->setOrder('path'); |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * @param $path |
| 223 | + * @param $categoryCollection |
| 224 | + * |
| 225 | + * @return string |
| 226 | + */ |
| 227 | + protected function filterCategoryWithPath($path, $categoryCollection) |
| 228 | + { |
| 229 | + $excludeIds = []; |
| 230 | + foreach ($categoryCollection as $category) { |
| 231 | + if ($this->isExcludeCategory($category, $path)) { |
| 232 | + $excludeIds[] = $category->getData('entity_id'); |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + return implode('-', $excludeIds); |
| 237 | + } |
| 238 | + |
| 239 | + /** |
| 240 | + * @param $category |
| 241 | + * @param $path |
| 242 | + * |
| 243 | + * @return bool |
| 244 | + */ |
| 245 | + public function isExcludeCategory($category, $path) |
| 246 | + { |
| 247 | + $filterPath = explode('/', $path); |
| 248 | + $categoryPath = $category->getUrlPath(); |
| 249 | + $categoryPath = explode('/', $categoryPath); |
| 250 | + |
| 251 | + foreach ($filterPath as $pathInfo) { |
| 252 | + if (!in_array($pathInfo, $categoryPath)) { |
| 253 | + return false; |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + return true; |
| 258 | + } |
| 259 | + |
| 260 | + /** |
| 261 | + * @param $regex |
| 262 | + * |
| 263 | + * @return string |
| 264 | + * @throws LocalizedException |
| 265 | + * @throws NoSuchEntityException |
| 266 | + */ |
| 267 | + protected function filterCategoryWithRegex($regex) |
| 268 | + { |
| 269 | + $excludeCategoriesIds = []; |
| 270 | + $categoryCollection = $this->_categoryCollection->create()->addAttributeToSelect('*') |
| 271 | + ->setStoreId($this->_storeManager->getStore()->getId()); |
| 272 | + foreach ($categoryCollection as $category) { |
| 273 | + if (!preg_match($regex, $category->getUrlPath())) { |
| 274 | + $excludeCategoriesIds[] = $category->getId(); |
| 275 | + } |
| 276 | + } |
| 277 | + |
| 278 | + return implode('-', $excludeCategoriesIds); |
| 279 | + } |
| 280 | + |
| 281 | + /** |
| 282 | + * @param $excludeCategory |
| 283 | + * |
| 284 | + * @return string |
| 285 | + */ |
| 286 | + protected function getExcludePath($excludeCategory) |
| 287 | + { |
| 288 | + if ($excludeCategory[0] == '/') { |
| 289 | + $excludeCategory = substr($excludeCategory, 1); |
| 290 | + } |
| 291 | + if ($excludeCategory[-1] == '/') { |
| 292 | + $excludeCategory = substr($excludeCategory, 0, -1); |
| 293 | + } |
| 294 | + |
| 295 | + return $excludeCategory; |
161 | 296 | }
|
162 | 297 |
|
163 | 298 | /**
|
@@ -223,16 +358,15 @@ public function getAdditionLinksCollection()
|
223 | 358 | }
|
224 | 359 |
|
225 | 360 | /**
|
226 |
| - * Render link element |
227 |
| - * |
228 |
| - * @param string $link |
229 |
| - * @param string $title |
| 361 | + * @param $link |
| 362 | + * @param $title |
| 363 | + * @param $level |
230 | 364 | *
|
231 | 365 | * @return string
|
232 | 366 | */
|
233 |
| - public function renderLinkElement($link, $title) |
| 367 | + public function renderLinkElement($link, $title, $level = null) |
234 | 368 | {
|
235 |
| - return '<li><a href="' . $link . '">' . __($title) . '</a></li>'; |
| 369 | + return '<li><a class="level-' . $level . '" href="' . $link . '">' . __($title) . '</a></li>'; |
236 | 370 | }
|
237 | 371 |
|
238 | 372 | // phpcs:disable Generic.Metrics.NestingLevel
|
@@ -260,7 +394,8 @@ public function renderSection($section, $config, $title, $collection)
|
260 | 394 | if (!$category->getData('mp_exclude_sitemap')) {
|
261 | 395 | $html .= $this->renderLinkElement(
|
262 | 396 | $this->getCategoryUrl($item->getId()),
|
263 |
| - $item->getName() |
| 397 | + $item->getName(), |
| 398 | + $item->getLevel() |
264 | 399 | );
|
265 | 400 | }
|
266 | 401 | break;
|
@@ -334,4 +469,12 @@ public function isEnableHtmlSitemap()
|
334 | 469 | {
|
335 | 470 | return $this->_helper->isEnableHtmlSiteMap();
|
336 | 471 | }
|
| 472 | + |
| 473 | + /** |
| 474 | + * @return array|bool|mixed |
| 475 | + */ |
| 476 | + public function getCategoryDisplayType() |
| 477 | + { |
| 478 | + return $this->_helper->getHtmlSitemapConfig('display_type'); |
| 479 | + } |
337 | 480 | }
|
0 commit comments