Web design company India | Custom web development company India
open

How to generate google sitemap in Magento?

  • 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

        
        
            
                
            
        
    

    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.

    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.

  • Post Comments

    Submit Comment




    * Required Fields