9
9
10
10
use Magento \Catalog \Api \Data \ProductInterface ;
11
11
use Magento \Catalog \Api \ProductRepositoryInterface ;
12
- use Magento \CatalogInventory \Api \StockStatusRepositoryInterface ;
13
12
use Magento \Framework \Exception \NoSuchEntityException ;
14
13
use Magento \Quote \Model \Quote \Item ;
15
14
@@ -28,20 +27,13 @@ class ProductStock
28
27
*/
29
28
private const PRODUCT_TYPE_CONFIGURABLE = "configurable " ;
30
29
31
- /**
32
- * Simple product type code
33
- */
34
- private const PRODUCT_TYPE_SIMPLE = "simple " ;
35
-
36
30
/**
37
31
* ProductStock constructor
38
32
*
39
- * @param StockStatusRepositoryInterface $stockStatusRepository
40
33
* @param ProductRepositoryInterface $productRepositoryInterface
41
34
*/
42
35
public function __construct (
43
- private readonly StockStatusRepositoryInterface $ stockStatusRepository ,
44
- private readonly ProductRepositoryInterface $ productRepositoryInterface
36
+ private readonly ProductRepositoryInterface $ productRepositoryInterface ,
45
37
) {
46
38
}
47
39
@@ -57,7 +49,7 @@ public function isProductAvailable(Item $cartItem): bool
57
49
$ requestedQty = 0 ;
58
50
$ previousQty = 0 ;
59
51
/**
60
- * @var ProductInterface $variantProduct
52
+ * @var ProductInterface $variantProduct
61
53
* Configurable products cannot have stock, only its variants can. If the user adds a configurable product
62
54
* using its SKU and the selected options, we need to get the variant it refers to from the quote.
63
55
*/
@@ -81,24 +73,10 @@ public function isProductAvailable(Item $cartItem): bool
81
73
}
82
74
83
75
$ requiredItemQty = $ requestedQty + $ previousQty ;
84
- $ productId = (int ) $ cartItem ->getProduct ()->getId ();
85
76
if ($ variantProduct !== null ) {
86
- $ productId = ( int ) $ variantProduct -> getId ( );
77
+ return $ this -> isStockQtyAvailable ( $ variantProduct , $ requiredItemQty );
87
78
}
88
- return $ this ->isStockAvailable ($ productId , $ requiredItemQty );
89
- }
90
-
91
- /**
92
- * Check if is required product available in stock
93
- *
94
- * @param int $productId
95
- * @param float $requiredQuantity
96
- * @return bool
97
- */
98
- private function isStockAvailable (int $ productId , float $ requiredQuantity ): bool
99
- {
100
- $ stock = $ this ->stockStatusRepository ->get ($ productId );
101
- return $ stock ->getQty () >= $ requiredQuantity ;
79
+ return $ this ->isStockQtyAvailable ($ cartItem ->getProduct (), $ requiredItemQty );
102
80
}
103
81
104
82
/**
@@ -114,15 +92,34 @@ public function isStockAvailableBundle(Item $cartItem, int $previousQty, $reques
114
92
$ qtyOptions = $ cartItem ->getQtyOptions ();
115
93
$ totalRequestedQty = $ previousQty + $ requestedQty ;
116
94
foreach ($ qtyOptions as $ qtyOption ) {
117
- $ productId = (int )$ qtyOption ->getProductId ();
118
95
$ requiredItemQty = $ qtyOption ->getValue ();
119
96
if ($ totalRequestedQty ) {
120
97
$ requiredItemQty = $ requiredItemQty * $ totalRequestedQty ;
121
98
}
122
- if (!$ this ->isStockAvailable ( $ productId , $ requiredItemQty )) {
99
+ if (!$ this ->isStockQtyAvailable ( $ qtyOption -> getProduct () , $ requiredItemQty )) {
123
100
return false ;
124
101
}
125
102
}
126
103
return true ;
127
104
}
105
+
106
+ /**
107
+ * Check if product is available in stock using quantity from Catalog Inventory Stock Item
108
+ *
109
+ * @param ProductInterface $product
110
+ * @param float $requiredQuantity
111
+ * @throws NoSuchEntityException
112
+ * @return bool
113
+ */
114
+ private function isStockQtyAvailable (ProductInterface $ product , float $ requiredQuantity ): bool
115
+ {
116
+ $ stockItem = $ product ->getExtensionAttributes ()->getStockItem ();
117
+ if ($ stockItem === null ) {
118
+ return true ;
119
+ }
120
+ if ((int ) $ stockItem ->getProductId () !== (int ) $ product ->getId ()) {
121
+ throw new NoSuchEntityException (__ ('Stock item \'s product ID does not match requested product ID ' ));
122
+ }
123
+ return $ stockItem ->getQty () >= $ requiredQuantity ;
124
+ }
128
125
}
0 commit comments