Skip to content

Commit 1f1abcb

Browse files
authored
Merge pull request #136 from kadricaabuk/check-cart-item-quantity-and-actual-stock
check cart item quantity and actual stock
2 parents 90b395a + de0180d commit 1f1abcb

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

src/Adv/AdvModel.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,14 @@ public function addCart($item, $quantity = 1, $name = null)
341341
}
342342

343343

344+
public function getCartItemById($id){
345+
$cart = $this->dispatchSync(new GetCart());
346+
return $cart->getItems()->first(function ($item) use ($id) {
347+
return $item->entry_id == $id;
348+
});
349+
}
350+
351+
344352
public function getAdvDetailLinkByModel($object, $type = null)
345353
{
346354
if ($type != null) {

src/Http/Controller/AdvsController.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1422,11 +1422,20 @@ public function addCart(Request $request)
14221422
$thisModel = new AdvModel();
14231423
$adv = $thisModel->isAdv($id);
14241424
$response = array();
1425-
if(!$adv->inStock()) {
1425+
1426+
$check_stock_by_cart_count = true;
1427+
$cart_item = $thisModel->getCartItemById($id);
1428+
1429+
if($cart_item && $cart_item->quantity >= $adv->stock) {
1430+
$check_stock_by_cart_count = false;
1431+
}
1432+
1433+
if(!$adv->inStock() || !$check_stock_by_cart_count) {
14261434
$response['status'] = "error";
14271435
$response['msg'] = trans('visiosoft.module.advs::message.out_of_stock');
14281436
return $response;
14291437
}
1438+
14301439
if ($adv and $adv->getStatus() == "approved") {
14311440
$cart = $thisModel->addCart($adv, $quantity, $name);
14321441
$response['status'] = "success";

0 commit comments

Comments
 (0)