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,
        ]
     ));
}
}
| improve this answer | |
  • 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
2

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
            ]
        );
    }
}
| improve this answer | |
  • i tried the preference, and I generated the new sitemap URL, but it's not adding to the sitemap. – senthil Oct 16 '19 at 6:49
  • this code not working. – Divyesh Rana Nov 18 '19 at 9:44
  • 'changefreq' => 'weekly' is not showing in xml after generated xml – Manish Maheshwari Nov 27 '19 at 9:53
0

update etc/di.xml file.

<preference for="Magento\Sitemap\Model\Sitemap" type="{{vendor}}\{{modulename}}\Model\Sitemap"/>

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

namespace {vendor}\{modulename}\Model;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\DataObject;
class Sitemap extends \Magento\Sitemap\Model\Sitemap
{
   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(); 
    }
}
| improve this answer | |
  • what will be the id? url is the custom new urls right? – jibin george Feb 21 at 11:22
  • 1
    yes, Ex home page is not included in sitemap so if you want to add it then just add above code and add my_custom_id = "homepage" and url = "{{your url}}" – Shinesh Feb 22 at 7:08
  • how to check store id – jibin george Mar 4 at 14:59
  • 1
    protected $_storeManager; public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Store\Model\StoreManagerInterface $storeManager, array $data = [] ) { $this->_storeManager = $storeManager; parent::__construct($context, $data); } /** * Get store identifier * * @return int */ public function getStoreId() { return $this->_storeManager->getStore()->getId(); } – Shinesh Mar 6 at 17:56
0

Use \Magento\Sitemap\Model\ItemProvider for that you can see

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

| improve this answer | |

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.