1

In Magento 1, sitemap_products_generating_before event allows us to modify the Product Sitemap.

Can I use sitemap_products_generating_before event names in Magento 2.4?

I want to filter the product collection with my custom product attribute:
addFieldToFilter('use_in_sitemap',['eq' => 1])

1 Answer 1

1

There are no related events for sitemap_products_generating_before in Magento 2.

To modify production collection for sitemap, you can use after method plugin to getCollection method in Magento\Sitemap\Model\ResourceModel\Catalog\Product class which is located in vendor/magento/module-sitemap/Model/ResourceModel/Catalog/Product.php

As of Magento 2.2.1, Magento introduce a new prepareSelectStatement method in Magento\Sitemap\Model\ResourceModel\Catalog\Product class which is located in vendor/magento/module-sitemap/Model/ResourceModel/Catalog/Product.php. You can user after method plugin to that method to change the select statement to get Product collection.

Related documents:


Update as your addition request:
getCollection method of Magento\Sitemap\Model\ResourceModel\Catalog\Product class does not return an object collection like others, it return an array or boolean.

In case you want to add a custom filter based on your custom product attribute, there are 2 solutions:

  1. Use afterPrepareSelectStatement plugin method instead of afterGetCollection.

Here you should take a look at _addFilter to know how to apply filter by product attribute.

Note: This solution will take longer time than the 2nd solution (using preference) but this solution allows you to prevent conflict between your customize with Magento code base in the future Magento version upgrade or other 3rd-party extensions (modules) which customize/override _addFilter, _getAttribute and _joinAttribute methods in Magento\Sitemap\Model\ResourceModel\Catalog\Product class.

  1. Use preference to override prepareSelectStatement method in Magento\Sitemap\Model\ResourceModel\Catalog\Product class.

prepareSelectStatement method in your custom module should looks like this:

public function prepareSelectStatement(\Magento\Framework\DB\Select $select)
{
    $this->_addFilter($store->getId(), 'use_in_sitemap', 1);

    return $this->_select;
}

Note: This solution just take a little time for the first implement but it might be lead to conflict between your customize with Magento code base in the future Magento version upgrade or other 3rd-party extensions (modules) which customize/override _addFilter, _getAttribute and _joinAttribute methods in Magento\Sitemap\Model\ResourceModel\Catalog\Product class.

Related documents:

Plugin: https://developer.adobe.com/commerce/php/development/components/plugins/ Preference: https://developer.adobe.com/commerce/php/development/build/dependency-injection-file/

2
  • Added afterGetCollection to plugin. The argument is $result. If I simply wanted to operate a filter on a specific attribute, should I have done something like $result->addFieldToFilter('use_in_sitemap', ['eq' => 1])? Updated question. I would be happy if you could take a look and answer. Commented Dec 25, 2022 at 7:48
  • I've updated my answer to support your addition request, please take a look.
    – Tu Van
    Commented Dec 25, 2022 at 15:02

Your Answer

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

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