77
88namespace Magento \CatalogGraphQl \Model \Resolver ;
99
10+ use Magento \CatalogGraphQl \Model \Resolver \Product \ProductCategories ;
1011use Magento \Framework \Exception \LocalizedException ;
1112use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
1213use Magento \Catalog \Api \Data \CategoryInterface ;
1819use Magento \Framework \GraphQl \Query \ResolverInterface ;
1920use Magento \Framework \GraphQl \Query \Resolver \ValueFactory ;
2021use Magento \CatalogGraphQl \Model \Category \Hydrator as CategoryHydrator ;
22+ use Magento \Store \Model \StoreManagerInterface ;
2123
2224/**
2325 * Resolver for category objects the product is assigned to.
@@ -56,25 +58,41 @@ class Categories implements ResolverInterface
5658 */
5759 private $ categoryHydrator ;
5860
61+ /**
62+ * @var ProductCategories
63+ */
64+ private $ productCategories ;
65+
66+ /**
67+ * @var StoreManagerInterface
68+ */
69+ private $ storeManager ;
70+
5971 /**
6072 * @param CollectionFactory $collectionFactory
6173 * @param AttributesJoiner $attributesJoiner
6274 * @param CustomAttributesFlattener $customAttributesFlattener
6375 * @param ValueFactory $valueFactory
6476 * @param CategoryHydrator $categoryHydrator
77+ * @param ProductCategories $productCategories
78+ * @param StoreManagerInterface $storeManager
6579 */
6680 public function __construct (
6781 CollectionFactory $ collectionFactory ,
6882 AttributesJoiner $ attributesJoiner ,
6983 CustomAttributesFlattener $ customAttributesFlattener ,
7084 ValueFactory $ valueFactory ,
71- CategoryHydrator $ categoryHydrator
85+ CategoryHydrator $ categoryHydrator ,
86+ ProductCategories $ productCategories ,
87+ StoreManagerInterface $ storeManager
7288 ) {
7389 $ this ->collection = $ collectionFactory ->create ();
7490 $ this ->attributesJoiner = $ attributesJoiner ;
7591 $ this ->customAttributesFlattener = $ customAttributesFlattener ;
7692 $ this ->valueFactory = $ valueFactory ;
7793 $ this ->categoryHydrator = $ categoryHydrator ;
94+ $ this ->productCategories = $ productCategories ;
95+ $ this ->storeManager = $ storeManager ;
7896 }
7997
8098 /**
@@ -90,39 +108,42 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
90108
91109 /** @var \Magento\Catalog\Model\Product $product */
92110 $ product = $ value ['model ' ];
93- $ categoryIds = $ product ->getCategoryIds ();
111+ $ storeId = $ this ->storeManager ->getStore ()->getId ();
112+ $ categoryIds = $ this ->productCategories ->getCategoryIdsByProduct ((int )$ product ->getId (), (int )$ storeId );
94113 $ this ->categoryIds = array_merge ($ this ->categoryIds , $ categoryIds );
95114 $ that = $ this ;
96115
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+ }
102122
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+ }
119139
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+ }
122143 }
123- }
124144
125- return $ categories ;
126- });
145+ return $ categories ;
146+ }
147+ );
127148 }
128149}
0 commit comments