
Shopware 6提供强大的自定义字段系统,允许开发者轻松扩展核心实体(如产品)的数据模型,并自动集成到管理后台界面,同时支持复杂的继承机制。本教程将详细指导如何定义、配置和在管理后台产品表单中利用这些自定义字段,从而避免手动创建实体和处理复杂的UI与继承逻辑。
理解Shopware 6的自定义字段系统
在Shopware 6中,当需要为现有实体(例如产品、客户、订单等)添加额外数据时,最推荐和最强大的方式是使用其内置的“自定义字段”(Custom Fields)系统。这个系统旨在简化数据扩展过程,它不仅处理数据库层的字段添加,还负责在管理后台自动生成用户界面,并无缝支持Shopware的继承机制。这意味着您无需手动编写Vue组件来渲染字段或处理父子产品之间的数据继承逻辑。
自定义字段通常通过插件来定义,它们可以附加到任何使用EntityCustomFieldsTrait的实体上,ProductEntity就是其中之一。
如何定义和配置自定义字段
定义自定义字段主要通过插件的迁移文件或services.xml配置来完成。以下是使用迁移文件添加自定义字段的示例:
<?php declare(strict_types=1);
namespace YourPlugin/Migration;
use Doctrine/DBAL/Connection;
use Shopware/Core/Framework/Migration/MigrationStep;
use Shopware/Core/Framework/Uuid/Uuid;
class Migration1678888888AddProductFabricFields extends MigrationStep
{
public function get}CreationTimestamp(): int
{
return 1678888888;
}
public function update(Connection $connection): void
{
$connection->insert('custom_field_set', [
'id' => Uuid::randomBytes(),
'name' => 'custom_product_fabric_fields',
'config' => json_encode([
'label' => [
'zh-CN' => '产品面料信息',
'en-GB' => 'Product Fabric Information',
],
]),
'created_at' => (new /DateTime())->format(DATE_ATOM),
]);
$customFieldSetId = $connection->lastInsertId();
// 关联到产品实体
$connection->insert('custom_field_set_relation', [
'custom_field_set_id' => $customFieldSetId,
'entity_name' => 'product',
]);
// 添加第一个自定义字段:最小购买米数
$connection->insert('custom_field', [
'id' => Uuid::randomBytes(),
'name' => 'custom_product_min_purchase_meters',
'type' => 'float',
'config' => json_encode([
'label' => [
'zh-CN' => '最小购买米数',
'en-GB' => 'Min Purchase Meters',
],
'customFieldType' => 'number',
'customFieldPosition' => 1,
'numberType' => 'float', // 确保输入框接受浮点数
'min' => 0,
]),
'set_id' => $customFieldSetId,
'created_at' => (new /DateTime())->format(DATE_ATOM),
]);
// 添加第二个自定义字段:步长米数
$connection->insert('custom_field', [
'id' => Uuid::randomBytes(),
'name' => 'custom_product_step_meters',
'type' => 'float',
'config' => json_encode([
'label' => [
'zh-CN' => '购买步长米数',
'en-GB' => 'Purchase Step Meters',
],
'customFieldType' => 'number',
'customFieldPosition' => 2,
'numberType' => 'float',
'min' => 0,
]),
'set_id' => $customFieldSetId,
'created_at' => (new /DateTime())->format(DATE_ATOM),
]);
// 添加第三个自定义字段:最大购买米数
$connection->insert('custom_field', [
'id' => Uuid::randomBytes(),
'name' => 'custom_product_max_purchase_meters',
'type' => 'float',
'config' => json_encode([
'label' => [
'zh-CN' => '最大购买米数',
'en-GB' => 'Max Purchase Meters',
],
'customFieldType' => 'number',
'customFieldPosition' => 3,
'numberType' => 'float',
'min' => 0,
]),
'set_id' => $customFieldSetId,
'created_at' => (new /DateTime())->format(DATE_ATOM),
]);
}
public function updateDestructive(Connection $connection): void
{
// Implement destructive changes if necessary, e.g., dropping custom fields
}
}
在上述代码中:
- 我们创建了一个custom_field_set,它是一个逻辑分组,可以包含多个自定义字段。
- 通过custom_field_set_relation表,我们将这个字段集关联到product实体。
- 然后,我们定义了三个custom_field,类型均为float,并配置了它们的标签、类型和在管理后台的显示位置。numberType: ‘float’是关键,它指示管理后台的sw-field组件渲染一个浮点数输入框。
完成迁移并激活插件后,Shopware会自动在产品编辑页面的“自定义字段”选项卡下显示这些新字段。
在管理后台现有区域集成自定义字段
虽然自定义字段会自动出现在“自定义字段”选项卡中,但有时我们希望将它们直接嵌入到产品表单的特定部分(例如,像原始问题中提到的“可交付性”部分)。这需要通过扩展管理后台的Vue组件模板来实现。
以下是一个基于原始问题中的Twig模板,但经过修正以正确绑定到Shopware自定义字段的示例:
{# plugins/YourPlugin/src/Resources/app/administration/src/module/sw-product/view/sw-product-detail-base/index.html.twig #}
{% sw_extends '@SwProduct/administration/src/module/sw-product/view/sw-product-detail-base/sw-product-detail-base.html.twig' %}
{% block sw_product_deliverability_form_min_purchase_field %}
{# 假设您想替换或添加字段到“最小购买量”附近 #}
{# 原始的最小购买量字段可能在这里,您可以选择保留或替换 #}
{{ parent() }} {# 保留原始的最小购买量字段 #}
<sw-inherit-wrapper
v-model="product.customFields.custom_product_min_purchase_meters"
class="sw-product-deliverability__custom-min-purchase-meters"
:has-parent="!!parentProduct.id"
:inherited-value="parentProduct.customFields.custom_product_min_purchase_meters"
>
<template #content="props">
<sw-field
type="number"
:map-inheritance="props"
number-type="float"
:min="0"
:label="$tc('your-plugin.product.labelMinPurchaseMeters')" {# 使用插件的翻译键 #}
:placeholder="$tc('your-plugin.product.placeholderMinPurchaseMeters')"
:disabled="props.isInherited || !allowEdit"
:value="props.currentValue"
@change="props.updateCurrentValue"
/>
</template>
</sw-inherit-wrapper>
{# 您可以类似地添加其他两个字段 #}
<sw-inherit-wrapper
v-model="product.customFields.custom_product_step_meters"
class="sw-product-deliverability__custom-step-meters"
:has-parent="!!parentProduct.id"
:inherited-value="parentProduct.customFields.custom_product_step_meters"
>
<template #content="props">
<sw-field
type="number"
:map-inheritance="props"
number-type="float"
:min="0"
:label="$tc('your-plugin.product.labelStepMeters')"
:placeholder="$tc('your-plugin.product.placeholderStepMeters')"
:disabled="props.isInherited || !allowEdit"
:value="props.currentValue"
@change="props.updateCurrentValue"
/>
</template>
</sw-inherit-wrapper>
<sw-inherit-wrapper
v-model="product.customFields.custom_product_max_purchase_meters"
class="sw-product-deliverability__custom-max-meters"
:has-parent="!!parentProduct.id"
:inherited-value="parentProduct.customFields.custom_product_max_purchase_meters"
>
<template #content="props">
<sw-field
type="number"
:map-inheritance="props"
number-type="float"
:min="0"
:label="$tc('your-plugin.product.labelMaxMeters')"
:placeholder="$tc('your-plugin.product.placeholderMaxMeters')"
:disabled="props.isInherited || !allowEdit"
:value="props.currentValue"
@change="props.updateCurrentValue"
/>
</template>
</sw-inherit-wrapper>
{% endblock %}
关键修正和注意事项:
- 数据绑定 (v-model): 正确的自定义字段绑定路径是 product.customFields.your_custom_field_name。Shopware会将所有自定义字段聚合在实体的 customFields 属性下。
- 继承值 (inherited-value): 对于父产品的值,也应通过 parentProduct.customFields.your_custom_field_name 来获取。
- 翻译: 建议为自定义字段的标签和占位符使用插件内部的翻译键,而不是硬编码或使用Shopware核心的翻译键,以保持模块化。
-
sw-inherit-wrapper: 这个组件是处理Shopware继承逻辑的核心。
- v-model: 绑定当前产品自定义字段的值。
- :has-parent: 判断当前产品是否有父产品。
- :inherited-value: 绑定父产品对应自定义字段的值。
- sw-field的:map-inheritance=”props”属性是关键,它将sw-inherit-wrapper提供的继承状态(如isInherited)映射到sw-field组件,从而控制字段的禁用状态和继承指示器。
- sw-field的number-type=”float”: 确保输入框允许浮点数输入,这与我们在迁移文件中定义的float类型相匹配。
要使上述Twig扩展生效,您还需要在插件的src/Resources/app/administration/src/main.js中注册您的管理后台扩展,并确保Vue组件能够访问到product和parentProduct对象。通常,当您扩展sw-product-detail-base时,这些数据是自动可用的。
总结与最佳实践
- 优先使用自定义字段: 对于简单的实体数据扩展,始终优先考虑Shopware的自定义字段系统。它能自动处理数据库、API、管理后台UI和继承,大大减少开发工作量和潜在错误。
- 避免手动创建实体扩展: 除非您需要创建具有复杂业务逻辑和关系的新独立实体,否则不要为简单的字段添加手动创建新的实体扩展和Repository,这会使您的代码变得复杂且难以维护。
- 数据类型匹配: 在定义自定义字段时,确保其type和config.numberType(如果适用)与您期望在UI中使用的sw-field类型匹配。
- 清晰的命名和翻译: 为自定义字段使用清晰、有意义的名称,并提供多语言翻译,以提高管理后台的用户体验。
通过遵循这些指南,您可以高效且优雅地扩展Shopware 6的管理后台产品表单,满足各种业务需求,同时保持代码的整洁和可维护性。
以上就是深入Shopware 6:在管理后台产品表单中添加和继承自定义字段的详细内容,更多请关注php中文网其它相关文章!


