7
7
namespace Magento \GraphQlCatalog \Model \Resolver \Products \DataProvider ;
8
8
9
9
use Magento \Catalog \Api \ProductRepositoryInterface ;
10
+ use Magento \Catalog \Model \Product \Option ;
11
+ use Magento \Catalog \Model \Product \TierPrice ;
10
12
use Magento \Framework \Api \SearchCriteriaInterface ;
11
13
use Magento \Framework \Data \SearchResultInterface ;
12
14
use Magento \Framework \Exception \NoSuchEntityException ;
23
25
*/
24
26
class Product
25
27
{
26
- /**
27
- * @var ProductRepositoryInterface
28
- */
29
- private $ productRepository ;
30
-
31
28
/**
32
29
* @var ServiceOutputProcessor
33
30
*/
@@ -64,83 +61,30 @@ class Product
64
61
private $ searchResultsFactory ;
65
62
66
63
/**
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
-
76
- /**
77
- * @param ProductRepositoryInterface $productRepository
78
64
* @param ServiceOutputProcessor $serviceOutputProcessor
79
65
* @param MediaGalleryEntries $mediaGalleryResolver
80
66
* @param SerializerInterface $jsonSerializer
81
67
* @param CollectionFactory $collectionFactory
82
68
* @param JoinProcessorInterface $joinProcessor
83
69
* @param CollectionProcessorInterface $collectionProcessor
84
70
* @param ProductSearchResultsInterfaceFactory $searchResultsFactory
85
- * @param EntityAttributeList $entityAttributeList
86
- * @param \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute\Collection $collection
87
71
*/
88
72
public function __construct (
89
- ProductRepositoryInterface $ productRepository ,
90
73
ServiceOutputProcessor $ serviceOutputProcessor ,
91
74
MediaGalleryEntries $ mediaGalleryResolver ,
92
75
SerializerInterface $ jsonSerializer ,
93
76
CollectionFactory $ collectionFactory ,
94
77
JoinProcessorInterface $ joinProcessor ,
95
78
CollectionProcessorInterface $ collectionProcessor ,
96
- ProductSearchResultsInterfaceFactory $ searchResultsFactory ,
97
- EntityAttributeList $ entityAttributeList ,
98
- \Magento \ConfigurableProduct \Model \ResourceModel \Product \Type \Configurable \Attribute \Collection $ collection
79
+ ProductSearchResultsInterfaceFactory $ searchResultsFactory
99
80
) {
100
- $ this ->productRepository = $ productRepository ;
101
81
$ this ->serviceOutputProcessor = $ serviceOutputProcessor ;
102
82
$ this ->mediaGalleryResolver = $ mediaGalleryResolver ;
103
83
$ this ->jsonSerializer = $ jsonSerializer ;
104
84
$ this ->collectionFactory = $ collectionFactory ;
105
85
$ this ->joinProcessor = $ joinProcessor ;
106
86
$ this ->collectionProcessor = $ collectionProcessor ;
107
87
$ this ->searchResultsFactory = $ searchResultsFactory ;
108
- $ this ->entityAttributeList = $ entityAttributeList ;
109
- $ this ->configurable = $ collection ;
110
- }
111
-
112
- /**
113
- * Get product data by Sku
114
- *
115
- * @param string $sku
116
- * @return array|null
117
- */
118
- public function getProduct (string $ sku )
119
- {
120
- try {
121
- $ productObject = $ this ->productRepository ->get ($ sku );
122
- } catch (NoSuchEntityException $ e ) {
123
- // No error should be thrown, null result should be returned
124
- return null ;
125
- }
126
- return $ this ->processProduct ($ productObject );
127
- }
128
-
129
- /**
130
- * Get product data by Id
131
- *
132
- * @param int $productId
133
- * @return array|null
134
- */
135
- public function getProductById (int $ productId )
136
- {
137
- try {
138
- $ productObject = $ this ->productRepository ->getById ($ productId );
139
- } catch (NoSuchEntityException $ e ) {
140
- // No error should be thrown, null result should be returned
141
- return null ;
142
- }
143
- return $ this ->processProduct ($ productObject );
144
88
}
145
89
146
90
/**
@@ -151,44 +95,42 @@ public function getProductById(int $productId)
151
95
*/
152
96
public function processProduct (\Magento \Catalog \Api \Data \ProductInterface $ productObject )
153
97
{
154
- // $productObject = $this->productRepository->get($productObject->getSku());
155
- $ product = $ this ->serviceOutputProcessor ->process (
156
- $ productObject ,
157
- ProductRepositoryInterface::class,
158
- 'get '
159
- );
160
- if (isset ($ product ['extension_attributes ' ])) {
161
- $ product = array_merge ($ product , $ product ['extension_attributes ' ]);
162
- }
163
- $ customAttributes = [];
164
- if (isset ($ product ['custom_attributes ' ])) {
165
- foreach ($ product ['custom_attributes ' ] as $ attribute ) {
166
- $ isArray = false ;
167
- if (is_array ($ attribute ['value ' ])) {
168
- $ isArray = true ;
169
- foreach ($ attribute ['value ' ] as $ attributeValue ) {
170
- if (is_array ($ attributeValue )) {
171
- $ customAttributes [$ attribute ['attribute_code ' ]] = $ this ->jsonSerializer ->serialize (
172
- $ attribute ['value ' ]
173
- );
174
- continue ;
175
- }
176
- $ customAttributes [$ attribute ['attribute_code ' ]] = implode (', ' , $ attribute ['value ' ]);
177
- continue ;
178
- }
98
+ /** @var \Magento\Catalog\Model\Product $productObject */
99
+ $ productArray = $ productObject ->getData ();
100
+ $ productArray ['id ' ] = $ productArray ['entity_id ' ];
101
+ unset($ productArray ['entity_id ' ]);
102
+ $ productArray ['media_gallery_entries ' ] = $ productObject ->getMediaGalleryEntries ();
103
+ if (isset ($ productArray ['media_gallery_entries ' ])) {
104
+ foreach ($ productArray ['media_gallery_entries ' ] as $ key => $ entry ) {
105
+ if ($ entry ->getExtensionAttributes () && $ entry ->getExtensionAttributes ()->getVideoContent ()) {
106
+ $ productArray ['media_gallery_entries ' ][$ key ]['video_content ' ]
107
+ = $ entry ->getExtensionAttributes ()->getVideoContent ()->getData ();
179
108
}
180
- if ($ isArray ) {
181
- continue ;
109
+ }
110
+ }
111
+ if (isset ($ productArray ['options ' ])) {
112
+ /** @var Option $option */
113
+ foreach ($ productArray ['options ' ] as $ key => $ option ) {
114
+ $ productArray ['options ' ][$ key ] = $ option ->getData ();
115
+ $ productArray ['options ' ][$ key ]['product_sku ' ] = $ option ->getProductSku ();
116
+ $ values = $ option ->getValues () ?: [];
117
+ /** @var Option\Value $value */
118
+ foreach ($ values as $ value ) {
119
+ $ productArray ['options ' ][$ key ]['values ' ][] = $ value ->getData ();
182
120
}
183
- $ customAttributes [$ attribute ['attribute_code ' ]] = $ attribute ['value ' ];
184
121
}
185
122
}
186
- $ product = array_merge ($ product , $ customAttributes );
187
- $ product = array_merge ($ product , $ product ['product_links ' ]);
188
- $ product ['media_gallery_entries ' ]
189
- = $ this ->mediaGalleryResolver ->getMediaGalleryEntries ($ productObject ->getSku ());
123
+ $ tierPrices = $ productObject ->getTierPrices ();
124
+ if ($ tierPrices ) {
125
+ /** @var TierPrice $tierPrice */
126
+ foreach ($ tierPrices as $ tierPrice ) {
127
+ $ productArray ['tier_prices ' ][] = $ tierPrice ->getData ();
128
+ }
129
+ } else {
130
+ $ productArray ['tier_prices ' ] = null ;
131
+ }
190
132
191
- return $ product ;
133
+ return $ productArray ;
192
134
}
193
135
194
136
/**
0 commit comments