
Shopware 6提供强大的自定义字段系统,允许开发者轻松扩展核心实体(如产品)的数据模型,并自动集成到管理后台界面,同时支持复杂的继承机制。本教程将详细指导如何定义、配置和在管理后台产品表单中利用这些自定义字段,从而避免手动创建实体和处理复杂的UI与继承逻辑。
在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
}
}在上述代码中:
完成迁移并激活插件后,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 %}关键修正和注意事项:
要使上述Twig扩展生效,您还需要在插件的src/Resources/app/administration/src/main.js中注册您的管理后台扩展,并确保Vue组件能够访问到product和parentProduct对象。通常,当您扩展sw-product-detail-base时,这些数据是自动可用的。
通过遵循这些指南,您可以高效且优雅地扩展Shopware 6的管理后台产品表单,满足各种业务需求,同时保持代码的整洁和可维护性。
以上就是深入Shopware 6:在管理后台产品表单中添加和继承自定义字段的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号