7
7
8
8
namespace Magento \CatalogGraphQl \Model \Resolver ;
9
9
10
+ use Magento \CatalogGraphQl \Model \Resolver \Product \ProductCategories ;
10
11
use Magento \Framework \Exception \LocalizedException ;
11
12
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
12
13
use Magento \Catalog \Api \Data \CategoryInterface ;
18
19
use Magento \Framework \GraphQl \Query \ResolverInterface ;
19
20
use Magento \Framework \GraphQl \Query \Resolver \ValueFactory ;
20
21
use Magento \CatalogGraphQl \Model \Category \Hydrator as CategoryHydrator ;
22
+ use Magento \Store \Model \StoreManagerInterface ;
21
23
22
24
/**
23
25
* Resolver for category objects the product is assigned to.
@@ -56,25 +58,41 @@ class Categories implements ResolverInterface
56
58
*/
57
59
private $ categoryHydrator ;
58
60
61
+ /**
62
+ * @var ProductCategories
63
+ */
64
+ private $ productCategories ;
65
+
66
+ /**
67
+ * @var StoreManagerInterface
68
+ */
69
+ private $ storeManager ;
70
+
59
71
/**
60
72
* @param CollectionFactory $collectionFactory
61
73
* @param AttributesJoiner $attributesJoiner
62
74
* @param CustomAttributesFlattener $customAttributesFlattener
63
75
* @param ValueFactory $valueFactory
64
76
* @param CategoryHydrator $categoryHydrator
77
+ * @param ProductCategories $productCategories
78
+ * @param StoreManagerInterface $storeManager
65
79
*/
66
80
public function __construct (
67
81
CollectionFactory $ collectionFactory ,
68
82
AttributesJoiner $ attributesJoiner ,
69
83
CustomAttributesFlattener $ customAttributesFlattener ,
70
84
ValueFactory $ valueFactory ,
71
- CategoryHydrator $ categoryHydrator
85
+ CategoryHydrator $ categoryHydrator ,
86
+ ProductCategories $ productCategories ,
87
+ StoreManagerInterface $ storeManager
72
88
) {
73
89
$ this ->collection = $ collectionFactory ->create ();
74
90
$ this ->attributesJoiner = $ attributesJoiner ;
75
91
$ this ->customAttributesFlattener = $ customAttributesFlattener ;
76
92
$ this ->valueFactory = $ valueFactory ;
77
93
$ this ->categoryHydrator = $ categoryHydrator ;
94
+ $ this ->productCategories = $ productCategories ;
95
+ $ this ->storeManager = $ storeManager ;
78
96
}
79
97
80
98
/**
@@ -90,39 +108,42 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
90
108
91
109
/** @var \Magento\Catalog\Model\Product $product */
92
110
$ product = $ value ['model ' ];
93
- $ categoryIds = $ product ->getCategoryIds ();
111
+ $ storeId = $ this ->storeManager ->getStore ()->getId ();
112
+ $ categoryIds = $ this ->productCategories ->getCategoryIdsByProduct ((int )$ product ->getId (), (int )$ storeId );
94
113
$ this ->categoryIds = array_merge ($ this ->categoryIds , $ categoryIds );
95
114
$ that = $ this ;
96
115
97
- return $ this ->valueFactory ->create (function () use ($ that , $ categoryIds , $ info ) {
98
- $ categories = [];
99
- if (empty ($ that ->categoryIds )) {
100
- return [];
101
- }
116
+ return $ this ->valueFactory ->create (
117
+ function () use ($ that , $ categoryIds , $ info ) {
118
+ $ categories = [];
119
+ if (empty ($ that ->categoryIds )) {
120
+ return [];
121
+ }
102
122
103
- if (!$ this ->collection ->isLoaded ()) {
104
- $ that ->attributesJoiner ->join ($ info ->fieldNodes [0 ], $ this ->collection );
105
- $ this ->collection ->addIdFilter ($ this ->categoryIds );
106
- }
107
- /** @var CategoryInterface | \Magento\Catalog\Model\Category $item */
108
- foreach ($ this ->collection as $ item ) {
109
- if (in_array ($ item ->getId (), $ categoryIds )) {
110
- // Try to extract all requested fields from the loaded collection data
111
- $ categories [$ item ->getId ()] = $ this ->categoryHydrator ->hydrateCategory ($ item , true );
112
- $ categories [$ item ->getId ()]['model ' ] = $ item ;
113
- $ requestedFields = $ that ->attributesJoiner ->getQueryFields ($ info ->fieldNodes [0 ]);
114
- $ extractedFields = array_keys ($ categories [$ item ->getId ()]);
115
- $ foundFields = array_intersect ($ requestedFields , $ extractedFields );
116
- if (count ($ requestedFields ) === count ($ foundFields )) {
117
- continue ;
118
- }
123
+ if (!$ this ->collection ->isLoaded ()) {
124
+ $ that ->attributesJoiner ->join ($ info ->fieldNodes [0 ], $ this ->collection );
125
+ $ this ->collection ->addIdFilter ($ this ->categoryIds );
126
+ }
127
+ /** @var CategoryInterface | \Magento\Catalog\Model\Category $item */
128
+ foreach ($ this ->collection as $ item ) {
129
+ if (in_array ($ item ->getId (), $ categoryIds )) {
130
+ // Try to extract all requested fields from the loaded collection data
131
+ $ categories [$ item ->getId ()] = $ this ->categoryHydrator ->hydrateCategory ($ item , true );
132
+ $ categories [$ item ->getId ()]['model ' ] = $ item ;
133
+ $ requestedFields = $ that ->attributesJoiner ->getQueryFields ($ info ->fieldNodes [0 ]);
134
+ $ extractedFields = array_keys ($ categories [$ item ->getId ()]);
135
+ $ foundFields = array_intersect ($ requestedFields , $ extractedFields );
136
+ if (count ($ requestedFields ) === count ($ foundFields )) {
137
+ continue ;
138
+ }
119
139
120
- // If not all requested fields were extracted from the collection, start more complex extraction
121
- $ categories [$ item ->getId ()] = $ this ->categoryHydrator ->hydrateCategory ($ item );
140
+ // If not all requested fields were extracted from the collection, start more complex extraction
141
+ $ categories [$ item ->getId ()] = $ this ->categoryHydrator ->hydrateCategory ($ item );
142
+ }
122
143
}
123
- }
124
144
125
- return $ categories ;
126
- });
145
+ return $ categories ;
146
+ }
147
+ );
127
148
}
128
149
}
0 commit comments