Shopware 6迁移中产品导出自定义字段白名单异常问题
解决Shopware 6迁移后自定义字段无法导出至Shopzilla等渠道的问题
问题根源
Shopware 6的第三方产品导出(比如Shopzilla)默认有自定义字段白名单机制,仅放行以migration_开头的迁移类自定义字段。非此类字段会触发Twig渲染失败,报错示例:
"custom_field_set_product_mybaby_pixi_swpixideliverydate"不在白名单的键列表中
解决方案
1. 通过事件订阅者扩展白名单
创建一个事件订阅者,监听产品导出收集事件,把所有自定义字段加入白名单:
<?php namespace YourPlugin\Subscriber; use Shopware\Core\Content\Product\Events\ProductExportCollectorEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; class ProductExportCustomFieldsSubscriber implements EventSubscriberInterface { public static function getSubscribedEvents(): array { return [ ProductExportCollectorEvent::class => 'onProductExportCollect', ]; } public function onProductExportCollect(ProductExportCollectorEvent $event): void { $criteria = $event->getCriteria(); $customFieldSets = $event->getContext()->getExtension('custom_field_sets'); if (!$customFieldSets) { return; } foreach ($customFieldSets as $set) { foreach ($set->getFields() as $field) { $associationName = 'custom_field_' . $field->getName(); $criteria->addAssociation($associationName); } } } }
接着在插件的services.xml里注册这个订阅者:
<service id="YourPlugin\Subscriber\ProductExportCustomFieldsSubscriber"> <tag name="kernel.event_subscriber"/> </service>
2. 调整导出模板(按需)
如果导出模板里需要直接调用自定义字段,确保字段名正确即可,比如在Shopzilla模板里:
<delivery_date>{{ product.custom_field_set_product_mybaby_pixi_swpixideliverydate }}</delivery_date>
3. 验证
- 清空Shopware缓存
- 重新执行产品导出,检查非迁移类自定义字段是否正常导出,无报错
内容的提问来源于stack exchange,提问作者Marian Steinhorst
相关产品推荐
相关产品推荐

