How to generate google sitemap in Magento 2?

How to generate google sitemap in Magento 2?

add custom url to sitemap  in magento2

A sitemap is a plain text XML contains links to all the pages in your Magento 2 store.

keep Magento google sitemap updated because it is necessary to help search engine robots in indexing your store.

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=""
        <type name="Magento\Sitemap\Model\Sitemap">
            <plugin name="add_new_url" sortOrder="1" disabled="false" type="Egits\General\Plugin\Model\SitemapPlugin"/>

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->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,

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

magento2 sitemap



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.


add sitemap in magento2

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.

  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn
  • Youtube

leave a comment