You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Shopware 6.5中从XML站点地图排除商品URL?

在Shopware 6.5中移除站点地图内指定商品URL的方法

方法1:订阅SitemapItemCollectionEvent事件过滤条目

Shopware 6.5提供了Shopware\Core\Content\Sitemap\Event\SitemapItemCollectionEvent事件,该事件在站点地图条目集合生成后、XML渲染前触发,可通过订阅此事件移除目标商品的URL。

创建事件订阅类示例:

<?php

namespace YourPlugin\Subscriber;

use Shopware\Core\Content\Sitemap\Event\SitemapItemCollectionEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class SitemapProductFilterSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            SitemapItemCollectionEvent::class => 'filterProducts'
        ];
    }

    public function filterProducts(SitemapItemCollectionEvent $event): void
    {
        $collection = $event->getCollection();
        
        // 遍历条目,移除指定商品(示例:根据商品ID过滤)
        foreach ($collection as $key => $item) {
            // 仅处理商品类型的条目
            if ($item->getType() !== 'product') {
                continue;
            }
            
            // 替换为你的过滤条件,比如商品ID在排除列表中
            $excludedProductIds = ['xxxxxxx', 'yyyyyyy'];
            if (in_array($item->getIdentifier(), $excludedProductIds)) {
                $collection->remove($key);
            }
        }
    }
}

将订阅类注册到服务容器(config/services.xml):

<service id="YourPlugin\Subscriber\SitemapProductFilterSubscriber">
    <tag name="kernel.event_subscriber"/>
</service>

方法2:自定义商品站点地图提供者

继承默认的ProductSitemapProvider,重写getItems方法,从数据源层面过滤不需要的商品,避免这些商品进入条目集合。

示例代码:

<?php

namespace YourPlugin\Sitemap\Provider;

use Shopware\Core\Content\Product\ProductDefinition;
use Shopware\Core\Content\Sitemap\Provider\ProductSitemapProvider;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;

class CustomProductSitemapProvider extends ProductSitemapProvider
{
    protected function getCriteria(int $limit, int $offset): Criteria
    {
        $criteria = parent::getCriteria($limit, $offset);
        
        // 添加过滤条件,示例:排除特定分类下的商品
        $criteria->addFilter(new EqualsFilter('product.categoryId', 'not-the-category-you-want'));
        
        // 或根据自定义字段过滤
        // $criteria->addFilter(new EqualsFilter('product.customFields.exclude_from_sitemap', false));
        
        return $criteria;
    }
}

在服务容器中替换默认提供者:

<service id="Shopware\Core\Content\Sitemap\Provider\ProductSitemapProvider" alias="YourPlugin\Sitemap\Provider\CustomProductSitemapProvider" public="true"/>

方法3:结合自定义字段批量控制

如果有统一的排除规则,可先给商品添加自定义字段(如exclude_from_sitemap),在后台标记需要排除的商品,再通过上述两种方法中的任意一种,依据自定义字段值过滤条目。

注意:修改代码后需清除缓存,再进入后台「设置->SEO->站点地图」点击生成按钮,重新生成站点地图。

内容的提问来源于stack exchange,提问作者Anse

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 02:16:01