Excluding Specific Products from Magento Sitemap.xml Generation
As an experienced e-commerce developer, I often receive queries about customizing Magento sitemap generation to exclude certain products. This is particularly useful for removing low-stock items or seasonal products that shouldn’t appear in search engine results.
Understanding the Default Magento Sitemap Process
Before we dive into exclusions, let’s first understand how Magento constructs its sitemap.xml by default. When you enable and save the sitemap configuration under “Configuration > General > Design > HTML Head”, Magento automatically generates a sitemap for all enabled products. The exclusion logic is not built-in.
Excluding Products via Magento Configuration
The most straightforward approach to exclude specific products from the sitemap.xml is by disabling them in the Admin Panel. Here’s how:
- Log in to your Magento Admin Panel.
- Navigate to “Catalog > Manage Products”.
- Select the product(s) you want to exclude from the grid view.
- Click on “Actions” and select “Disable”.
However, this method simply hides the products from the frontend store - they are still part of the backend catalog system. They will not appear in the sitemap.xml.
Customizing Sitemap Generation with an Observer
To achieve more granular control over which products are included in the sitemap.xml, you can create a custom observer for the sitemap_generate_before event. Here’s the process:
- Create an Observer Script
In your custom module directory (let’s assume we have a module called “ExcludeSitemapProducts”), create a file named
app/code/ExcludeSitemapProducts/etc/observers.xml:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App etc/observers.xsd">
<event name="sitemap_generate_before">
<observer name="exclude_specific_products" instance="ExcludeSitemapProducts\Observer\ExcludeSpecificProducts"/>
</event>
</config>
- Create the Observer
In the same module directory, create a file named
app/code/ExcludeSitemapProducts/Observer/ExcludeSpecificProducts.php:
namespace ExcludeSitemapProducts\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Sitemap\Model\ResourceModel\Sitemap as SitemapResourceModel;
class ExcludeSpecificProducts implements ObserverInterface
{
protected $sitemapResource;
public function __construct(SitemapResourceModel $sitemapResource)
{
$this->sitemapResource = $sitemapResource;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$this->excludeProducts($observer->getSitemap());
}
private function excludeProducts(\Magento\Sitemap\Model\Sitemap $sitemap)
{
// Here you can add your logic to exclude specific products based on their IDs, categories, etc.
$productIdsToExclude = [123, 456]; // Example product IDs
foreach ($productIdsToExclude as $id) {
if ($sitemap->getItemByProductId($id)) {
$sitemap->deleteItem($id);
}
}
$this->sitemapResource->save($sitemap);
}
}
In this example, the excludeProducts method removes products with IDs 123 and 456 from the sitemap before it’s saved.
Conclusion
Excluding specific products from Magento’s sitemap.xml generation requires a combination of Admin Panel configuration and custom development. By understanding the default process and implementing an observer to filter out unwanted products, you can achieve greater control over your site’s SEO structure.
Remember to thoroughly test this setup in a staging environment before deploying it to your live store. If you’re looking for more advanced Magento customizations or need expert advice on e-commerce projects, feel free to reach out to us at Laravel Company.
This approach ensures that your sitemap.xml remains clean and relevant, helping search engines index only the most valuable content from your store.