Shopware 6-管理中实体扩展的表单字段



这是一个Shopware 6的问题。我想通过添加max_budget字段来扩展PromotionEntity,并在管理中将其显示为表单字段。当前仅存在max_redemptions_globalmax_redemptions_per_customer字段。max_budget字段应显示在max_redemptions_globalmax_redemptions_per_customer字段下方的管理权限中。CCD_ 8的作用将类似于其它两个。如果该促销活动的总订单折扣达到max_budget的值,则该促销活动将不再有效。

所以我创建了一个实体扩展如下:

class PromotionMaxBudgetExtension extends EntityExtension
{
public function extendFields(FieldCollection $collection): void
{
$collection->add(
new OneToOneAssociationField('maxBudget', 'id', 'promotion_id', PromotionMaxBudgetDefinition::class, false)
);
}
public function getDefinitionClass(): string
{
return PromotionDefinition::class;
}
}

然后扩展的定义:

class PromotionMaxBudgetDefinition extends EntityDefinition
{
public const ENTITY_NAME = 'promotion_max_budget';
public function getEntityName(): string
{
return self::ENTITY_NAME;
}
public function getEntityClass(): string
{
return PromotionMaxBudgetEntity::class;
}
protected function defineFields(): FieldCollection
{
return new FieldCollection([
(new IdField('id', 'id'))->addFlags(new Required(), new PrimaryKey()),
(new FkField('promotion_id', 'promotionId', PromotionDefinition::class)),
(new IntField('max_budget', 'maxBudget')),
(new IntField('left_budget', 'leftBudget')),
(new OneToOneAssociationField('promotion', 'promotion_id', 'id', PromotionDefinition::class, false))
]);
}
}

为了显示字段,我必须覆盖sw-promotion-v2-detail-base。所以我把sw-promotion-v2-detail-base.html.twig修改成这样:

{% block sw_promotion_v2_detail_base_general_max_uses_customer %}
{% parent %}
<sw-number-field
v-model="??????"
class="sw-promotion-v2-detail-base__field-max-uses-per-customer"
number-type="int"
:label="Max budget"
:placeholder="$tc('sw-promotion-v2.detail.base.general.maxUsesPerCustomerPlaceholder')"
:disabled="!acl.can('promotion.editor')"
allow-empty
/>
{% endblock %}

所以,我的问题是,我如何告诉Shopware这个字段是用于max_budget实体扩展的,以便它将我对它所做的更改保存在onSave似乎即使实体扩展存在,它也不会作为升级的关联获取(即使autoload为true(。

好的,所以我看到了如何做到这一点才能完美工作。

首先,我必须覆盖sw-promotion-v2-detail组件并添加这个js:

Shopware.Component.override('sw-promotion-v2-detail', {
computed: {
promotionCriteria() {
const criteria = this.$super('promotionCriteria');
criteria.addAssociation('maxBudget');
return criteria;
}
},
});

其次,在sw-promotion-v2-detail-base.html.twig中,字段将如下所示:

<sw-number-field
v-if="promotion.extensions.maxBudget"
v-model="promotion.extensions.maxBudget.maxBudget"
class="sw-promotion-v2-detail-base__field-max-budget"
number-type="int"
:label="$tc('vouchers.maxBudget')"
:placeholder="$tc('sw-promotion-v2.detail.base.codes.individual.generateModal.labelPrefix')"
:disabled="!acl.can('promotion.editor')"
allow-empty
/>

请注意,v型已更改为promotion.extensions。。。。。

所以答案是:要在admin中为实体扩展添加Shopware 6表单字段,您必须将扩展的实体关联添加到提取扩展实体的组件中(在本例中为sw-promotion-v2-detail(。之后,您可以使用具有promotion.extensions.extensionName…的trick中的扩展

最新更新