【问题标题】:Shopware 6 - form field for entity extension in administrationShopware 6 - 管理中实体扩展的表单字段
【发布时间】:2023-04-06 10:18:01
【问题描述】:

这是一个 Shopware 6 问题。我想通过添加max_budget 字段来扩展PromotionEntity 并将其显示为管理中的表单字段。目前只存在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 好像即使实体扩展存在,它也不会作为促销的关联获取(即使 autoload 为 true)。

【问题讨论】:

    标签: shopware shopware6


    【解决方案1】:

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

    首先我必须覆盖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)。 之后,您可以使用带有promotion.extensions.extensionName....

    的树枝中的扩展程序

    【讨论】:

      猜你喜欢
      • 2023-01-31
      • 1970-01-01
      • 2022-11-26
      • 2023-01-29
      • 1970-01-01
      • 2023-03-21
      • 2022-11-12
      • 1970-01-01
      • 2022-07-08
      相关资源
      最近更新 更多