2

这是一个 Shopware 6 问题。我想PromotionEntity通过添加一个max_budget字段来扩展它并将其显示为管理中的表单字段。目前只有max_redemptions_globalmax_redemptions_per_customer字段存在。该max_budget字段应出现在管理中,就在max_redemptions_globalmax_redemptions_per_customer字段下方。max_budget将与其他两个类似。如果总订单中此促销的折扣达到 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似乎即使实体扩展存在,它也不会作为促销的关联获取(即使自动加载为真)。

4

1 回答 1

2

好的,所以我看到了如何才能完美地工作。

首先我必须覆盖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-model 更改为promotion.extensions.....

所以答案是这样的: 要在管理员中为实体扩展添加 Shopware 6 表单字段,您必须将扩展的实体关联添加到获取扩展实体的组件(在本例中为 sw-promotion-v2-detail )。之后,您可以使用 twig 中的扩展名promotion.extensions.extensionName....

于 2022-01-03T13:07:17.470 回答