@@ -255,13 +255,16 @@ public static function isInStock($idProduct, $wantedQuantity = 1, Cart $cart = n
255
255
}
256
256
257
257
/**
258
- * Returns the available quantity of a given pack (this method already have decreased products in cart).
258
+ * Returns the available quantity of a given pack.
259
+ *
260
+ * By default, it returns the TRUE quantity in stock. If you want to, you can pass a $cart parameter
261
+ * and the quantity in stock will be reduced by the quantity there is in the cart.
259
262
*
260
263
* @param int $idProduct Product id
261
264
* @param int|null $idProductAttribute Product attribute id (optional)
262
- * @param bool|null $cacheIsPack
263
- * @param CartCore|null $cart
264
- * @param bool| int|null $idCustomization Product customization id (optional)
265
+ * @param bool|null $cacheIsPack (unused, you can pass null)
266
+ * @param CartCore|null $cart Pass if you want to reduce the quantity by amount in cart
267
+ * @param int|null $idCustomization Product customization id (optional)
265
268
*
266
269
* @return int
267
270
*
@@ -284,10 +287,8 @@ public static function getQuantity(
284
287
// Initialize
285
288
$ product = new Product ($ idProduct , false );
286
289
$ packQuantity = 0 ;
287
- $ packQuantityInStock = StockAvailable::getQuantityAvailableByProduct (
288
- $ idProduct ,
289
- $ idProductAttribute
290
- );
290
+
291
+ // We get the pack stock calculation type it has set up
291
292
$ packStockType = $ product ->pack_stock_type ;
292
293
$ allPackStockType = [
293
294
self ::STOCK_TYPE_PACK_ONLY ,
@@ -300,20 +301,30 @@ public static function getQuantity(
300
301
throw new PrestaShopException ('Unknown pack stock type ' );
301
302
}
302
303
303
- // If no pack stock or shop default, set it
304
- if (empty ($ packStockType )
305
- || $ packStockType == self ::STOCK_TYPE_DEFAULT
306
- ) {
304
+ /*
305
+ * Now, we have resolved how we will calculate the stock of this pack. It can be one of the following.
306
+ *
307
+ * STOCK_TYPE_PACK_ONLY - pack 1pcs + product A 10pcs + product B 20pcs = 1pcs
308
+ * STOCK_TYPE_PRODUCTS_ONLY - pack 1pcs + product A 10pcs + product B 20pcs = 10 pcs
309
+ * STOCK_TYPE_PACK_BOTH - pack 1pcs + product A 10pcs + product B 20pcs = 1 pcs
310
+ */
311
+
312
+ // If no pack stock or shop default, set it from configuration
313
+ if (empty ($ packStockType ) || $ packStockType == self ::STOCK_TYPE_DEFAULT ) {
307
314
$ packStockType = Configuration::get ('PS_PACK_STOCK_TYPE ' );
308
315
}
309
316
310
- // Initialize with pack quantity if not only products
317
+ // If the quantity of the pack depends only on the pack or both packs and products,
318
+ // we need to load the quantity of the pack from stock_available table.
311
319
if (in_array ($ packStockType , [self ::STOCK_TYPE_PACK_ONLY , self ::STOCK_TYPE_PACK_BOTH ])) {
312
- $ packQuantity = $ packQuantityInStock ;
320
+ $ packQuantity = StockAvailable::getQuantityAvailableByProduct (
321
+ $ idProduct ,
322
+ $ idProductAttribute
323
+ );
313
324
}
314
325
315
- // Set pack quantity to the minimum quantity of pack , or
316
- // product pack
326
+ // If the quantity of the pack depends on the products inside , or both pack and products,
327
+ // we need to set the pack quantity to the lowest quantity of products inside.
317
328
if (in_array ($ packStockType , [self ::STOCK_TYPE_PACK_BOTH , self ::STOCK_TYPE_PRODUCTS_ONLY ])) {
318
329
$ items = array_values (Pack::getItems ($ idProduct , Configuration::get ('PS_LANG_DEFAULT ' )));
319
330
@@ -323,6 +334,7 @@ public static function getQuantity(
323
334
324
335
// Initialize packQuantity with the first product quantity
325
336
// if pack decrement stock type is products only
337
+ // @todo This is probably not needed because $packQuantity is always initialized to zero.
326
338
if ($ index === 0
327
339
&& $ packStockType == self ::STOCK_TYPE_PRODUCTS_ONLY
328
340
) {
@@ -331,6 +343,7 @@ public static function getQuantity(
331
343
continue ;
332
344
}
333
345
346
+ // If the quantity of the individual item is lower than what we currently calculated, it's our new quantity.
334
347
if ($ nbPackAvailableForItem < $ packQuantity ) {
335
348
$ packQuantity = $ nbPackAvailableForItem ;
336
349
}
0 commit comments