7
7
namespace Magento \GraphQlCatalog \Model \Resolver \Products \DataProvider ;
8
8
9
9
use Magento \Catalog \Api \ProductRepositoryInterface ;
10
+ use Magento \Framework \Api \SearchCriteriaInterface ;
11
+ use Magento \Framework \Data \SearchResultInterface ;
10
12
use Magento \Framework \Exception \NoSuchEntityException ;
11
13
use Magento \Framework \Serialize \SerializerInterface ;
12
14
use Magento \Framework \Webapi \ServiceOutputProcessor ;
13
- use Magento \GraphQlCatalog \Model \Resolver \Products \DataProvider \MediaGalleryEntries ;
15
+ use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory ;
16
+ use Magento \Framework \Api \ExtensionAttribute \JoinProcessorInterface ;
17
+ use Magento \Framework \Api \SearchCriteria \CollectionProcessorInterface ;
18
+ use Magento \Catalog \Api \Data \ProductSearchResultsInterfaceFactory ;
19
+ use Magento \GraphQl \Model \EntityAttributeList ;
14
20
15
21
/**
16
22
* Product field data provider, used for GraphQL resolver processing.
@@ -37,22 +43,70 @@ class Product
37
43
*/
38
44
private $ jsonSerializer ;
39
45
46
+ /**
47
+ * @var CollectionFactory
48
+ */
49
+ private $ collectionFactory ;
50
+
51
+ /**
52
+ * @var JoinProcessorInterface
53
+ */
54
+ private $ joinProcessor ;
55
+
56
+ /**
57
+ * @var CollectionProcessorInterface
58
+ */
59
+ private $ collectionProcessor ;
60
+
61
+ /**
62
+ * @var ProductSearchResultsInterfaceFactory
63
+ */
64
+ private $ searchResultsFactory ;
65
+
66
+ /**
67
+ * @var EntityAttributeList
68
+ */
69
+ private $ entityAttributeList ;
70
+
71
+ /**
72
+ * @var \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute\Collection
73
+ */
74
+ private $ configurable ;
75
+
40
76
/**
41
77
* @param ProductRepositoryInterface $productRepository
42
78
* @param ServiceOutputProcessor $serviceOutputProcessor
43
79
* @param MediaGalleryEntries $mediaGalleryResolver
44
80
* @param SerializerInterface $jsonSerializer
81
+ * @param CollectionFactory $collectionFactory
82
+ * @param JoinProcessorInterface $joinProcessor
83
+ * @param CollectionProcessorInterface $collectionProcessor
84
+ * @param ProductSearchResultsInterfaceFactory $searchResultsFactory
85
+ * @param EntityAttributeList $entityAttributeList
86
+ * @param \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute\Collection $collection
45
87
*/
46
88
public function __construct (
47
89
ProductRepositoryInterface $ productRepository ,
48
90
ServiceOutputProcessor $ serviceOutputProcessor ,
49
91
MediaGalleryEntries $ mediaGalleryResolver ,
50
- SerializerInterface $ jsonSerializer
92
+ SerializerInterface $ jsonSerializer ,
93
+ CollectionFactory $ collectionFactory ,
94
+ JoinProcessorInterface $ joinProcessor ,
95
+ CollectionProcessorInterface $ collectionProcessor ,
96
+ ProductSearchResultsInterfaceFactory $ searchResultsFactory ,
97
+ EntityAttributeList $ entityAttributeList ,
98
+ \Magento \ConfigurableProduct \Model \ResourceModel \Product \Type \Configurable \Attribute \Collection $ collection
51
99
) {
52
100
$ this ->productRepository = $ productRepository ;
53
101
$ this ->serviceOutputProcessor = $ serviceOutputProcessor ;
54
102
$ this ->mediaGalleryResolver = $ mediaGalleryResolver ;
55
103
$ this ->jsonSerializer = $ jsonSerializer ;
104
+ $ this ->collectionFactory = $ collectionFactory ;
105
+ $ this ->joinProcessor = $ joinProcessor ;
106
+ $ this ->collectionProcessor = $ collectionProcessor ;
107
+ $ this ->searchResultsFactory = $ searchResultsFactory ;
108
+ $ this ->entityAttributeList = $ entityAttributeList ;
109
+ $ this ->configurable = $ collection ;
56
110
}
57
111
58
112
/**
@@ -64,7 +118,7 @@ public function __construct(
64
118
public function getProduct (string $ sku )
65
119
{
66
120
try {
67
- $ productObject = $ this ->productRepository ->get ($ sku, false , null , true );
121
+ $ productObject = $ this ->productRepository ->get ($ sku );
68
122
} catch (NoSuchEntityException $ e ) {
69
123
// No error should be thrown, null result should be returned
70
124
return null ;
@@ -95,8 +149,9 @@ public function getProductById(int $productId)
95
149
* @param \Magento\Catalog\Api\Data\ProductInterface $productObject
96
150
* @return array|null
97
151
*/
98
- private function processProduct (\Magento \Catalog \Api \Data \ProductInterface $ productObject )
152
+ public function processProduct (\Magento \Catalog \Api \Data \ProductInterface $ productObject )
99
153
{
154
+ // $productObject = $this->productRepository->get($productObject->getSku());
100
155
$ product = $ this ->serviceOutputProcessor ->process (
101
156
$ productObject ,
102
157
ProductRepositoryInterface::class,
@@ -133,29 +188,43 @@ private function processProduct(\Magento\Catalog\Api\Data\ProductInterface $prod
133
188
$ product ['media_gallery_entries ' ]
134
189
= $ this ->mediaGalleryResolver ->getMediaGalleryEntries ($ productObject ->getSku ());
135
190
136
- if (isset ($ product ['configurable_product_links ' ])) {
137
- $ product ['configurable_product_links ' ] = $ this
138
- ->resolveConfigurableProductLinks ($ product ['configurable_product_links ' ]);
139
- }
140
-
141
191
return $ product ;
142
192
}
143
193
144
194
/**
145
- * Resolve links for configurable product into simple products
195
+ * Gets list of product data with full data set
146
196
*
147
- * @param int[]
148
- * @return array
197
+ * @param SearchCriteriaInterface $searchCriteria
198
+ * @return SearchResultInterface
149
199
*/
150
- private function resolveConfigurableProductLinks ( $ configurableProductLinks )
200
+ public function getList ( \ Magento \ Framework \ Api \ SearchCriteriaInterface $ searchCriteria )
151
201
{
152
- if (empty ($ configurableProductLinks )) {
153
- return [];
154
- }
155
- $ result = [];
156
- foreach ($ configurableProductLinks as $ key => $ id ) {
157
- $ result [$ key ] = $ this ->getProductById ($ id );
158
- }
159
- return $ result ;
202
+ /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
203
+ $ collection = $ this ->collectionFactory ->create ();
204
+ $ this ->joinProcessor ->process ($ collection );
205
+
206
+ $ collection ->addAttributeToSelect ('* ' );
207
+ $ collection ->joinAttribute ('status ' , 'catalog_product/status ' , 'entity_id ' , null , 'inner ' );
208
+ $ collection ->joinAttribute ('visibility ' , 'catalog_product/visibility ' , 'entity_id ' , null , 'inner ' );
209
+
210
+ $ this ->collectionProcessor ->process ($ searchCriteria , $ collection );
211
+
212
+ $ collection ->load ();
213
+
214
+ $ collection ->addCategoryIds ();
215
+ $ collection ->addFinalPrice ();
216
+ $ collection ->addMediaGalleryData ();
217
+ $ collection ->addMinimalPrice ();
218
+ $ collection ->addPriceData ();
219
+ $ collection ->addWebsiteNamesToResult ();
220
+ $ collection ->addOptionsToResult ();
221
+ $ collection ->addTaxPercents ();
222
+ $ collection ->addWebsiteNamesToResult ();
223
+ $ searchResult = $ this ->searchResultsFactory ->create ();
224
+ $ searchResult ->setSearchCriteria ($ searchCriteria );
225
+ $ searchResult ->setItems ($ collection ->getItems ());
226
+ $ searchResult ->setTotalCount ($ collection ->getSize ());
227
+
228
+ return $ searchResult ;
160
229
}
161
230
}
0 commit comments