|
26 | 26 | use Magento\Catalog\Model\Product\Visibility as ProductVisibility; |
27 | 27 | use Magento\Catalog\Model\ResourceModel\Category\Collection; |
28 | 28 | 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; |
30 | 30 | use Magento\CatalogInventory\Helper\Stock; |
31 | 31 | use Magento\Cms\Model\Page; |
32 | 32 | 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; |
34 | 35 | use Magento\Framework\Exception\NoSuchEntityException; |
35 | 36 | use Magento\Framework\View\Element\Template; |
36 | 37 | use Magento\Framework\View\Element\Template\Context; |
37 | 38 | use Mageplaza\Sitemap\Helper\Data as HelperConfig; |
| 39 | +use Mageplaza\Sitemap\Model\Source\SortProduct; |
38 | 40 |
|
39 | 41 | /** |
40 | 42 | * Class Sitemap |
@@ -136,28 +138,61 @@ public function __construct( |
136 | 138 | public function getProductCollection() |
137 | 139 | { |
138 | 140 | $limit = $this->_helper->getProductLimit() ?: self::DEFAULT_PRODUCT_LIMIT; |
139 | | - $collection = $this->productCollection |
| 141 | + $collection = $this->productCollection->create() |
140 | 142 | ->setVisibility($this->productVisibility->getVisibleInCatalogIds()) |
141 | 143 | ->addMinimalPrice() |
142 | 144 | ->addFinalPrice() |
143 | 145 | ->addTaxPercents() |
144 | 146 | ->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('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 |
|
150 | 168 | return $collection; |
151 | 169 | } |
152 | 170 |
|
153 | 171 | /** |
154 | | - * Get category collection |
155 | | - * |
156 | | - * @return TreeCollection |
| 172 | + * @return Collection|AbstractDb |
| 173 | + * @throws NoSuchEntityException |
| 174 | + * @throws LocalizedException |
157 | 175 | */ |
158 | 176 | public function getCategoryCollection() |
159 | 177 | { |
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; |
161 | 196 | } |
162 | 197 |
|
163 | 198 | /** |
@@ -223,16 +258,15 @@ public function getAdditionLinksCollection() |
223 | 258 | } |
224 | 259 |
|
225 | 260 | /** |
226 | | - * Render link element |
227 | | - * |
228 | | - * @param string $link |
229 | | - * @param string $title |
| 261 | + * @param $link |
| 262 | + * @param $title |
| 263 | + * @param $level |
230 | 264 | * |
231 | 265 | * @return string |
232 | 266 | */ |
233 | | - public function renderLinkElement($link, $title) |
| 267 | + public function renderLinkElement($link, $title, $level = null) |
234 | 268 | { |
235 | | - return '<li><a href="' . $link . '">' . __($title) . '</a></li>'; |
| 269 | + return '<li><a class="level-' . $level . '" href="' . $link . '">' . __($title) . '</a></li>'; |
236 | 270 | } |
237 | 271 |
|
238 | 272 | // phpcs:disable Generic.Metrics.NestingLevel |
@@ -260,7 +294,8 @@ public function renderSection($section, $config, $title, $collection) |
260 | 294 | if (!$category->getData('mp_exclude_sitemap')) { |
261 | 295 | $html .= $this->renderLinkElement( |
262 | 296 | $this->getCategoryUrl($item->getId()), |
263 | | - $item->getName() |
| 297 | + $item->getName(), |
| 298 | + $item->getLevel() |
264 | 299 | ); |
265 | 300 | } |
266 | 301 | break; |
@@ -334,4 +369,12 @@ public function isEnableHtmlSitemap() |
334 | 369 | { |
335 | 370 | return $this->_helper->isEnableHtmlSiteMap(); |
336 | 371 | } |
| 372 | + |
| 373 | + /** |
| 374 | + * @return array|bool|mixed |
| 375 | + */ |
| 376 | + public function getCategoryDisplayType() |
| 377 | + { |
| 378 | + return $this->_helper->getHtmlSitemapConfig('display_type'); |
| 379 | + } |
337 | 380 | } |
0 commit comments