Skip to content

Commit

Permalink
Merge pull request #13 from mabruchet/fix/add-cart-without-stock
Browse files Browse the repository at this point in the history
Fix add cart without stock
  • Loading branch information
Lucanis authored Jan 7, 2021
2 parents 140386d + 8e2f5bf commit 4c20b88
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions Controller/Front/CartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
use Thelia\Model\AreaDeliveryModuleQuery;
use Thelia\Model\Cart;
use Thelia\Model\CartItemQuery;
use Thelia\Model\ConfigQuery;
use Thelia\Model\Country;
use Thelia\Model\CouponQuery;
use Thelia\Model\ModuleQuery;
use Thelia\Model\ProductSaleElements;
use Thelia\Model\ProductSaleElementsQuery;
use Thelia\Model\State;
use Thelia\Module\BaseModule;
Expand Down Expand Up @@ -292,6 +294,20 @@ protected function getCurrentOpenApiCart($cart)
return $openApiCart;
}

/**
* @param ProductSaleElements $pse
* @param integer $quantity
* @return bool
*/
protected function checkAvailableStock(ProductSaleElements $pse, $quantity) {

if ($pse && $quantity) {
return $quantity > $pse->getQuantity() && ConfigQuery::checkAvailableStock() && !$pse->getProduct()->getVirtual() === 0;
}

throw new \Exception(Translator::getInstance()->trans('A PSE is needed in the POST request to add an item to the cart.'));
}

/**
* Update a Cart Event from a json
*
Expand All @@ -310,7 +326,7 @@ protected function updateCartEventFromJson($json, CartEvent $event)
/** If the function was called from the PATCH route, we just update the quantity and return */
if ($cartItemId = $event->getCartItemId()) {
$cartItem = CartItemQuery::create()->filterById($cartItemId)->findOne();
if ($data['quantity'] >= $cartItem->getProductSaleElements()->getQuantity()) {
if ($this->checkAvailableStock($cartItem->getProductSaleElements(), $data['quantity'])) {
throw new \Exception(Translator::getInstance()->trans('Desired quantity exceed available stock'));
}
$event->setQuantity($data['quantity']);
Expand All @@ -325,18 +341,14 @@ protected function updateCartEventFromJson($json, CartEvent $event)
throw new \Exception(Translator::getInstance()->trans('You need to set the append value in the POST request to add an item to the cart.'));
}

$availableQuantity = ProductSaleElementsQuery::create()
->filterById($data['pseId'])
->findOne()
->getQuantity()
;
$pse = ProductSaleElementsQuery::create()->findPk($data['pseId']);

if ($data['quantity'] > $availableQuantity) {
if ($this->checkAvailableStock($pse, $data['quantity'])) {
throw new \Exception(Translator::getInstance()->trans('Desired quantity exceed available stock'));
}

$event
->setProduct(ProductSaleElementsQuery::create()->findPk($data['pseId'])->getProductId())
->setProduct($pse->getProductId())
->setProductSaleElementsId($data['pseId'])
->setQuantity($data['quantity'])
->setAppend($data['append'])
Expand Down

0 comments on commit 4c20b88

Please sign in to comment.