TransWikia.com

How to add custom URLs to sitemap in Magento 2?

Magento Asked by latha reddy on December 26, 2021

I need to add the custom links to sitemap.xml in Magento 2. How can I do this?

4 Answers

Use MagentoSitemapModelItemProvider for that you can see

https://belvg.com/blog/how-to-create-sitemap-in-magento.html

Answered by Benoit AUDEVAL on December 26, 2021

update etc/di.xml file.

<preference for="MagentoSitemapModelSitemap" type="{{vendor}}{{modulename}}ModelSitemap"/>

create Sitemap.php file in app/code/{vendor}/{modulename}/Model/

namespace {vendor}{modulename}Model;

use MagentoFrameworkAppObjectManager;
use MagentoFrameworkDataObject;
class Sitemap extends MagentoSitemapModelSitemap
{
   public function getCustomurlCollection()
   {
    $siteMapcollection = array();
    /* if want to add multiple url then load your custom collection and set  */

       /*foreach ($siteMapcollection as $custom) { */

                $siteMapcollection[] = new DataObject([
                    'id'         => "my_custom_id",
                    'url'        => "my_custom_url",
                    'updated_at' => date("Y-m-d h:i:s"),
                ]);
         /*     } */
        return $siteMapcollection;

   }

   public function _initSitemapItems()
    {
        $this->_sitemapItems[] = new DataObject([
            'collection' => $this->getCustomurlCollection(),
        ]);

        parent::_initSitemapItems(); 
    }
}

Answered by Shinesh on December 26, 2021

etc/di.xml

<preference for="MagentoSitemapModelSitemap" type="VendorNameModuleNameModelSitemap" />

Model/Sitemap.php

namespace VendorNameModuleNameModel;

class Sitemap extends MagentoSitemapModelSitemap
{
    protected function _initSitemapItems()
    {
        parent::_initSitemapItems();

        $newLine = [];
        $object = new MagentoFrameworkDataObject();
        $object->setId(['contact']);
        $object->setUrl('contact');
        $object->setUpdatedAt(date("Y-m-d h:i:s"));
        $newLine['contact'] = $object;

        $this->_sitemapItems[] = new MagentoFrameworkDataObject(
            [
                'changefreq' => 'weekly',
                'priority' => '0.25',
                'collection' => $newLine
            ]
        );
    }
}

Answered by Bhavin iFlair on December 26, 2021

Update:

Yes, you can do that by creating plug on class MagentoSitemapModelSitemap.

Create after plugin of collectSitemapItems() and add your custom URL to the collection.

di.xml

<?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="MagentoSitemapModelSitemap">
       <plugin name="add_new_item" sortOrder="1"  disabled="false"
       type="{VendorName}{Modulename}PluginModelSitemapPlugin"/>
    </type>    
</config>

Plugin class:

namespace {VendorName}{Modulename}PluginModel;

class SitemapPlugin
{
       public function __construct(
    MagentoSitemapHelperData $helper
    ) {
        $this->helper = $helper;
}
public function afterCollectSitemapItems(
    MagentoSitemapModelSitemap $subject
    )
{


    $storeId = $subject->getStoreId();
    $newRecords = [];
    $object = new MagentoFrameworkDataObject();
    $object->setId(['my_uniqukey_id']);
    $object->setUrl('contact-us');
    $object->setUpdatedAt('2018-04-04 13:41:58');

    $newRecords['my_uniqukey_id'] = $object;

    $subject->addSitemapItem(new  MagentoFrameworkDataObject(
        [

            'changefreq' => $this->helper->getPageChangefreq($storeId),
            'priority' => $this->helper->getPagePriority($storeId),
            'collection' => $newRecords,
        ]
     ));
}
}

Answered by Amit Bera on December 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP