{"id":312451,"date":"2021-11-19T13:45:33","date_gmt":"2021-11-19T13:45:33","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=312451"},"modified":"2021-11-26T12:54:56","modified_gmt":"2021-11-26T12:54:56","slug":"how-to-get-data-according-to-sales-channel-using-predefined-event","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-predefined-event\/","title":{"rendered":"How to get data according to sales channel using predefined event"},"content":{"rendered":"\n<p>In this blog, you are going to learn \u201cHow to get data according to sales channel using predefined event.\u201d<br>I hope you know the directory structure of&nbsp;<a href=\"https:\/\/webkul.com\/blog\/create-product-and-product-variant-in-shopware-6\/\">Shopware<\/a>&nbsp;6 plugin, if you don\u2019t know, see here-&nbsp;<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>The Purpose of using sales channel is some of the event does not have the sales channel context. So we need to add sales channel event to get data according to them.  Sales Channel context is required for using core service like <code>productListRoute<\/code> service.<\/p>\n\n\n\n<p>Create an subscriber file for using the event, follow the directory structure : <code>&lt;plugin root&gt;\/src\/Subscriber\/TestSubscribe<\/code>r.php<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">TestSubscriber.php<\/h6>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php declare(strict_types=1);\n\nnamespace Webkul\\Test\\Subscriber;\n\n\nuse Shopware\\Core\\Content\\Product\\ProductEvents;\nuse Symfony\\Component\\EventDispatcher\\EventSubscriberInterface;\nuse Shopware\\Core\\Framework\\DataAbstractionLayer\\Event\\EntityLoadedEvent;\nuse Shopware\\Core\\System\\SalesChannel\\Entity\\SalesChannelEntityLoadedEvent;\nuse Shopware\\Core\\Checkout\\Customer\\SalesChannel\\LoginRoute;\nuse Shopware\\Core\\Framework\\Validation\\DataBag\\DataBag;\n\nclass TestSubscriber implements EventSubscriberInterface\n{   \n    \/**\n     * @var LoginRoute\n     *\/\n    protected $loginRoute;\n\n    public function __construct(LoginRoute $loginRoute)\n    {\n        $this-&gt;loginRoute = $loginRoute;\n    }\n\n    public static function getSubscribedEvents()\n    {\n        return &#091;\n            ProductEvents::PRODUCT_LOADED_EVENT =&gt; &#039;updateLoadedProduct&#039;,\n            &#039;sales_channel.&#039;. ProductEvents::PRODUCT_LOADED_EVENT =&gt; &#039;salesChannelLoadedProduct&#039;\n            \n        ];\n    }\n\n    public function updateLoadedProduct(EntityLoadedEvent $event)\n    {  \n        \/\/$event doesn&#039;t have sales channel\n    }\n\n    public function salesChannelLoadedProduct(SalesChannelEntityLoadedEvent $event)\n    {   \n       $saleChannelContext = $event-&gt;getSalesChannelContext();\n       $dataBag = new DataBag();\n       $dataBag-&gt;set(&#039;username&#039;, &#039;test@webkul.com&#039;);\n       $dataBag-&gt;set(&#039;password&#039;, &#039;test&#039;);\n       $this-&gt;loginRoute-&gt;login($dataBag, $saleChannelContext);\n    }\n}<\/pre>\n\n\n\n<p>Add <code>sales_channel<\/code> in the event and use <code>SalesChannelEntityLoadedEvent<\/code> , this file load the data according to sales channel. In the above code, login route need data bag as first argument, salesChannelContext as second argument. If we use only <code>EntityLoadedEvent<\/code> we didn&#8217;t call login function because of this must use the sales channel.<\/p>\n\n\n\n<p>In Shopware, core service is now based on sales channel , there is two definition of product table. One of ProductDefiniton <code>Webkul\\MultiVendor\\Core\\Content\\Bundle\\ProductDefinition<\/code> and second is SalesChannelProductDefinition <code>Shopware\\Core\\Content\\Product\\SalesChannel\\SalesChannelProductDefinition<\/code>, this is reason we didn&#8217;t get sales channel like wise other table or service is done.<\/p>\n\n\n\n<p>To add the service follow the directory structure:-  <code>&lt;plugin-root&gt;\/src\/Resources\/config\/services.x<\/code>ml<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">services.xml<\/h6>\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; public=&quot;true&quot;&gt;\n            &lt;tag name=&quot;kernel.event_subscriber&quot; \/&gt;\n            &lt;argument type=&quot;service&quot; id=&quot;Shopware\\Core\\Checkout\\Customer\\SalesChannel\\LoginRoute&quot;&gt;&lt;\/argument&gt;\n        &lt;\/service&gt;\n    &lt;\/services&gt;\n&lt;\/container&gt;<\/pre>\n\n\n\n<p>Add subscriber file in the service container so that Shopware knows this file and also add the login route service in the argument of that subscriber.<\/p>\n\n\n\n<p> 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 \u201cHow to get data according to sales channel using predefined event.\u201dI hope you know the directory structure of&nbsp;Shopware&nbsp;6 plugin, if you don\u2019t know, see here-&nbsp;https:\/\/docs.shopware.com\/en\/shopware-platform-dev-en\/internals\/directory-structure. The Purpose of using sales channel is some of the event does not have the sales channel context. So we need to <a href=\"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-predefined-event\/\">[&#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":[9568],"tags":[],"class_list":["post-312451","post","type-post","status-publish","format-standard","hentry","category-shopware"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to get data according to sales channel using predefined event - Webkul Blog<\/title>\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-get-data-according-to-sales-channel-using-predefined-event\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to get data according to sales channel using predefined event - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"In this blog, you are going to learn \u201cHow to get data according to sales channel using predefined event.\u201dI hope you know the directory structure of&nbsp;Shopware&nbsp;6 plugin, if you don\u2019t know, see here-&nbsp;https:\/\/docs.shopware.com\/en\/shopware-platform-dev-en\/internals\/directory-structure. The Purpose of using sales channel is some of the event does not have the sales channel context. So we need to [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-predefined-event\/\" \/>\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-11-19T13:45:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-11-26T12:54:56+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=\"2 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-get-data-according-to-sales-channel-using-predefined-event\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-predefined-event\/\"},\"author\":{\"name\":\"Diwakar Rana\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/4b025fe4ecbc5c0378cd13bb70da654f\"},\"headline\":\"How to get data according to sales channel using predefined event\",\"datePublished\":\"2021-11-19T13:45:33+00:00\",\"dateModified\":\"2021-11-26T12:54:56+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-predefined-event\/\"},\"wordCount\":255,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"articleSection\":[\"Shopware\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-predefined-event\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-predefined-event\/\",\"url\":\"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-predefined-event\/\",\"name\":\"How to get data according to sales channel using predefined event - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2021-11-19T13:45:33+00:00\",\"dateModified\":\"2021-11-26T12:54:56+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-predefined-event\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-predefined-event\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-predefined-event\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to get data according to sales channel using predefined event\"}]},{\"@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 get data according to sales channel using predefined event - Webkul Blog","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-get-data-according-to-sales-channel-using-predefined-event\/","og_locale":"en_US","og_type":"article","og_title":"How to get data according to sales channel using predefined event - Webkul Blog","og_description":"In this blog, you are going to learn \u201cHow to get data according to sales channel using predefined event.\u201dI hope you know the directory structure of&nbsp;Shopware&nbsp;6 plugin, if you don\u2019t know, see here-&nbsp;https:\/\/docs.shopware.com\/en\/shopware-platform-dev-en\/internals\/directory-structure. The Purpose of using sales channel is some of the event does not have the sales channel context. So we need to [...]","og_url":"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-predefined-event\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2021-11-19T13:45:33+00:00","article_modified_time":"2021-11-26T12:54:56+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":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-predefined-event\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-predefined-event\/"},"author":{"name":"Diwakar Rana","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/4b025fe4ecbc5c0378cd13bb70da654f"},"headline":"How to get data according to sales channel using predefined event","datePublished":"2021-11-19T13:45:33+00:00","dateModified":"2021-11-26T12:54:56+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-predefined-event\/"},"wordCount":255,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"articleSection":["Shopware"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-predefined-event\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-predefined-event\/","url":"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-predefined-event\/","name":"How to get data according to sales channel using predefined event - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2021-11-19T13:45:33+00:00","dateModified":"2021-11-26T12:54:56+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-predefined-event\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-predefined-event\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-predefined-event\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to get data according to sales channel using predefined event"}]},{"@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\/312451","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=312451"}],"version-history":[{"count":2,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/312451\/revisions"}],"predecessor-version":[{"id":313289,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/312451\/revisions\/313289"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=312451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=312451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=312451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}