/** * Make a simple validation of a Purchasable instance. * * @param PurchasableInterface $purchasable Purchasable * @param int $stockRequired Stock required * @param bool $useStock Use stock * * @return bool|int Is valid or the number of elements that can be used */ public function isValidUsingSimplePurchasableValidation(PurchasableInterface $purchasable, $stockRequired, $useStock) { if (!$purchasable->isEnabled() || $stockRequired <= 0 || $useStock && $purchasable->getStock() <= 0) { return false; } if ($purchasable->getStock() < $stockRequired) { return $purchasable->getStock(); } return true; }
/** * Update stock. * * @param PurchasableInterface $purchasable Purchasable * @param int $stockToDecrease Stock to decrease * * @return false|int Real decreased stock or false if error */ public function updateSimplePurchasableStock(PurchasableInterface $purchasable, $stockToDecrease) { $stock = $purchasable->getStock(); if ($stock === ElcodiProductStock::INFINITE_STOCK || $stockToDecrease <= 0 || $stock <= 0) { return false; } $realStockToDecrease = min($stock, $stockToDecrease); $resultingStock = $stock - $realStockToDecrease; $purchasable->setStock($resultingStock); return $realStockToDecrease; }