{"id":263467,"date":"2020-08-16T15:08:38","date_gmt":"2020-08-16T15:08:38","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=263467"},"modified":"2020-08-16T15:08:40","modified_gmt":"2020-08-16T15:08:40","slug":"how-to-extend-the-entity-at-the-shopware","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/how-to-extend-the-entity-at-the-shopware\/","title":{"rendered":"How to extend the entity at the Shopware"},"content":{"rendered":"\n<p>In this blog, you are going to learn \u201c how to extend the entity at the Shopware \u201d.<br>I hope you know the directory structure of\u00a0<a href=\"https:\/\/webkul.com\/blog\/create-product-and-product-variant-in-shopware-6\/\">Shopware<\/a>\u00a06 plugin, if you don\u2019t know, see here-\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/webkul.com\/blog\/create-product-and-product-variant-in-shopware-6\/\" target=\"_blank\">https:\/\/docs.shopware.com\/en\/shopware-platform-dev-en\/internals\/directory-structure<\/a>.<\/p>\n\n\n\n<p>Own entities can be integrated into the core via the corresponding entry in the\u00a0<code>services.xml<\/code>.To extend existing entities, the abstract class\u00a0<code>\\Shopware\\Core\\Framework\\DataAbstractionLayer\\EntityExtension<\/code>\u00a0is used. The EntityExtension must define which entity should be extended in the\u00a0<code>getDefinitionClass<\/code>\u00a0method. Once this extension access in the system, the extension can add more fields to it:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php declare(strict_types=1);\n\nnamespace Webkul\\test\\Core\\Content\\Extension;\n\nuse Shopware\\Core\\Framework\\DataAbstractionLayer\\EntityExtension;\nuse Shopware\\Core\\Framework\\DataAbstractionLayer\\FieldCollection;\nuse Shopware\\Core\\Framework\\DataAbstractionLayer\\Field\\OneToManyAssociationField;\nuse Webkul\\test\\Core\\Content\\test\\testDefinition;\nuse Shopware\\Core\\Content\\Product\\Aggregate\\ProductReview\\ProductReviewDefinition;\nuse Shopware\\Core\\Content\\Product\\ProductDefinition;\nuse Shopware\\Core\\Framework\\DataAbstractionLayer\\Field\\ManyToManyAssociationField;\n\nclass ProductExtension extends EntityExtension\n{\n    public function extendFields(FieldCollection $collection): void\n    {\n        $collection-&gt;add(\n            (new OneToManyAssociationField(&#039;test&#039;, testDefinition::class, &#039;product_id&#039;))\n        );\n    }\n\n    public function getDefinitionClass(): string\n    {\n        return ProductDefinition::class;\n    }\n}<\/pre>\n\n\n\n<p>This example adds another association named\u00a0<code>`OneToManyAssociationField`<\/code>\u00a0to the\u00a0<code>ProductDefinition<\/code>.\u00a0 You can add any association with them or object field or whatever you want to add in the definition. You can learn about the\u00a0<a href=\"https:\/\/docs.shopware.com\/en\/shopware-platform-dev-en\/references-internals\/core\/dal\">flags<\/a>\u00a0or find out which\u00a0<a href=\"https:\/\/docs.shopware.com\/en\/shopware-platform-dev-en\/references-internals\/core\/dal\">field types<\/a>\u00a0are available in Shopware.<\/p>\n\n\n\n<p>So, time to take care of the product entities&#8217; new field yourself. You&#8217;re going to need a new subscriber for this. Have a look\u00a0<a href=\"https:\/\/docs.shopware.com\/en\/shopware-platform-dev-en\/how-to\/register-subscriber\">here<\/a>\u00a0to find out how to properly add your own subscriber class.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php declare(strict_types=1);\n\nnamespace Webkul\\test\\Subscriber;\n\nuse Shopware\\Storefront\\Page\\Product\\ProductLoaderCriteriaEvent;\nuse Symfony\\Component\\EventDispatcher\\EventSubscriberInterface;\n\nclass OrderedProductReviewSubscriber implements EventSubscriberInterface\n{\n    public static function getSubscribedEvents(): array\n    {\n        return &#091;\n            ProductLoaderCriteriaEvent::class =&gt; &#039;onProductCriteriaLoaded&#039;\n        ];\n    }\n\n    public function onProductCriteriaLoaded(ProductLoaderCriteriaEvent $event): void\n    {\n        $event-&gt;getCriteria()-&gt;addAssociation(&#039;test&#039;);\n    }\n}<\/pre>\n\n\n\n<p>As you can see, the subscriber listens to the\u00a0<code>ProductLoaderCriteriaEvent<\/code>\u00a0event, which is triggered every time a set of products is requested. The listener\u00a0<code>onProductCriteriaLoaded<\/code>\u00a0then add an association with them. This is a good way to add your own entities with core entities and wherever you can add your data and display them. There are so many events of a product that helps us to modify the criteria according to them.<br>So whenever you creating your own page add generic page loader and create an event to them so that you can simply add your data.<\/p>\n\n\n\n<p><strong><code>services.xml<\/code>:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?xml version=&quot;1.0&quot; ?&gt;\n\n&lt;container xmlns=&quot;http:\/\/symfony.com\/schema\/dic\/services&quot;\n           xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\n           xsi:schemaLocation=&quot;http:\/\/symfony.com\/schema\/dic\/services http:\/\/symfony.com\/schema\/dic\/services\/services-1.0.xsd&quot;&gt;\n\n    &lt;services&gt;\n        &lt;service id=&quot;Webkul\\test\\Subscriber\\testSubscriber&quot;&gt;\n            &lt;tag name=&quot;kernel.event_subscriber&quot;\/&gt;\n        &lt;\/service&gt;\n         &lt;service id=&quot;Webkul\\test\\Core\\Content\\Extension\\ProductExtension&quot;&gt;\n            &lt;tag name=&quot;shopware.entity.extension&quot;\/&gt;\n        &lt;\/service&gt;\n    &lt;\/services&gt; \n&lt;\/container&gt;<\/pre>\n\n\n\n<p>If you are adding the entity extension file in the service file so make sure tag name `shopware.entity.extension`.<\/p>\n\n\n\n<p>I hope it will help you. Thanks for reading. Happy Coding \ud83d\ude42<br>Thank You.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, you are going to learn \u201c how to extend the entity at the Shopware \u201d.I hope you know the directory structure of\u00a0Shopware\u00a06 plugin, if you don\u2019t know, see here-\u00a0https:\/\/docs.shopware.com\/en\/shopware-platform-dev-en\/internals\/directory-structure. Own entities can be integrated into the core via the corresponding entry in the\u00a0services.xml.To extend existing entities, the abstract class\u00a0\\Shopware\\Core\\Framework\\DataAbstractionLayer\\EntityExtension\u00a0is used. The EntityExtension <a href=\"https:\/\/webkul.com\/blog\/how-to-extend-the-entity-at-the-shopware\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":284,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-263467","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to extend the entity at the Shopware - Webkul Blog<\/title>\n<meta name=\"description\" content=\"Own entities can be integrated into the core via the corresponding entry in the services.xml. Once this extension is accessed in the system.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/how-to-extend-the-entity-at-the-shopware\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to extend the entity at the Shopware - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"Own entities can be integrated into the core via the corresponding entry in the services.xml. Once this extension is accessed in the system.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/how-to-extend-the-entity-at-the-shopware\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-08-16T15:08:38+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-08-16T15:08:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Diwakar Rana\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Diwakar Rana\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-extend-the-entity-at-the-shopware\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-extend-the-entity-at-the-shopware\/\"},\"author\":{\"name\":\"Diwakar Rana\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/4b025fe4ecbc5c0378cd13bb70da654f\"},\"headline\":\"How to extend the entity at the Shopware\",\"datePublished\":\"2020-08-16T15:08:38+00:00\",\"dateModified\":\"2020-08-16T15:08:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-extend-the-entity-at-the-shopware\/\"},\"wordCount\":306,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/how-to-extend-the-entity-at-the-shopware\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-extend-the-entity-at-the-shopware\/\",\"url\":\"https:\/\/webkul.com\/blog\/how-to-extend-the-entity-at-the-shopware\/\",\"name\":\"How to extend the entity at the Shopware - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2020-08-16T15:08:38+00:00\",\"dateModified\":\"2020-08-16T15:08:40+00:00\",\"description\":\"Own entities can be integrated into the core via the corresponding entry in the services.xml. Once this extension is accessed in the system.\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-extend-the-entity-at-the-shopware\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/how-to-extend-the-entity-at-the-shopware\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-extend-the-entity-at-the-shopware\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to extend the entity at the Shopware\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/4b025fe4ecbc5c0378cd13bb70da654f\",\"name\":\"Diwakar Rana\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/46482d0264c191ccd0337892016340a80ca4e4987a37f42514a0506aaee7e8dc?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/46482d0264c191ccd0337892016340a80ca4e4987a37f42514a0506aaee7e8dc?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Diwakar Rana\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/diwakar-rana829\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to extend the entity at the Shopware - Webkul Blog","description":"Own entities can be integrated into the core via the corresponding entry in the services.xml. Once this extension is accessed in the system.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/how-to-extend-the-entity-at-the-shopware\/","og_locale":"en_US","og_type":"article","og_title":"How to extend the entity at the Shopware - Webkul Blog","og_description":"Own entities can be integrated into the core via the corresponding entry in the services.xml. Once this extension is accessed in the system.","og_url":"https:\/\/webkul.com\/blog\/how-to-extend-the-entity-at-the-shopware\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2020-08-16T15:08:38+00:00","article_modified_time":"2020-08-16T15:08:40+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Diwakar Rana","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Diwakar Rana","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/how-to-extend-the-entity-at-the-shopware\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/how-to-extend-the-entity-at-the-shopware\/"},"author":{"name":"Diwakar Rana","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/4b025fe4ecbc5c0378cd13bb70da654f"},"headline":"How to extend the entity at the Shopware","datePublished":"2020-08-16T15:08:38+00:00","dateModified":"2020-08-16T15:08:40+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/how-to-extend-the-entity-at-the-shopware\/"},"wordCount":306,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/how-to-extend-the-entity-at-the-shopware\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/how-to-extend-the-entity-at-the-shopware\/","url":"https:\/\/webkul.com\/blog\/how-to-extend-the-entity-at-the-shopware\/","name":"How to extend the entity at the Shopware - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2020-08-16T15:08:38+00:00","dateModified":"2020-08-16T15:08:40+00:00","description":"Own entities can be integrated into the core via the corresponding entry in the services.xml. Once this extension is accessed in the system.","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/how-to-extend-the-entity-at-the-shopware\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/how-to-extend-the-entity-at-the-shopware\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/how-to-extend-the-entity-at-the-shopware\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to extend the entity at the Shopware"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/4b025fe4ecbc5c0378cd13bb70da654f","name":"Diwakar Rana","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/46482d0264c191ccd0337892016340a80ca4e4987a37f42514a0506aaee7e8dc?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/46482d0264c191ccd0337892016340a80ca4e4987a37f42514a0506aaee7e8dc?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Diwakar Rana"},"url":"https:\/\/webkul.com\/blog\/author\/diwakar-rana829\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/263467","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/284"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=263467"}],"version-history":[{"count":3,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/263467\/revisions"}],"predecessor-version":[{"id":263470,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/263467\/revisions\/263470"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=263467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=263467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=263467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}