I extended the Shopware 6 product
table following Shopware documentation Adding complex data to existing entities.
I have added an input field to accept user data, how do I save it to the database when the product is saved. I want the data available in product.written
event payload.
Since EntityExtension
only allows to add either associations or runtime only fields (evaluated during runtime and cannot be persisted), I assume you registered your own custom entity and associated it with product
.
You can then use the extension
property of the product
entity to store values for associations to the extended entity.
I assume you did override sw-product-settings-form
to place the field in the administration:
Component.override('sw-product-settings-form', {
template,
inject: ['repositoryFactory'],
computed: {
sellerName: {
get() {
return this.product.extensions.yourEntity?.sellerName ?? null;
},
set(value) {
if (!this.customer.extensions.yourEntity) {
this.$set(this.product.extensions, 'yourEntity', this.repositoryFactory.create('your_entity').create());
}
this.$set(this.product.extensions.yourEntity, 'sellerName', value);
},
},
}
});
In your override's template:
<sw-text-field
v-model="sellerName"
/>
Changes to the field will then be persisted when saving the product. Here's an example plugin that extends the customer module with an extension field in the same manner.