{"id":313288,"date":"2021-11-26T13:26:45","date_gmt":"2021-11-26T13:26:45","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=313288"},"modified":"2021-12-30T09:33:31","modified_gmt":"2021-12-30T09:33:31","slug":"how-to-get-data-according-to-sales-channel-using-the-sales-channel-repository","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-the-sales-channel-repository\/","title":{"rendered":"How to get data according to sales channel using the sales channel repository"},"content":{"rendered":"\n<p>In this blog, you are going to learn \u201cHow to get data according to sales channel using the sales channel repository .\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 in the repository is getting data according to sales channel and it save the request time and much fast because it basically add the filter of sales channel in the repository.<\/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; 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;WebkulTest\\Service\\UserService&quot;&gt;\n            &lt;argument type=&quot;service&quot; id=&quot;sales_channel.product.repository&quot;\/&gt;\n        &lt;\/service&gt;\n    &lt;\/services&gt;\n&lt;\/container&gt;<\/pre>\n\n\n\n<p>Create an <code>services.xml<\/code> file, follow the directory structure:- <code>&lt;plugin root&gt;\/src\/Resources\/config\/services.xml<\/code><br> Add the service, in <code>type<\/code> attribute put service and in <code>id<\/code> add sales_channel followed by entity name.<br>This added the <code>product<\/code> entity as sales channel in the service.<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">UserService.php<\/h6>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php declare(strict_types=1);\n\nnamespace WebkulTest\\Service;\n\nuse Shopware\\Core\\Framework\\DataAbstractionLayer\\Search\\Criteria;\nuse Shopware\\Core\\System\\SalesChannel\\Entity\\SalesChannelRepositoryInterface;\nuse Shopware\\Core\\System\\SalesChannel\\SalesChannelContext;\n\nclass UserService \n{\n    \/**\n     * @var SalesChannelRepositoryInterface\n     *\/\n    private $productRepository;\n\n    public function __construct(SalesChannelRepositoryInterface $productRepository)\n    {\n        $this-&gt;productRepository = $productRepository;\n    }\n\n    public function load(SalesChannelContext $context)\n    {\n        $criteria = new Criteria();\n        $criteria-&gt;addAssociation(&#039;cover&#039;);\n        $criteria-&gt;addAssociation(&#039;options.group&#039;);\n        $products = $this-&gt;productRepository-&gt;search($criteria, $context);\n        return $products;\n    }\n}<\/pre>\n\n\n\n<p>Create an service file or add them in any where according to your need.Normally we use <code>EntityRepositoryInterface<\/code> to fetch, write the data but in the case we are using the <code>SalesChannelRepositoryInterface<\/code>. SalesChannelRepositoryInterface have only three function like search, aggregate, searchIds. <\/p>\n\n\n\n<p>It take two parameters, first one is criteria and second is SaleschannelContext whereas EntityRepositoryInterface have more function and it take two parameters, first is criteria and second is Context. Only Difference is in two parameter.<br><\/p>\n\n\n\n<p>SalesChannelRepositoryInterface require the sales channel definition and entity, so also create them by using SalesChannelDefinitionInterface.<br>You can view in the core part, by followed the path of product :- <code>&lt;shopware\/platform\/src\/core\/content\/product\/salesChannel\/SalesChannelProductDefinition<\/code> .<\/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 the sales channel repository .\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 in the repository is getting data according to sales channel and it save <a href=\"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-the-sales-channel-repository\/\">[&#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,1],"tags":[9274],"class_list":["post-313288","post","type-post","status-publish","format-standard","hentry","category-shopware","category-uncategorized","tag-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 the sales channel repository - 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-the-sales-channel-repository\/\" \/>\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 the sales channel repository - 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 the sales channel repository .\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 in the repository is getting data according to sales channel and it save [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-the-sales-channel-repository\/\" \/>\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-26T13:26:45+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-30T09:33:31+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-the-sales-channel-repository\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-the-sales-channel-repository\/\"},\"author\":{\"name\":\"Diwakar Rana\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/4b025fe4ecbc5c0378cd13bb70da654f\"},\"headline\":\"How to get data according to sales channel using the sales channel repository\",\"datePublished\":\"2021-11-26T13:26:45+00:00\",\"dateModified\":\"2021-12-30T09:33:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-the-sales-channel-repository\/\"},\"wordCount\":252,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"Shopware\"],\"articleSection\":[\"Shopware\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-the-sales-channel-repository\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-the-sales-channel-repository\/\",\"url\":\"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-the-sales-channel-repository\/\",\"name\":\"How to get data according to sales channel using the sales channel repository - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2021-11-26T13:26:45+00:00\",\"dateModified\":\"2021-12-30T09:33:31+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-the-sales-channel-repository\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-the-sales-channel-repository\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-the-sales-channel-repository\/#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 the sales channel repository\"}]},{\"@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 the sales channel repository - 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-the-sales-channel-repository\/","og_locale":"en_US","og_type":"article","og_title":"How to get data according to sales channel using the sales channel repository - Webkul Blog","og_description":"In this blog, you are going to learn \u201cHow to get data according to sales channel using the sales channel repository .\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 in the repository is getting data according to sales channel and it save [...]","og_url":"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-the-sales-channel-repository\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2021-11-26T13:26:45+00:00","article_modified_time":"2021-12-30T09:33:31+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-the-sales-channel-repository\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-the-sales-channel-repository\/"},"author":{"name":"Diwakar Rana","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/4b025fe4ecbc5c0378cd13bb70da654f"},"headline":"How to get data according to sales channel using the sales channel repository","datePublished":"2021-11-26T13:26:45+00:00","dateModified":"2021-12-30T09:33:31+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-the-sales-channel-repository\/"},"wordCount":252,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["Shopware"],"articleSection":["Shopware"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-the-sales-channel-repository\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-the-sales-channel-repository\/","url":"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-the-sales-channel-repository\/","name":"How to get data according to sales channel using the sales channel repository - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2021-11-26T13:26:45+00:00","dateModified":"2021-12-30T09:33:31+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-the-sales-channel-repository\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-the-sales-channel-repository\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/how-to-get-data-according-to-sales-channel-using-the-sales-channel-repository\/#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 the sales channel repository"}]},{"@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\/313288","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=313288"}],"version-history":[{"count":2,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/313288\/revisions"}],"predecessor-version":[{"id":318226,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/313288\/revisions\/318226"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=313288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=313288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=313288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}