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

Shopware 6.5导入导出配置文件:如何用ImportExportBeforeImportRecordEvent判断实体

在Shopware 6.5中针对特定实体类型修改导入数据的解决方案

在Shopware 6.5移除EntityRepositoryDecorator后,若需借助ImportExportBeforeImportRecordEvent针对特定实体类型修改导入数据,可通过以下方式获取当前导入的实体类型:

  • 从事件对象中直接获取导入配置文件(Profile),配置文件的source字段存储了对应的实体类型信息
  • 解析source字段即可得到当前导入的实体名称,以此区分不同实体的修改逻辑

代码示例

创建事件订阅类,实现针对不同实体的导入数据修改:

<?php

namespace YourPlugin\Subscriber;

use Shopware\Core\Content\ImportExport\Event\ImportExportBeforeImportRecordEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class ImportDataModifierSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            ImportExportBeforeImportRecordEvent::class => 'onBeforeImportRecord',
        ];
    }

    public function onBeforeImportRecord(ImportExportBeforeImportRecordEvent $event): void
    {
        // 获取当前导入的配置文件
        $profile = $event->getProfile();
        // 从source字段解析实体类型,source格式为{"entity":"product"}
        $sourceConfig = json_decode($profile->getSource(), true);
        $entityName = $sourceConfig['entity'] ?? null;

        if (!$entityName) {
            return;
        }

        // 根据实体类型执行对应的数据修改逻辑
        $record = $event->getRecord();
        switch ($entityName) {
            case 'product':
                // 修改产品导入数据,例如调整价格字段
                $record['price'] = $record['price'] * 1.05;
                break;
            case 'category':
                // 修改分类导入数据,例如统一分类名称格式
                $record['name'] = ucwords($record['name']);
                break;
            // 其他实体类型的处理逻辑
        }

        // 将修改后的记录放回事件中
        $event->setRecord($record);
    }
}

关键说明

  • $profile->getSource()返回JSON字符串,包含导入核心配置,其中entity字段明确标记当前导入的实体类型
  • Shopware默认导入配置均包含source字段的entity键值对,自定义配置需确保该字段存在
  • 通过$event->setRecord()重新赋值修改后的记录,即可完成导入前的数据调整

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 00:37:17