【发布时间】:2023-04-06 10:18:01
【问题描述】:
这是一个 Shopware 6 问题。我想通过添加max_budget 字段来扩展PromotionEntity 并将其显示为管理中的表单字段。目前只存在max_redemptions_global 和max_redemptions_per_customer 字段。 max_budget 字段应出现在管理中的 max_redemptions_global 和 max_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)。
【问题讨论】: