{"id":318273,"date":"2021-12-30T13:46:11","date_gmt":"2021-12-30T13:46:11","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=318273"},"modified":"2021-12-30T13:50:39","modified_gmt":"2021-12-30T13:50:39","slug":"event-before-and-after-write-operation","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/event-before-and-after-write-operation\/","title":{"rendered":"Listening to events before and after write results in Shopware6"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Overview<\/h2>\n\n\n\n<p>In this post, we will discuss how to listen to the event before and after the write operation in <a href=\"https:\/\/developer.shopware.com\/docs\/guides\/plugins\/plugins\/storefront\/add-custom-styling\" target=\"_blank\" rel=\"noreferrer noopener\">Shopware6<\/a>. When you create your custom plugins then you need to perform some additional logic with existing events.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Listen to the event<\/h2>\n\n\n\n<p>In this topic, we listen to the customer register event before and after the write operation in  Shopware6, so let&#8217;s assume we need to add some additional information about the user and persist in another table.<\/p>\n\n\n\n<p>First of all, we find all possible customer events in <code>customerEvent<\/code> class, here we use CUSTOMER_WRITTEN_EVENT for adding some custom logic.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php declare(strict_types=1);\n\nnamespace Webkul\\TestExample\\Subscriber;\n\nuse Shopware\\Core\\Checkout\\Customer\\CustomerEvents;\nuse Shopware\\Core\\Framework\\DataAbstractionLayer\\Event\\EntityWrittenEvent;\nuse Symfony\\Component\\EventDispatcher\\EventSubscriberInterface;\n\nclass WebkulEventSubscriber implements EventSubscriberInterface\n{\n    public static function getSubscribedEvents(): array\n    {\n        return &#091;\n            CustomerEvent::CUSTOMER_WRITTEN_EVENT =&gt; &#039;onCustomerWritten&#039;,\n        ];\n    }\n\n    public function onCustomerWritten(EntityWrittenEvent $event): void\n    {\n        \/\/ Do stuff\n    }\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Reading the event data<\/h2>\n\n\n\n<p>If you want to change the payload of the customer data before the persists in the database. Shopware provides a <code>PreWriteValidationEvent<\/code> which is trigged before the write result set is generated.<\/p>\n\n\n\n<p>Suppose we need to add custom data in custom entity before the customer create, then we listen <code>PreWriteValidationEvent<\/code>  in our subscriber.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php declare(strict_types=1);\n\nnamespace Swag\\BasicExample\\Service;\n\nuse Shopware\\Core\\Checkout\\Customer\\CustomerEvents;\nuse Shopware\\Core\\Framework\\DataAbstractionLayer\\Event\\EntityWrittenEvent;\nuse Shopware\\Core\\Framework\\DataAbstractionLayer\\Write\\Command\\ChangeSetAware;\nuse Shopware\\Core\\Framework\\DataAbstractionLayer\\Write\\Command\\InsertCommand;\nuse Shopware\\Core\\Framework\\DataAbstractionLayer\\Write\\Command\\UpdateCommand;\nuse Shopware\\Core\\Framework\\DataAbstractionLayer\\Write\\Validation\\PreWriteValidationEvent;\nuse Symfony\\Component\\EventDispatcher\\EventSubscriberInterface;\n\nclass WebkulEventSubscriber implements EventSubscriberInterface\n{\n    public static function getSubscribedEvents(): array\n    {\n        return &#091;\n            PreWriteValidationEvent::class =&gt; &#039;triggerChangeSet&#039;,\n            CustomerEvent::CUSTOMER_WRITTEN_EVENT =&gt; &#039;onCustomerWritten&#039;,\n        ];\n    }\n\n    public function triggerChangeSet(PreWriteValidationEvent $event): void\n    {\n        foreach ($event-&gt;getCommands() as $command) {\n            \/\/ Do stuff\n\n        }\n\n    }\n\n    public function onCustomerWritten(EntityWrittenEvent $event): void\n    {\n        foreach ($event-&gt;getWriteResults() as $result) {\n            $changeSet = $result-&gt;getChangeSet();\n\n            \/\/ Do stuff\n        }\n    }\n}<\/pre>\n\n\n\n<p>In the above code, we subscribe to the CUSTOMER_WRITTEN_EVENT for adding custom logic or stuff. In the first method, we subscribe to the PreWriteValidationEvent which is trigged before the write result set is generated.<\/p>\n\n\n\n<p><a href=\"https:\/\/webkul.com\/blog\/shopware-multi-seller-marketplace\/\" target=\"_blank\" rel=\"noreferrer noopener\">Multi-Seller Marketplace Plugin<\/a><\/p>\n\n\n\n<p>Thanks for reading this blog, I hope it will help you. Happy coding \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview In this post, we will discuss how to listen to the event before and after the write operation in Shopware6. When you create your custom plugins then you need to perform some additional logic with existing events. Listen to the event In this topic, we listen to the customer register event before and after <a href=\"https:\/\/webkul.com\/blog\/event-before-and-after-write-operation\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":325,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-318273","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 Listen the event before and after in Shopware6<\/title>\n<meta name=\"description\" content=\"Listen to the event before and after write operation in Shopware6 | How to listen to the event in Shopware6\" \/>\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\/event-before-and-after-write-operation\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Listen the event before and after in Shopware6\" \/>\n<meta property=\"og:description\" content=\"Listen to the event before and after write operation in Shopware6 | How to listen to the event in Shopware6\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/event-before-and-after-write-operation\/\" \/>\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=\"2021-12-30T13:46:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-30T13:50:39+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=\"Prince Gupta\" \/>\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=\"Prince Gupta\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/event-before-and-after-write-operation\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/event-before-and-after-write-operation\/\"},\"author\":{\"name\":\"Prince Gupta\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/669b7c5067f73a7ae2204ff4aca829fd\"},\"headline\":\"Listening to events before and after write results in Shopware6\",\"datePublished\":\"2021-12-30T13:46:11+00:00\",\"dateModified\":\"2021-12-30T13:50:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/event-before-and-after-write-operation\/\"},\"wordCount\":216,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/event-before-and-after-write-operation\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/event-before-and-after-write-operation\/\",\"url\":\"https:\/\/webkul.com\/blog\/event-before-and-after-write-operation\/\",\"name\":\"How to Listen the event before and after in Shopware6\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2021-12-30T13:46:11+00:00\",\"dateModified\":\"2021-12-30T13:50:39+00:00\",\"description\":\"Listen to the event before and after write operation in Shopware6 | How to listen to the event in Shopware6\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/event-before-and-after-write-operation\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/event-before-and-after-write-operation\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/event-before-and-after-write-operation\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Listening to events before and after write results in Shopware6\"}]},{\"@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\/669b7c5067f73a7ae2204ff4aca829fd\",\"name\":\"Prince Gupta\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/dd55a986709c72a714a8135a38f3b2cba1009ea371caec823a8547b2e01df18d?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\/dd55a986709c72a714a8135a38f3b2cba1009ea371caec823a8547b2e01df18d?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Prince Gupta\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/princegupta-wp031\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Listen the event before and after in Shopware6","description":"Listen to the event before and after write operation in Shopware6 | How to listen to the event in Shopware6","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\/event-before-and-after-write-operation\/","og_locale":"en_US","og_type":"article","og_title":"How to Listen the event before and after in Shopware6","og_description":"Listen to the event before and after write operation in Shopware6 | How to listen to the event in Shopware6","og_url":"https:\/\/webkul.com\/blog\/event-before-and-after-write-operation\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2021-12-30T13:46:11+00:00","article_modified_time":"2021-12-30T13:50:39+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":"Prince Gupta","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Prince Gupta","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/event-before-and-after-write-operation\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/event-before-and-after-write-operation\/"},"author":{"name":"Prince Gupta","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/669b7c5067f73a7ae2204ff4aca829fd"},"headline":"Listening to events before and after write results in Shopware6","datePublished":"2021-12-30T13:46:11+00:00","dateModified":"2021-12-30T13:50:39+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/event-before-and-after-write-operation\/"},"wordCount":216,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/event-before-and-after-write-operation\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/event-before-and-after-write-operation\/","url":"https:\/\/webkul.com\/blog\/event-before-and-after-write-operation\/","name":"How to Listen the event before and after in Shopware6","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2021-12-30T13:46:11+00:00","dateModified":"2021-12-30T13:50:39+00:00","description":"Listen to the event before and after write operation in Shopware6 | How to listen to the event in Shopware6","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/event-before-and-after-write-operation\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/event-before-and-after-write-operation\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/event-before-and-after-write-operation\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Listening to events before and after write results in Shopware6"}]},{"@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\/669b7c5067f73a7ae2204ff4aca829fd","name":"Prince Gupta","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/dd55a986709c72a714a8135a38f3b2cba1009ea371caec823a8547b2e01df18d?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\/dd55a986709c72a714a8135a38f3b2cba1009ea371caec823a8547b2e01df18d?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Prince Gupta"},"url":"https:\/\/webkul.com\/blog\/author\/princegupta-wp031\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/318273","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\/325"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=318273"}],"version-history":[{"count":4,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/318273\/revisions"}],"predecessor-version":[{"id":318312,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/318273\/revisions\/318312"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=318273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=318273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=318273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}