How to exclude specific Products, Categories and CMS Pages from sitemap.xml generation in Magento 2?
Domanda
I need to create a custom module that allows excluding/skipping the Products, Categories and CMS Pages URLs from generating in sitemap.xml. I had tried this solution for Magento 1, How could I do it in Magento 2?
Soluzione
Answer given a base on Magento 2.3.1 Opensource
There are 3 files, the Sitemap related data of Products, Categories, and CMS Pages is coming
Magento\Sitemap\Model\ResourceModel\Catalog\Category:getCollection()
Magento\Sitemap\Model\ResourceModel\Catalog\Product:getCollection()
Magento\Sitemap\Model\ResourceModel\Cms\Page:getCollection()
So, you want to skip any product/category/page from sitemap.xml you have to work on that related class and its method.
So, create plugin on required classes and changes the out of that required getCollection()
method.
Or You can work on
Or you can work om Magento\Sitemap\Model\ItemProvider\Composite
argument list
See di.xml
of vendor/magento/module-sitemap/etc/di.xml
. On below code
<type name="Magento\Sitemap\Model\ItemProvider\Composite">
<arguments>
<argument name="itemProviders" xsi:type="array">
<item name="categoryProvider" xsi:type="object">Magento\Sitemap\Model\ItemProvider\Category</item>
<item name="cmsPageProvider" xsi:type="object">Magento\Sitemap\Model\ItemProvider\CmsPage</item>
<item name="productProvider" xsi:type="object">Magento\Sitemap\Model\ItemProvider\Product</item>
</argument>
</arguments>
</type>
Suppose, I want to change at the Cms page list of sitemap. To build a module and that cmsPageProvider
argument value.
Example:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Sitemap\Model\ItemProvider\Composite">
<arguments>
<argument name="itemProviders" xsi:type="array">
<item name="cmsPageProvider" xsi:type="object">MyVendor\MymoDule\Model\ItemProvider\MyCmsPage</item>
</argument>
</arguments>
</type>
</config>
It means MyVendor\MymoDule\Model\ItemProvider\MyCmsPage
Class will call On cms Page collection classes at sitemap generation instead of Magento\Sitemap\Model\ItemProvider\CmsPage