20
20
21
21
namespace Meta \Catalog \Model \Product \Feed \Builder ;
22
22
23
- use Meta \BusinessExtension \Model \System \Config as SystemConfig ;
24
23
use Magento \Catalog \Model \Product ;
24
+ use Magento \InventorySalesAdminUi \Model \GetIsManageStockForProduct ;
25
25
use Magento \InventorySalesApi \Api \GetProductSalableQtyInterface ;
26
26
use Magento \InventorySalesApi \Api \IsProductSalableInterface ;
27
27
use Magento \InventorySalesApi \Model \StockByWebsiteIdResolverInterface ;
28
+ use Meta \BusinessExtension \Model \System \Config as SystemConfig ;
28
29
29
30
class MultiSourceInventory implements InventoryInterface
30
31
{
@@ -63,22 +64,30 @@ class MultiSourceInventory implements InventoryInterface
63
64
*/
64
65
private $ stockQty ;
65
66
67
+ /**
68
+ * @var GetIsManageStockForProduct
69
+ */
70
+ private GetIsManageStockForProduct $ getIsManageStockForProduct ;
71
+
66
72
/**
67
73
* @param IsProductSalableInterface $isProductSalableInterface
68
74
* @param GetProductSalableQtyInterface $getProductSalableQtyInterface
69
75
* @param SystemConfig $systemConfig
70
76
* @param StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver
77
+ * @param GetIsManageStockForProduct $getIsManageStockForProduct
71
78
*/
72
79
public function __construct (
73
80
IsProductSalableInterface $ isProductSalableInterface ,
74
81
GetProductSalableQtyInterface $ getProductSalableQtyInterface ,
75
82
SystemConfig $ systemConfig ,
76
- StockByWebsiteIdResolverInterface $ stockByWebsiteIdResolver
83
+ StockByWebsiteIdResolverInterface $ stockByWebsiteIdResolver ,
84
+ GetIsManageStockForProduct $ getIsManageStockForProduct
77
85
) {
78
86
$ this ->isProductSalableInterface = $ isProductSalableInterface ;
79
87
$ this ->getProductSalableQtyInterface = $ getProductSalableQtyInterface ;
80
88
$ this ->systemConfig = $ systemConfig ;
81
89
$ this ->stockByWebsiteIdResolver = $ stockByWebsiteIdResolver ;
90
+ $ this ->getIsManageStockForProduct = $ getIsManageStockForProduct ;
82
91
}
83
92
84
93
/**
@@ -119,6 +128,21 @@ private function getStockQty(Product $product, int $stockId)
119
128
}
120
129
}
121
130
131
+ /**
132
+ * Checks if product is having managed stock
133
+ *
134
+ * @return bool
135
+ */
136
+ public function isStockManagedForProduct (): bool
137
+ {
138
+ try {
139
+ $ websiteCode = $ this ->product ->getStore ()->getWebsite ()->getCode ();
140
+ return $ this ->getIsManageStockForProduct ->execute ($ this ->product ->getSku (), $ websiteCode );
141
+ } catch (\Throwable $ e ) {
142
+ return false ;
143
+ }
144
+ }
145
+
122
146
/**
123
147
* Initiate inventory for the product
124
148
*
@@ -142,6 +166,10 @@ public function initInventoryForProduct(Product $product): MultiSourceInventory
142
166
*/
143
167
public function getAvailability (): string
144
168
{
169
+ // unmanaged stock is always available
170
+ if (!$ this ->isStockManagedForProduct ()) {
171
+ return self ::STATUS_IN_STOCK ;
172
+ }
145
173
return $ this ->getInventory () && $ this ->stockStatus ? self ::STATUS_IN_STOCK : self ::STATUS_OUT_OF_STOCK ;
146
174
}
147
175
@@ -155,6 +183,9 @@ public function getInventory(): int
155
183
if (!$ this ->product ) {
156
184
return 0 ;
157
185
}
186
+ if (!$ this ->isStockManagedForProduct ()) {
187
+ return self ::UNMANAGED_STOCK_QTY ;
188
+ }
158
189
$ outOfStockThreshold = $ this ->systemConfig ->getOutOfStockThreshold ($ this ->product ->getStoreId ());
159
190
$ quantityAvailableForCatalog = (int ) $ this ->stockQty - $ outOfStockThreshold ;
160
191
return $ quantityAvailableForCatalog > 0 ? $ quantityAvailableForCatalog : 0 ;
0 commit comments