【发布时间】:2022-11-06 18:08:48
【问题描述】:
我们在插件中为订单和产品创建了自定义字段。 Shopware 为自定义字段标签创建文本 sn-ps。 卸载插件时应将其删除。
它适用于产品自定义字段。
...
'customFields' => [
[
'name' => 'product_custom_field_name_dummy',
'type' => CustomFieldTypes::BOOL,
'config' => [
'type' => 'checkbox',
'componentName' => 'sw-field',
'customFieldType' => 'checkbox',
'label' => [
self::GER_ISO => 'Label GER',
self::EN_ISO => 'Label EN',
Defaults::LANGUAGE_SYSTEM => 'Label EN',
]
],
]
],
'relations' => [
[
'entityName' => ProductDefinition::ENTITY_NAME,
],
],
...
但不适用于订单自定义字段。
...
'customFields' => [
[
'name' => 'order_custom_field_name_dummy_one',
'type' => CustomFieldTypes::TEXT,
'config' => [
'customFieldType' => CustomFieldTypes::TEXT,
'label' => [
self::GER_ISO => 'Order Label GER',
self::EN_ISO => 'Order Label EN',
Defaults::LANGUAGE_SYSTEM => 'Order Label EN',
]
],
],
[
'name' => 'order_custom_field_name_dummy_two',
'type' => CustomFieldTypes::SELECT,
'config' => [
'customFieldType' => CustomFieldTypes::SELECT,
'componentName' => 'sw-single-select',
'label' => [
self::GER_ISO => 'Order Label GER 2',
self::EN_ISO => 'Order Label EN 2',
Defaults::LANGUAGE_SYSTEM => 'Order Label EN 2',
],
'options' => [
...
]
]
],
[
'name' => 'order_custom_field_name_dummy_three',
'type' => CustomFieldTypes::DATETIME,
'config' => [
'customFieldType' => CustomFieldTypes::DATETIME,
'label' => [
self::GER_ISO => 'Order Label GER 3',
self::EN_ISO => 'Order Label EN 3',
Defaults::LANGUAGE_SYSTEM => 'Order Label EN 3',
]
],
],
[
'name' => 'order_custom_field_name_dummy_four',
'type' => CustomFieldTypes::SELECT,
'config' => [
'customFieldType' => CustomFieldTypes::SELECT,
'componentName' => 'sw-single-select',
'label' => [
self::GER_ISO => 'Order Label GER 4',
self::EN_ISO => 'Order Label EN 4',
Defaults::LANGUAGE_SYSTEM => 'Order Label EN 4',
],
'options' => [
...
]
],
],
],
'relations' => [
[
'entityName' => OrderDefinition::ENTITY_NAME,
],
],
...
这是 Shopware 的订单自定义字段的问题,还是我们在创建订单自定义字段时可能犯了错误?
编辑:自定义字段是在安装方法上创建的,并通过 CustomFieldSetRepository 在插件内的卸载方法上删除。
编辑:这就是我们在卸载时删除自定义字段的方式:
public function uninstallCustomFieldSet() {
$customFieldSet = $this->getCustomFieldSet(self::CUSTOM_FIELD_SET_NAME);
if ($customFieldSet instanceof CustomFieldSetEntity) {
$this->customFieldSetRepository->delete([['id' => $customFieldSet->getId()]], $this->context);
}
}
protected function getCustomFieldSet(string $customFieldSetName): ?CustomFieldSetEntity {
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', $customFieldSetName));
$criteria->addAssociation('customFields');
$criteria->addAssociation('relations');
$customFieldSet = $this->customFieldSetRepository->search($criteria, $this->context)->first();
if ($customFieldSet instanceof CustomFieldSetEntity) {
return $customFieldSet;
} else {
return null;
}
}
【问题讨论】:
-
如何创建自定义字段?通过存储库服务或 API 在插件内部?
-
通过 CustomFieldSetRepository 在插件内部。
-
您应该将代码添加到插件类的
uninstall方法中,该方法会在插件卸载期间删除您的自定义字段。 -
是的,这就是我的观点。在卸载时删除这些自定义字段时,订单自定义字段的文本 sn-ps 不会被删除。
-
我看到 Shopware 有一个
custom_field.deleted事件的侦听器,但是您删除了 CustomFieldSet 而不是字段本身。所以看起来你需要先删除字段才能触发 sn-p 删除。