8
8
namespace Magento \CatalogInventoryGraphQl \Model \Resolver ;
9
9
10
10
use Magento \Catalog \Api \Data \ProductInterface ;
11
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
11
12
use Magento \CatalogInventory \Api \Data \StockStatusInterface ;
12
13
use Magento \CatalogInventory \Api \StockStatusRepositoryInterface ;
13
14
use Magento \Framework \Exception \LocalizedException ;
15
+ use Magento \Framework \Exception \NoSuchEntityException ;
14
16
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
15
17
use Magento \Framework \GraphQl \Config \Element \Field ;
16
18
use Magento \Framework \GraphQl \Query \ResolverInterface ;
19
+ use Magento \Quote \Model \Quote \Item ;
17
20
18
21
/**
19
22
* @inheritdoc
20
23
*/
21
24
class StockStatusProvider implements ResolverInterface
22
25
{
23
26
/**
24
- * @var StockStatusRepositoryInterface
27
+ * Bundle product type code
25
28
*/
26
- private $ stockStatusRepository ;
29
+ private const PRODUCT_TYPE_BUNDLE = " bundle " ;
27
30
28
31
/**
32
+ * Configurable product type code
33
+ */
34
+ private const PRODUCT_TYPE_CONFIGURABLE = "configurable " ;
35
+
36
+ /**
37
+ * In Stock return code
38
+ */
39
+ private const IN_STOCK = "IN_STOCK " ;
40
+
41
+ /**
42
+ * Out of Stock return code
43
+ */
44
+ private const OUT_OF_STOCK = "OUT_OF_STOCK " ;
45
+
46
+ /**
47
+ * StockStatusProvider Constructor
48
+ *
29
49
* @param StockStatusRepositoryInterface $stockStatusRepository
50
+ * @param ProductRepositoryInterface $productRepositoryInterface
30
51
*/
31
- public function __construct (StockStatusRepositoryInterface $ stockStatusRepository )
32
- {
33
- $ this ->stockStatusRepository = $ stockStatusRepository ;
52
+ public function __construct (
53
+ private readonly StockStatusRepositoryInterface $ stockStatusRepository ,
54
+ private readonly ProductRepositoryInterface $ productRepositoryInterface ,
55
+ ) {
34
56
}
35
57
36
58
/**
@@ -41,13 +63,58 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
41
63
if (!array_key_exists ('model ' , $ value ) || !$ value ['model ' ] instanceof ProductInterface) {
42
64
throw new LocalizedException (__ ('"model" value should be specified ' ));
43
65
}
66
+ /** @var Item $cartItem */
67
+ $ cartItem = $ value ['cart_item ' ] ?? [];
68
+ if (!$ cartItem instanceof Item) {
69
+ $ product = $ value ['model ' ];
70
+ $ stockStatus = $ this ->stockStatusRepository ->get ($ product ->getId ());
44
71
45
- /* @var $product ProductInterface */
46
- $ product = $ value ['model ' ];
72
+ return ((int )$ stockStatus ->getStockStatus ()) ? self ::IN_STOCK : self ::OUT_OF_STOCK ;
73
+ }
74
+
75
+ if ($ cartItem ->getProductType () === self ::PRODUCT_TYPE_BUNDLE ) {
76
+ return $ this ->getBundleProductStockStatus ($ cartItem );
77
+ }
47
78
79
+ $ product = $ this ->getVariantProduct ($ cartItem ) ?? $ cartItem ->getProduct ();
48
80
$ stockStatus = $ this ->stockStatusRepository ->get ($ product ->getId ());
49
- $ productStockStatus = (int )$ stockStatus ->getStockStatus ();
50
81
51
- return $ productStockStatus === StockStatusInterface::STATUS_IN_STOCK ? 'IN_STOCK ' : 'OUT_OF_STOCK ' ;
82
+ return ((int )$ stockStatus ->getStockStatus ()) ? self ::IN_STOCK : self ::OUT_OF_STOCK ;
83
+ }
84
+
85
+ /**
86
+ * Get stock status of added bundle options
87
+ *
88
+ * @param Item $cartItem
89
+ * @return string
90
+ */
91
+ private function getBundleProductStockStatus (Item $ cartItem ): string
92
+ {
93
+ $ qtyOptions = $ cartItem ->getQtyOptions ();
94
+ foreach ($ qtyOptions as $ qtyOption ) {
95
+ $ stockStatus = $ this ->stockStatusRepository ->get ($ qtyOption ->getProduct ()->getId ());
96
+ if (!(int )$ stockStatus ->getStockStatus ()) {
97
+ return self ::OUT_OF_STOCK ;
98
+ }
99
+ }
100
+
101
+ return self ::IN_STOCK ;
102
+ }
103
+
104
+ /**
105
+ * Returns variant product if available
106
+ *
107
+ * @param Item $cartItem
108
+ * @return ProductInterface|null
109
+ * @throws NoSuchEntityException
110
+ */
111
+ private function getVariantProduct (Item $ cartItem ): ?ProductInterface
112
+ {
113
+ if ($ cartItem ->getProductType () === self ::PRODUCT_TYPE_CONFIGURABLE ) {
114
+ if ($ cartItem ->getChildren ()[0 ] !== null ) {
115
+ return $ this ->productRepositoryInterface ->get ($ cartItem ->getSku ());
116
+ }
117
+ }
118
+ return null ;
52
119
}
53
120
}
0 commit comments