Magento 2 Sitemap: How to Generate & Configure

Posted on: 10 Feb 2020 by Admin

In this tutorial you will learn how to configure sitemap in Magento 2 programmatically

what is a sitemap

A sitemap is a plain-text XML file that holds the link to all of the pages on your Magento store.
Once you add the links to the sitemap search engines can crawl your whole websites easily.

Therefore, it is important to keep the sitemap page updated as regularly as the content on your store changes.
Generating a Magento Sitemap also enhances the SEO of your online store.

Lets start how to configure sitemap in Magento 2 programmatically

In order to add custom URL to the Magento sitemap programmatically, you need to create a plugin for class Magento\Sitemap\Model\Sitemap.
Create an after plugin for collectSitemapItems() and add your custom URL to the collection.

Step 1 : Create di.xml in folder app/code/Egits/General/etc

<?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\Sitemap">

        <plugin name="add_new_url" sortOrder="1" disabled="false" type="Egits\General\Plugin\Model\SitemapPlugin"/>

    </type>

</config>

 

Here I used Egits as vendor_name and General as my module_name.
Step 2: Create a plugin class SitemapPlugin in folder app/code/Egits/General/Plugin/Model

helper = $helper;

    }

    /**

     * @param \Magento\Sitemap\Model\Sitemap $subject

     */

    public function afterCollectSitemapItems(\Magento\Sitemap\Model\Sitemap $subject)

    {

        $newItem = [];

        $storeId = $subject->getStoreId();

        $object = new \Magento\Framework\DataObject();

        $object->setId('unique_id');

        $object->setUrl('custom_url');

        $object->setUpdatedAt(date('Y-m-d h:i:s'));

        $newItem['unique_id'] = $object;

        $subject->addSitemapItem(new  \Magento\Framework\DataObject(

            [

                'changefreq' => $this->helper->getPageChangefreq($storeId),

                'priority' => $this->helper->getPagePriority($storeId),

                'collection' => $newItem,

            ]

        ));

        return;

    }

}

 

Then go to admin panel of your store and navigate to Marketing and click Sitemap under SEO & Search.

 

Under the Sitemap generator page, click on the Add Sitemap button on the right top corner if the sitemap is not yet added.
Fill the required fields such as File name and File Path.

 

Once it is done click the Generate link in the action column to generate the Magento sitemap.
Click on the Magento sitemap link to see the sitemap.

If the Magento sitemap page is already added then click the Generate button in the action column to complete the final step.

If you have followed the about instructions carefully then you have successfully added the URL to the sitemap in Magento 2.

Note: Hit the Magento 2 sitemap URL and ensure that your custom URL is added. As a Magento development company, we recommend you to keep your sitemap updated to improve your website ranking.

Well above you have read the process for sitemaping in Magento 2, then for making this process easiler please refer to the tutorial below.