14
14
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
15
15
use Magento \InventoryCatalog \Model \GetStockIdForCurrentWebsite ;
16
16
use Magento \InventorySalesApi \Api \AreProductsSalableInterface ;
17
- use Magento \Catalog \Model \Product \Type ;
17
+ use Magento \Bundle \Model \Product \Type ;
18
18
19
19
/**
20
20
* @inheritdoc
21
21
*/
22
22
class StockStatusProvider implements ResolverInterface
23
23
{
24
- /**
25
- * @var GetStockIdForCurrentWebsite
26
- */
27
- private $ getStockIdForCurrentWebsite ;
28
-
29
- /**
30
- * @var AreProductsSalableInterface|null
31
- */
32
- private $ areProductsSalable ;
24
+ private const IN_STOCK = "IN_STOCK " ;
25
+ private const OUT_OF_STOCK = "OUT_OF_STOCK " ;
33
26
34
27
/**
35
28
* @param GetStockIdForCurrentWebsite $getStockIdForCurrentWebsite
36
29
* @param AreProductsSalableInterface $areProductsSalable
30
+ * @param Type $bundleType
37
31
*/
38
32
public function __construct (
39
- GetStockIdForCurrentWebsite $ getStockIdForCurrentWebsite ,
40
- AreProductsSalableInterface $ areProductsSalable
33
+ private readonly GetStockIdForCurrentWebsite $ getStockIdForCurrentWebsite ,
34
+ private readonly AreProductsSalableInterface $ areProductsSalable ,
35
+ private readonly Type $ bundleType
41
36
) {
42
- $ this ->getStockIdForCurrentWebsite = $ getStockIdForCurrentWebsite ;
43
- $ this ->areProductsSalable = $ areProductsSalable ;
44
37
}
45
38
46
39
/**
@@ -52,15 +45,25 @@ public function resolve(Field $field, $context, ResolveInfo $info, ?array $value
52
45
throw new LocalizedException (__ ('"model" value should be specified ' ));
53
46
}
54
47
55
- /* @var $product ProductInterface */
56
48
$ product = $ value ['model ' ];
57
49
58
- $ productSku = ($ product ->getTypeId () === TYPE ::TYPE_BUNDLE || !empty ($ product ->getOptions ()))
59
- ? $ value ['sku ' ] : $ product ->getSku ();
50
+ if ($ product ->getTypeId () === Type::TYPE_CODE ) {
51
+ try {
52
+ if (!$ product ->getCustomOption ('bundle_selection_ids ' )) {
53
+ return self ::OUT_OF_STOCK ;
54
+ }
55
+ $ this ->bundleType ->checkProductBuyState ($ product );
56
+ } catch (LocalizedException $ e ) {
57
+ return self ::OUT_OF_STOCK ;
58
+ }
59
+
60
+ return self ::IN_STOCK ;
61
+ }
62
+
63
+ $ productSku = (!empty ($ product ->getOptions ())) ? $ value ['sku ' ] : $ product ->getSku ();
60
64
$ stockId = $ this ->getStockIdForCurrentWebsite ->execute ();
61
65
$ result = $ this ->areProductsSalable ->execute ([$ productSku ], $ stockId );
62
- $ result = current ($ result );
63
66
64
- return $ result ->isSalable () ? ' IN_STOCK ' : ' OUT_OF_STOCK ' ;
67
+ return current ( $ result) ->isSalable () ? self :: IN_STOCK : self :: OUT_OF_STOCK ;
65
68
}
66
69
}
0 commit comments