{"id":12270,"date":"2021-09-09T01:54:37","date_gmt":"2021-09-09T07:24:37","guid":{"rendered":"https:\/\/www.emizentech.com\/blog\/?p=12270"},"modified":"2023-01-30T11:02:22","modified_gmt":"2023-01-30T11:02:22","slug":"get-product-data-from-repository-using-different-methods-shopware-6","status":"publish","type":"post","link":"https:\/\/multisitelocal.ezxdemo.com\/blog\/get-product-data-from-repository-using-different-methods-shopware-6.html","title":{"rendered":"How To Get Product Data from Repository using Different Methods In Shopware 6"},"content":{"rendered":"<p>First, we create a controller in our custom plugin:<\/p>\n<p>There are two ways to use repository in controller:<\/p>\n<div>\n<ul>\n<li>1. Using container of current controller<\/li>\n<li>2. By sending the repository from services.xml file.<\/li>\n<\/ul>\n<\/div>\n<p>\/custom\/plugins\/PluginName\/src\/Controller<\/p>\n<pre>&lt;?php declare(strict_types=1);\n\nnamespace PluginName\\Controller;\n\nuse Shopware\\Core\\Content\\Cms\\Exception\\PageNotFoundException;\nuse Shopware\\Core\\Content\\Cms\\SalesChannel\\SalesChannelCmsPageLoaderInterface;\nuse Shopware\\Core\\Framework\\DataAbstractionLayer\\EntityRepositoryInterface;\nuse Shopware\\Core\\Framework\\DataAbstractionLayer\\Search\\Criteria;\nuse Shopware\\Core\\Framework\\Routing\\Annotation\\RouteScope;\nuse Shopware\\Core\\System\\SalesChannel\\SalesChannelContext;\nuse Shopware\\Storefront\\Controller\\StorefrontController;\nuse Shopware\\Storefront\\Framework\\Cache\\Annotation\\HttpCache;\nuse Shopware\\Storefront\\Page\\Navigation\\NavigationPage;\nuse Symfony\\Component\\HttpFoundation\\Request;\nuse Symfony\\Component\\HttpFoundation\\Response;\nuse Symfony\\Component\\Routing\\Annotation\\Route;\nuse Shopware\\Core\\Framework\\Context;\nuse Shopware\\Core\\Framework\\DataAbstractionLayer\\Search\\Filter\\EqualsFilter;\nuse Shopware\\Core\\Framework\\DataAbstractionLayer\\Search\\Filter\\EqualsAnyFilter;\n\n\/**\n * @RouteScope(scopes={\"storefront\"})\n *\/\nclass ProductController extends StorefrontController\n{\n\n   private $productCategoryRepository;\n\n     \/**\n     * @var EntityRepositoryInterface\n     *\/\n\n    private $productRepository;\n\n    public function __construct(EntityRepositoryInterface $productRepository)\n    {\n        $this-&gt;productRepository= $productRepository;\n    }\n\n    \/**\n     * @HttpCache()\n     * @Route(\"\/products\", name=\"frontend.products.detail\", methods={\"GET\"})\n     *\/\n    public function detailAction(Request $request, SalesChannelContext $context, Context $con): Response\n{\n\n        $criteria1 = new Criteria();\n$criteria1-&gt;addAssociations(['media', 'properties', 'properties.group', 'unit', 'tax']);\n        \/\/ using reference of repository sent from services.xml\n        $products = $this-&gt;productRepository-&gt;search($criteria1, $con)-&gt;getEntities();\n        \n        $productIds;\n        foreach($products as $key =&gt; $p){\n            foreach($p as $k =&gt; $pr){\n                    $productIds[] = $pr;\n            }\n        }\n        \n        \/\/ using current class reference controller\n        $this-&gt;productPriceRepository = $this-&gt;container-&gt;get('product_price.repository');\n\n        $criteria2 = new Criteria();\n        $productPrice = $this-&gt;productPriceRepository-&gt;search($criteria2, $con);\n        \n        return $this-&gt;renderStorefront('@Storefront\/storefront\/page\/content\/index.html.twig', [\n            'products' =&gt; $products,\n            'productPrice' =&gt; $productPrice,\n        ]);\n    }\n}\n<\/pre>\n<p>Then you need to create routes.xml file:<\/p>\n<p>\/custom\/plugins\/PluginName\/src\/Resources\/config\/routes.xml<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"UTF-8\" ?&gt;\n\n&lt;routes xmlns=\"http:\/\/symfony.com\/schema\/routing\"\n        xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:schemaLocation=\"http:\/\/symfony.com\/schema\/routing\n        http:\/\/symfony.com\/schema\/routing\/routing-1.0.xsd\"&gt;\n\n    &lt;import resource=\"..\/..\/Controller\" type=\"annotation\" \/&gt;\n&lt;\/routes&gt;\n<\/pre>\n<p>Then Next you need to create services.xml<\/p>\n<p>\/custom\/plugins\/PluginName\/src\/Resources\/config\/services.xml<\/p>\n<pre>&lt;?xml version=\"1.0\" ?&gt;\n&lt;container xmlns=\"http:\/\/symfony.com\/schema\/dic\/services\"\n           xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n           xsi:schemaLocation=\"http:\/\/symfony.com\/schema\/dic\/services http:\/\/symfony.com\/schema\/dic\/services\/services-1.0.xsd\"&gt;\n    &lt;services&gt;\n        &lt;service id=\"PluginName\\Controller\\ProductController\" public=\"true\"&gt;\n            &lt;argument type=\"service\" id=\"product.repository\"\/&gt;\n            &lt;call method=\"setContainer\"&gt;\n                &lt;argument type=\"service\" id=\"service_container\"\/&gt;\n            &lt;\/call&gt;\n        &lt;\/service&gt;\n    &lt;\/services&gt;\n&lt;\/container&gt;\n\n<\/pre>\n<p>Here I am creating two pages to show the product data on html.twig file<br>\ncustom\/plugins\/PluginName\/src\/Resources\/views\/storefront\/page\/content\/index.html.twig<\/p>\n<pre>{% sw_extends '@Storefront\/storefront\/page\/content\/index.html.twig' %}\n\n{% block base_main_inner %}\n    &lt;div class=\"container-main\"&gt;\n        {% block page_content %}\n            {% block cms_content %}\n                &lt;div class=\"cms-page\"&gt;\n                    {% block page_content_blocks %}\n                        {% sw_include \"@Storefront\/storefront\/page\/content\/product_detail.html.twig\" %}\n                    {% endblock %}\n                &lt;\/div&gt;\n            {% endblock %}\n        {% endblock %}\n    &lt;\/div&gt;\n{% endblock %}\n\n<\/pre>\n<p>custom\/plugins\/PluginName\/src\/Resources\/views\/storefront\/page\/content\/product_detail.html.twig<\/p>\n<p>This code will show the Product detail on the page I have only 3 products you can show more products according to your need:<\/p>\n<pre>&lt;div class=\"cms-block product-listing-part pos-1 cms-block-product-three-column section-products\" style=\"\"&gt;\n    &lt;div class=\"cms-block-container\" style=\"padding: 20px 20px 20px 20px;\"&gt;\n        &lt;div class=\"cms-block-container-row row cms-row \"&gt;\n            {% set i = 0 %}\n            \n            {% for product in products %}\n                {% if i &lt;= 2 %}\n                {% set i = (i+1) %}\n                &lt;div class=\"col-md-4 card-col\" data-cms-element-id=\"d3c7873569b844239e5756f6a89c1f7f\"&gt;\n                    &lt;div class=\"cms-element-product-box\"&gt;\n                        &lt;div class=\"card product-box box-minimal\"&gt;\n                            &lt;div class=\"card-body\"&gt;\n                                &lt;div class=\"product-badges\"&gt;&lt;\/div&gt;\n                                    &lt;meta itemprop=\"brand\" content=\"\"&gt;\n                                    &lt;meta itemprop=\"mpn\" content=\"SW10011\"&gt;\n                                    &lt;div class=\"product-image-wrapper\"&gt;\n                                        &lt;a href=\"{{ seoUrl('frontend.detail.page', {'productId': product.id}) }}\" title=\"AIX Ros\u00e9 Copy\" class=\"product-image-link is-standard\" \"=\"\"&gt;\n                                            \n                                            {% for element in product.media.elements %}\n                                                {% if element.id == product.coverId %}\n                                                    &lt;img src=\"{{element.media.url}}\" class=\"product-image is-standard\" alt=\"{{product.translated.name}}\" title=\"{{product.translated.name}}\"&gt;\n                                                {%endif%}\n                                            {% endfor %}\n                                        &lt;\/a&gt;\n                                    &lt;\/div&gt;\n\n                                    &lt;div class=\"product-info\"&gt;\n                                        &lt;span class=\"product-subtext\"&gt;\n                                            {% set comma = null %}{% for element in product.properties.elements %}{{(comma ~ ' ' ~ element.translated.name)|trim|trans}}{% set comma=','%}{% endfor %}\n                                        &lt;\/span&gt;\n\n                                        &lt;a href=\"{{ seoUrl('frontend.detail.page', {'productId': product.id}) }}\" class=\"product-name\" title=\"{{product.translated.name}}\"&gt;{{product.translated.name}}&lt;\/a&gt;\n                                        &lt;div class=\"product-price-info\"&gt;\n\n                                            &lt;div class=\"product-price-wrapper\"&gt;\n                                                &lt;span class=\"product-price\"&gt;\n                                                    {% set listPrice = 0 %}\n                                                    {% for element in product.cheapestPrice.price.elements %}\n                                                        {% if element.listPrice != null %}\n                                                            {{element.listPrice.net|currency}}{{ \"general.star\"|trans|sw_sanitize }}\n                                                            {% set listPrice = element.listPrice.net %}\n                                                        {%endif%}\n                                                    {% endfor %}\n                                                &lt;\/span&gt;\n                                            &lt;\/div&gt;\n                                        &lt;br&gt;\n                                        &lt;p class=\"product-price-unit\"&gt;\n                                            &lt;span class=\"price-unit-content\"&gt;\n                                                {{product.purchaseUnit}} {{product.unit.translated.name}}\n                                                {# for element in product.cheapestPriceContainer.value #}\n                                                    {#element.default.purchase_unit#}\n                                                {# endfor #}\n                                            &lt;\/span&gt;\n\n                                            &lt;span class=\"price-unit-reference\"&gt;\n                                                {% set pricePerUnit = (((listPrice\/product.purchaseUnit)*product.referenceUnit)|number_format(2))*1 %}\n\n                                                ({{pricePerUnit|currency }} {{ \"general.star\"|trans|sw_sanitize }} \/ {{product.referenceUnit}} {{product.unit.translated.name}})\n                                            &lt;\/span&gt;\n                                            &lt;br&gt;\n                                             {% set taxText = \"general.grossTaxInformation\"|trans|sw_sanitize %}\n                                             {% if taxText == null or taxText == ''%}\n                                             {% set taxText = \"general.netTaxInformation\"|trans|sw_sanitize %}\n                                             {% endif %}\n                                            &lt;span class=\"product-detail-tax\"&gt;\n                                                {{taxText}}\n                                            &lt;\/span&gt;\n                                        &lt;\/p&gt;\n                                    &lt;\/div&gt;\n\n\n                                    \n\n                                    &lt;input type=\"hidden\" name=\"product-name\" value=\"{{product.translated.name}}\"&gt;\n                                    &lt;input type=\"hidden\" name=\"product-id\" value=\"{{product.id}}\"&gt;\n                                &lt;\/div&gt;\n                            &lt;\/div&gt;\n                        &lt;\/div&gt;\n                    &lt;\/div&gt;\n                &lt;\/div&gt;\n                {% endif %}\n            {% endfor %}\n            \n        &lt;\/div&gt;\n    &lt;\/div&gt;\n&lt;\/div&gt;\n<\/pre>\n\n","protected":false},"excerpt":{"rendered":"<p>First, we create a controller in our custom plugin: There are two ways to use repository in controller: 1. Using container of current controller 2. By sending the repository from services.xml file. \/custom\/plugins\/PluginName\/src\/Controller &lt;?php declare(strict_types=1); namespace PluginName\\Controller; use Shopware\\Core\\Content\\Cms\\Exception\\PageNotFoundException; use Shopware\\Core\\Content\\Cms\\SalesChannel\\SalesChannelCmsPageLoaderInterface; use Shopware\\Core\\Framework\\DataAbstractionLayer\\EntityRepositoryInterface; use Shopware\\Core\\Framework\\DataAbstractionLayer\\Search\\Criteria; use Shopware\\Core\\Framework\\Routing\\Annotation\\RouteScope; use Shopware\\Core\\System\\SalesChannel\\SalesChannelContext; use Shopware\\Storefront\\Controller\\StorefrontController; use Shopware\\Storefront\\Framework\\Cache\\Annotation\\HttpCache; use Shopware\\Storefront\\Page\\Navigation\\NavigationPage;<\/p>\n","protected":false},"author":36,"featured_media":12271,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"MSN_Categories":"Uncategorized","MSN_Publish_Option":false,"MSN_Is_Local_News":false,"MSN_Is_AIAC_Included":"Empty","MSN_Location":"[]","MSN_Add_Feature_Img_On_Top_Of_Post":false,"MSN_Has_Custom_Author":false,"MSN_Custom_Author":"","MSN_Has_Custom_Canonical_Url":false,"MSN_Custom_Canonical_Url":"","_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[85],"tags":[84],"class_list":{"0":"post-12270","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-shopware","8":"tag-shopware"},"modified_by":"Marketing EmizenTech","featured_image_src":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-content\/uploads\/sites\/2\/2021\/09\/Get-Product-Data-from-Repository-using-Different-Method-1.jpg","featured_image_src_square":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-content\/uploads\/sites\/2\/2021\/09\/Get-Product-Data-from-Repository-using-Different-Method-1.jpg","author_info":{"display_name":"Vivek Khatri","author_link":"https:\/\/multisitelocal.ezxdemo.com\/blog\/author\/vivek"},"_links":{"self":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts\/12270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/users\/36"}],"replies":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/comments?post=12270"}],"version-history":[{"count":0,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts\/12270\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/media\/12271"}],"wp:attachment":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/media?parent=12270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/categories?post=12270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/tags?post=12270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}