【问题标题】:Shopware 6 Plugin - text snippet not deleted on uninstall plugin - order custom field labelShopware 6 插件 - 卸载插件时未删除文本片段 - 订购自定义字段标签
【发布时间】: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 删除。

标签: custom-fields shopware6


【解决方案1】:

作者提到 Shopware 6 确认这是一个错误。

我正在为这个问题创建一个答案,即使它已经在 cmets 中得到了回答,以便它可以被接受并解决这个问题。

【讨论】:

    猜你喜欢
    • 2023-01-20
    • 2023-01-20
    • 2023-01-20
    • 2011-12-02
    • 2016-08-19
    • 2020-10-30
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    相关资源
    最近更新 更多