3

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

  • 1
    what Magento version are you using? – Greg Apr 6 '18 at 7:43
4

Update:

Yes, you can do that by creating plug on class Magento\Sitemap\Model\Sitemap.

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="Magento\Sitemap\Model\Sitemap">
       <plugin name="add_new_item" sortOrder="1"  disabled="false"
       type="{VendorName}\{Modulename}\Plugin\Model\SitemapPlugin"/>
    </type>    
</config>

Plugin class:

namespace {VendorName}\{Modulename}\Plugin\Model;

class SitemapPlugin
{
       public function __construct(
    \Magento\Sitemap\Helper\Data $helper
    ) {
        $this->helper = $helper;
}
public function afterCollectSitemapItems(
    \Magento\Sitemap\Model\Sitemap $subject
    )
{


    $storeId = $subject->getStoreId();
    $newRecords = [];
    $object = new \Magento\Framework\DataObject();
    $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  \Magento\Framework\DataObject(
        [

            'changefreq' => $this->helper->getPageChangefreq($storeId),
            'priority' => $this->helper->getPagePriority($storeId),
            'collection' => $newRecords,
        ]
     ));
}
}
  • Function is not working – Vinod Kumar May 2 '18 at 11:11
  • which function is not working? – Amit Bera May 2 '18 at 11:19
  • public function afterCollectSitemapItems( \Magento\Sitemap\Model\Index\Sitemap $subject, $result ) – Vinod Kumar May 2 '18 at 11:22
  • I have created plugin and used this code in plugin class but it is not working – Vinod Kumar May 2 '18 at 11:23
  • it is fixed now.Pleas check it – Amit Bera May 2 '18 at 12:43
3

etc/di.xml

<preference for="Magento\Sitemap\Model\Sitemap" type="VendorName\ModuleName\Model\Sitemap" />

Model/Sitemap.php

namespace VendorName\ModuleName\Model;

class Sitemap extends \Magento\Sitemap\Model\Sitemap
{
    protected function _initSitemapItems()
    {
        parent::_initSitemapItems();

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.