1

I've had a plugin working on 2.2.2 version but it's not working on 2.3.4. Its seem that collectSitemapItems() method is not firing even in native magento WHen I print_r('Die');die;

public function collectSitemapItems()
    {
        print_r('Not firing');die;
        /** @var $helper \Magento\Sitemap\Helper\Data */
    ...

But it's printing under protected method _initSitemapItems()

/**
     * Initialize sitemap
     *
     * @return void
     */
    protected function _initSitemapItems()
    {
     print_r('Working Here');die; 
    ...

My plugin have afterCollectSitemapItems() Method:

public function afterCollectSitemapItems(
        Sitemap $subject
    )
    {
        print_r('Not firing here either');die;

My Plugin di.xml:

 <type  name="Magento\Sitemap\Model\Sitemap">
   <plugin name="vendor-sitemap" sortOrder="1"  disabled="false" type="Vendor\Module\Plugin\Sitemap\Plugin"/>
 </type>

Any ideas why this isn't working anymore?

2 Answers 2

3

I've found out that my plugin is firing ok after printing in __constructor bu method afterCollectSitemapItems not any more. So using public method generateXml like so:

public function __construct(
        \Magento\Sitemap\Helper\Data $helper
        ) {
            $this->helper = $helper;
    }
    public function beforeGenerateXml(
        \Magento\Sitemap\Model\Sitemap $subject
        )
    {
        $storeId = $subject->getStoreId();
        $newRecords = [];
        $object = new \Magento\Framework\DataObject();
        $object->setId(['test_id']);
        $object->setUrl('test-url');
        $object->setUpdatedAt('2020-04-17 14:24:38');
        $newRecords['test_id'] = $object;
        $subject->addSitemapItem(new  \Magento\Framework\DataObject(
            ['changefreq' => $this->helper->getPageChangefreq($storeId),
             'priority' => $this->helper->getPagePriority($storeId),
             'collection' => $newRecords,]
         ));
    }

}

Worked, Sorted!

Note/Tip: only before method works on generateXml and also when generating always open xml in incognito mode as is caches in chrome browser.

1
  • Thanks for that @Juliano Apr 17, 2020 at 13:41
2

Try this below and add your die as well

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

        die('Not firing here either');
        $storeId = $subject->getStoreId();
        $newRecords = [];
        $object = new \Magento\Framework\DataObject();
        $object->setId(['test_id']);
        $object->setUrl('test-url');
        $object->setUpdatedAt('2020-04-17 14:24:38');

        $newRecords['test_id'] = $object;

        $subject->addSitemapItem(new  \Magento\Framework\DataObject(
            [

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

}
4
  • can I see your DI.XML file Apr 17, 2020 at 13:27
  • this is exactly what I have. it seems that CollectSitemapItems or in my plugin afterCollectSitemapItems isn't doing anything. but Ive found a way around it. Apr 17, 2020 at 13:27
  • @JulianoVargas the issue might be where you using the di.xml. When i did it i didnt put it inside frontend or adminhtml. I kept my di.xml in etx/di.xml but if you got it working that's good news will you let me know how you fixed it just wanted to see what was the error with it Apr 17, 2020 at 13:30
  • @JulianoVargas Facing same issue not calling afterCollectSitemapItems in plugin. can you please share your di.xml and plugin file here so get the idea how you have fixed it. Feb 2 at 3:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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