1
1
<?php
2
2
/**
3
- * Copyright © Magento, Inc. All rights reserved.
4
- * See COPYING.txt for license details .
3
+ * Copyright 2019 Adobe
4
+ * All Rights Reserved .
5
5
*/
6
6
declare (strict_types=1 );
7
7
8
8
namespace Magento \RelatedProductGraphQl \Model \Resolver \Batch ;
9
9
10
+ use Magento \Catalog \Api \Data \ProductInterface ;
10
11
use 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 ;
11
15
use Magento \Framework \Exception \LocalizedException ;
12
16
use Magento \Framework \GraphQl \Config \Element \Field ;
17
+ use Magento \Framework \GraphQl \Query \Resolver \BatchRequestItemInterface ;
13
18
use Magento \Framework \GraphQl \Query \Resolver \BatchResolverInterface ;
14
19
use Magento \Framework \GraphQl \Query \Resolver \BatchResponse ;
15
20
use Magento \Framework \GraphQl \Query \Resolver \ContextInterface ;
16
21
use 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 ;
19
23
20
24
/**
21
25
* Resolve linked product lists.
@@ -42,22 +46,31 @@ abstract class AbstractLikedProducts implements BatchResolverInterface
42
46
*/
43
47
private $ searchCriteriaBuilder ;
44
48
49
+ /**
50
+ * @var RelatedProductsByStoreId
51
+ */
52
+ private $ relatedProductsByStoreId ;
53
+
45
54
/**
46
55
* @param ProductFieldsSelector $productFieldsSelector
47
56
* @param RelatedProductDataProvider $relatedProductDataProvider
48
57
* @param ProductDataProvider $productDataProvider
49
58
* @param SearchCriteriaBuilder $searchCriteriaBuilder
59
+ * @param RelatedProductsByStoreId|null $relatedProductsByStoreId
50
60
*/
51
61
public function __construct (
52
62
ProductFieldsSelector $ productFieldsSelector ,
53
63
RelatedProductDataProvider $ relatedProductDataProvider ,
54
64
ProductDataProvider $ productDataProvider ,
55
- SearchCriteriaBuilder $ searchCriteriaBuilder
65
+ SearchCriteriaBuilder $ searchCriteriaBuilder ,
66
+ ?RelatedProductsByStoreId $ relatedProductsByStoreId = null
56
67
) {
57
68
$ this ->productFieldsSelector = $ productFieldsSelector ;
58
69
$ this ->relatedProductDataProvider = $ relatedProductDataProvider ;
59
70
$ this ->productDataProvider = $ productDataProvider ;
60
71
$ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ;
72
+ $ this ->relatedProductsByStoreId = $ relatedProductsByStoreId ??
73
+ ObjectManager::getInstance ()->get (RelatedProductsByStoreId::class);
61
74
}
62
75
63
76
/**
@@ -77,29 +90,39 @@ abstract protected function getLinkType(): int;
77
90
/**
78
91
* Find related products.
79
92
*
80
- * @param \Magento\Catalog\Api\Data\ ProductInterface[] $products
93
+ * @param ProductInterface[] $products
81
94
* @param string[] $loadAttributes
82
95
* @param int $linkType
83
- * @return \Magento\Catalog\Api\Data\ProductInterface[][]
96
+ * @param string $websiteId
97
+ * @return ProductInterface[][]
98
+ * @throws LocalizedException
84
99
*/
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 {
87
106
//Loading relations
88
107
$ relations = $ this ->relatedProductDataProvider ->getRelations ($ products , $ linkType );
89
108
if (!$ relations ) {
90
109
return [];
91
110
}
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
+ );
93
116
//Loading products data.
94
117
$ this ->searchCriteriaBuilder ->addFilter ('entity_id ' , $ relatedIds , 'in ' );
95
118
$ relatedSearchResult = $ this ->productDataProvider ->getList (
96
119
$ this ->searchCriteriaBuilder ->create (),
97
120
$ loadAttributes
98
121
);
99
122
//Filling related products map.
100
- /** @var \Magento\Catalog\Api\Data\ ProductInterface[] $relatedProducts */
123
+ /** @var ProductInterface[] $relatedProducts */
101
124
$ relatedProducts = [];
102
- /** @var \Magento\Catalog\Api\Data\ ProductInterface $item */
125
+ /** @var ProductInterface $item */
103
126
foreach ($ relatedSearchResult ->getItems () as $ item ) {
104
127
$ relatedProducts [$ item ->getId ()] = $ item ;
105
128
}
@@ -127,10 +150,10 @@ function ($id) use ($relatedProducts) {
127
150
*/
128
151
public function resolve (ContextInterface $ context , Field $ field , array $ requests ): BatchResponse
129
152
{
130
- /** @var \Magento\Catalog\Api\Data\ ProductInterface[] $products */
153
+ /** @var ProductInterface[] $products */
131
154
$ products = [];
132
155
$ fields = [];
133
- /** @var \Magento\Framework\GraphQl\Query\Resolver\ BatchRequestItemInterface $request */
156
+ /** @var BatchRequestItemInterface $request */
134
157
foreach ($ requests as $ request ) {
135
158
//Gathering fields and relations to load.
136
159
if (empty ($ request ->getValue ()['model ' ])) {
@@ -141,14 +164,16 @@ public function resolve(ContextInterface $context, Field $field, array $requests
141
164
}
142
165
$ fields = array_unique (array_merge ([], ...$ fields ));
143
166
167
+ $ store = $ context ->getExtensionAttributes ()->getStore ();
168
+ $ websiteId = $ store ->getWebsiteId ();
144
169
//Finding relations.
145
- $ related = $ this ->findRelations ($ products , $ fields , $ this ->getLinkType ());
170
+ $ related = $ this ->findRelations ($ products , $ fields , $ this ->getLinkType (), ( string ) $ websiteId );
146
171
147
172
//Matching requests with responses.
148
173
$ response = new BatchResponse ();
149
- /** @var \Magento\Framework\GraphQl\Query\Resolver\ BatchRequestItemInterface $request */
174
+ /** @var BatchRequestItemInterface $request */
150
175
foreach ($ requests as $ request ) {
151
- /** @var \Magento\Catalog\Api\Data\ ProductInterface $product */
176
+ /** @var ProductInterface $product */
152
177
$ product = $ request ->getValue ()['model ' ];
153
178
$ result = [];
154
179
if (array_key_exists ($ product ->getId (), $ related )) {
0 commit comments