Bhaveshpp

Professional Magento Developr - Healp eachother to grow

Magento2: Exclude url from sitemap

06 Aug 2021 » magento2

define plugin

<?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\ResourceModel\Catalog\Category">
        <plugin name="afterGetCategoryCollection" type="Bhaveshpp\Custom\Plugin\Magento\Sitemap\Model\ResourceModel\Catalog\Category" sortOrder="1" />
    </type>
    <type name="Magento\Sitemap\Model\ResourceModel\Catalog\Product">
        <plugin name="afterGetProductCollection" type="Bhaveshpp\Custom\Plugin\Magento\Sitemap\Model\ResourceModel\Catalog\Product" sortOrder="1" />
    </type>
    <type name="Magento\Sitemap\Model\ResourceModel\Cms\Page">
        <plugin name="afterGetPageCollection" type="Bhaveshpp\Custom\Plugin\Magento\Sitemap\Model\ResourceModel\Cms\Page" sortOrder="1" />
    </type>
</config>

define systemconfig

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <section id="web" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Web</label>
            <tab>general</tab>
            <resource>Magento_Config::web</resource>
            <group id="sitemap_xml" translate="label comment" type="text" sortOrder="100" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>Sitemap XML</label>
                <comment>Remove Specific url from sitemap</comment>
                <field id="exclude_url" translate="label comment" type="textarea" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
                    <label>Exclude URL</label>
                    <comment><![CDATA[
                        add [,] comma seperated string
                    ]]></comment>
                </field>
            </group>
        </section>
    </system>
</config>

define model

<?php 
namespace Bhaveshpp\Custom\Model;
class Check  
{
    const EXCLUDE_URL_CONFIG = 'web/sitemap_xml/exclude_url';
    /**
     * @var ScopeConfigInterface
     */
    protected $_scopConfig;

    /**
     * constructor
     *
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopConfigIntrface
     */
    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopConfigIntrface
    )
    {
        $this->_scopConfig = $scopConfigIntrface;
    }
    public function checkAndReturn($data)
    {
        $excludeUrls = $this->_scopConfig->getValue(self::EXCLUDE_URL_CONFIG);
        $excludeUrls = explode(",",$excludeUrls);
        $returnArr = array_map(function($item) use($excludeUrls)
        {
            if ($item !== null) {
                if(!in_array($item['url'], $excludeUrls)){
                    return $item; 
                }
            }
            return new \Magento\Framework\DataObject();
        },$data);
        return $returnArr;
    }
}

use model

<?php 
namespace Bhaveshpp\Custom\Plugin\Magento\Sitemap\Model\ResourceModel\Cms;
use Bhaveshpp\Custom\Model\Check;
class Page 
{
    /**
     * @var Check
     */
    protected $_checkModel;

    /**
     * Constructor
     *
     * @param Check $check
     */         
    public function __construct(
        Check $check
    )
    {
        $this->_checkModel = $check;
    }

    /**
     * Filter specific urls
     *
     * @param \Magento\Sitemap\Model\ResourceModel\Cms\Page $subject
     * @param array|bool $return
     * @return array|bool
     */
    public function afterGetCollection(
        \Magento\Sitemap\Model\ResourceModel\Cms\Page $subject,
        $return
    )
    {
        return $this->_checkModel->checkAndReturn($return);
    }
}