<plugin root>/src/Extension/
. In this case we want to extend the product
entity, so we create a subdirectory Content/Product/
since the entity is located there in the Core. Our class then has to extend from the abstract Shopware\Core\Framework\DataAbstractionLayer\EntityExtension
class, which forces you to implement the getDefinitionClass
method. It has to point to the entity definition you want to extend, so ProductDefinition
in this case.extendFields
and add your new fields in there.CustomExtension
:services.xml
:product
table with a new column, you'll have to add a new table which contains the new data for the product. This new table will then be associated using a OneToOne association.CustomExtension
class by adding a new field in the extendFields
method.OneToOneAssociationField
. Its parameters are the following, in correct order:propertyName
: The name of the property which should contain the associated entity of type ExampleExtensionDefinition
in the ProductDefinition
. Property names are usually camelCase, with the first character being lower cased.storageName
: Use the id
column here, which refers to the id
field of your product. This will be used for the connection to your association. Storage names are always lowercase and snake_cased.referenceField
: In the storageName
you defined one of the two connected columns, id
. The name of the other column in the database, which you want to connect via thisproduct_id
, which we will define in the ExampleExtensionDefinition
.referenceClass
: The class name of the definition that we want to connect via the association.autoload
: As the name suggests, this parameter defines if this association should always be loaded by default when the product is loaded. In this case,ExampleExtensionDefinition
, which we're going to create now. It will contain the actual string field that we wanted to add to the product.ExampleExtensionDefinition
, as mentioned in the CustomExtension
class. Its table name will be swag_example_extension
and it will have custom entity class called ExampleExtensionEntity
, as you can see in the getEntityClass
method. This will remain an example, creating the actual entity ExampleExtensionEntity
is not part of this guide.defineFields
method. There's the default IdField
, that almost every entity owns. The next field is the actual product_id
column, which will be necessary in order to properly this entity with the product and vice versa. It has to be defined as FkField
, since that's what it is: A foreign key.customString
and could now be used in order to store new string data for the product in the database.OneToOneAssociationField
. The first parameter defines the name of the propery again, which will contain the ProductEntity
. Now have a look at the second and third parameter - those are the same as in the ProductDefinition
, but the other way around. This is important, do not forget to do that!ProductDefinition
in this case. The last parameter, once again, defines the auto loading. In this example, the product definition will not be loaded, when you're just trying to load this extension entity. Yet, the extension entity will always automatically be loaded when the product entity is loaded, just like we defined earlier.exampleExtension
, as defined in the product extension class CustomExtension
, and the key customString
, which is the property name that you defined in the ExampleExtensionDefinition
class.Runtime
flag on the new field.StringField
to the extension class itself. Afterwards we're adding the Runtime
flag to this field, so Shopware knows that it doesn't have to take care of this new field automatically. We're doing this ourselves now.Shopware\Core\Content\Product\ProductEvents
.ProductEvents::PRODUCT_LOADED_EVENT
event, which is fired everytime one or multiple products are requested. In the event listener method onProductsLoaded
, we're then adding our own data to the new field via the method addExtension
.services.xml
to register it. Below you can find our services.xml
.