11<?php
22/**
3- * Copyright © Magento, Inc. All rights reserved.
4- * See COPYING.txt for license details .
3+ * Copyright 2019 Adobe
4+ * All Rights Reserved .
55 */
66declare (strict_types=1 );
77
88namespace Magento \RelatedProductGraphQl \Model \Resolver \Batch ;
99
10+ use Magento \Catalog \Api \Data \ProductInterface ;
1011use Magento \CatalogGraphQl \Model \Resolver \Product \ProductFieldsSelector ;
12+ use Magento \CatalogGraphQl \Model \Resolver \Products \DataProvider \Product as ProductDataProvider ;
13+ use Magento \Framework \Api \SearchCriteriaBuilder ;
14+ use Magento \Framework \App \ObjectManager ;
1115use Magento \Framework \Exception \LocalizedException ;
1216use Magento \Framework \GraphQl \Config \Element \Field ;
17+ use Magento \Framework \GraphQl \Query \Resolver \BatchRequestItemInterface ;
1318use Magento \Framework \GraphQl \Query \Resolver \BatchResolverInterface ;
1419use Magento \Framework \GraphQl \Query \Resolver \BatchResponse ;
1520use Magento \Framework \GraphQl \Query \Resolver \ContextInterface ;
1621use Magento \RelatedProductGraphQl \Model \DataProvider \RelatedProductDataProvider ;
17- use Magento \CatalogGraphQl \Model \Resolver \Products \DataProvider \Product as ProductDataProvider ;
18- use Magento \Framework \Api \SearchCriteriaBuilder ;
22+ use Magento \RelatedProductGraphQl \Model \ResourceModel \RelatedProductsByStoreId ;
1923
2024/**
2125 * Resolve linked product lists.
@@ -42,22 +46,31 @@ abstract class AbstractLikedProducts implements BatchResolverInterface
4246 */
4347 private $ searchCriteriaBuilder ;
4448
49+ /**
50+ * @var RelatedProductsByStoreId
51+ */
52+ private $ relatedProductsByStoreId ;
53+
4554 /**
4655 * @param ProductFieldsSelector $productFieldsSelector
4756 * @param RelatedProductDataProvider $relatedProductDataProvider
4857 * @param ProductDataProvider $productDataProvider
4958 * @param SearchCriteriaBuilder $searchCriteriaBuilder
59+ * @param RelatedProductsByStoreId|null $relatedProductsByStoreId
5060 */
5161 public function __construct (
5262 ProductFieldsSelector $ productFieldsSelector ,
5363 RelatedProductDataProvider $ relatedProductDataProvider ,
5464 ProductDataProvider $ productDataProvider ,
55- SearchCriteriaBuilder $ searchCriteriaBuilder
65+ SearchCriteriaBuilder $ searchCriteriaBuilder ,
66+ ?RelatedProductsByStoreId $ relatedProductsByStoreId = null
5667 ) {
5768 $ this ->productFieldsSelector = $ productFieldsSelector ;
5869 $ this ->relatedProductDataProvider = $ relatedProductDataProvider ;
5970 $ this ->productDataProvider = $ productDataProvider ;
6071 $ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ;
72+ $ this ->relatedProductsByStoreId = $ relatedProductsByStoreId ??
73+ ObjectManager::getInstance ()->get (RelatedProductsByStoreId::class);
6174 }
6275
6376 /**
@@ -77,29 +90,39 @@ abstract protected function getLinkType(): int;
7790 /**
7891 * Find related products.
7992 *
80- * @param \Magento\Catalog\Api\Data\ ProductInterface[] $products
93+ * @param ProductInterface[] $products
8194 * @param string[] $loadAttributes
8295 * @param int $linkType
83- * @return \Magento\Catalog\Api\Data\ProductInterface[][]
96+ * @param string $websiteId
97+ * @return ProductInterface[][]
98+ * @throws LocalizedException
8499 */
85- private function findRelations (array $ products , array $ loadAttributes , int $ linkType ): array
86- {
100+ private function findRelations (
101+ array $ products ,
102+ array $ loadAttributes ,
103+ int $ linkType ,
104+ string $ websiteId
105+ ): array {
87106 //Loading relations
88107 $ relations = $ this ->relatedProductDataProvider ->getRelations ($ products , $ linkType );
89108 if (!$ relations ) {
90109 return [];
91110 }
92- $ relatedIds = array_unique (array_merge ([], ...array_values ($ relations )));
111+ //get related product ids by website id
112+ $ relatedIds = $ this ->relatedProductsByStoreId ->execute (
113+ array_unique (array_merge ([], ...array_values ($ relations ))),
114+ $ websiteId
115+ );
93116 //Loading products data.
94117 $ this ->searchCriteriaBuilder ->addFilter ('entity_id ' , $ relatedIds , 'in ' );
95118 $ relatedSearchResult = $ this ->productDataProvider ->getList (
96119 $ this ->searchCriteriaBuilder ->create (),
97120 $ loadAttributes
98121 );
99122 //Filling related products map.
100- /** @var \Magento\Catalog\Api\Data\ ProductInterface[] $relatedProducts */
123+ /** @var ProductInterface[] $relatedProducts */
101124 $ relatedProducts = [];
102- /** @var \Magento\Catalog\Api\Data\ ProductInterface $item */
125+ /** @var ProductInterface $item */
103126 foreach ($ relatedSearchResult ->getItems () as $ item ) {
104127 $ relatedProducts [$ item ->getId ()] = $ item ;
105128 }
@@ -127,10 +150,10 @@ function ($id) use ($relatedProducts) {
127150 */
128151 public function resolve (ContextInterface $ context , Field $ field , array $ requests ): BatchResponse
129152 {
130- /** @var \Magento\Catalog\Api\Data\ ProductInterface[] $products */
153+ /** @var ProductInterface[] $products */
131154 $ products = [];
132155 $ fields = [];
133- /** @var \Magento\Framework\GraphQl\Query\Resolver\ BatchRequestItemInterface $request */
156+ /** @var BatchRequestItemInterface $request */
134157 foreach ($ requests as $ request ) {
135158 //Gathering fields and relations to load.
136159 if (empty ($ request ->getValue ()['model ' ])) {
@@ -141,14 +164,16 @@ public function resolve(ContextInterface $context, Field $field, array $requests
141164 }
142165 $ fields = array_unique (array_merge ([], ...$ fields ));
143166
167+ $ store = $ context ->getExtensionAttributes ()->getStore ();
168+ $ websiteId = $ store ->getWebsiteId ();
144169 //Finding relations.
145- $ related = $ this ->findRelations ($ products , $ fields , $ this ->getLinkType ());
170+ $ related = $ this ->findRelations ($ products , $ fields , $ this ->getLinkType (), ( string ) $ websiteId );
146171
147172 //Matching requests with responses.
148173 $ response = new BatchResponse ();
149- /** @var \Magento\Framework\GraphQl\Query\Resolver\ BatchRequestItemInterface $request */
174+ /** @var BatchRequestItemInterface $request */
150175 foreach ($ requests as $ request ) {
151- /** @var \Magento\Catalog\Api\Data\ ProductInterface $product */
176+ /** @var ProductInterface $product */
152177 $ product = $ request ->getValue ()['model ' ];
153178 $ result = [];
154179 if (array_key_exists ($ product ->getId (), $ related )) {
0 commit comments