/** * @category ISO_HOOKS: addProductToCollection * * @param Product|\Model $product * @param int $quantity * @param ProductCollection $collection * * @return int */ public function checkBeforeAddToCollection(Product $product, $quantity, ProductCollection $collection) { /** @var ProductType|\Model $productType */ $productType = $product->getRelated('type'); if (!$productType->stockmanagement_active) { return $quantity; } $stock = Stock::getStockForProduct($product->id); $quantityInCart = 0; foreach ($collection->getItems() as $item) { if ($item->product_id === $product->id) { $quantityInCart += $item->quantity; } } if (false === $stock) { return $quantity; } elseif (0 === $stock) { Message::addError($GLOBALS['TL_LANG']['MSC']['simpleStockmanagement']['productUnavailable']); return 0; } elseif ($quantity + $quantityInCart > $stock) { Message::addInfo($GLOBALS['TL_LANG']['MSC']['simpleStockmanagement']['maxQuantityAdded']); return $stock - $quantityInCart; } return $quantity; }