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:
- 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.
- 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/