Skip to content

Commit 169d751

Browse files
authored
Merge pull request #183 from wip44850/LYNX-310_LYNX-302
Merging to create more changes to the task
2 parents 6a5060a + 6057db1 commit 169d751

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

app/code/Magento/Quote/Model/Quote/Item.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ public function addQty($qty)
348348
if (!$this->getParentItem() || !$this->getId()) {
349349
$qty = $this->_prepareQty($qty);
350350
$this->setQtyToAdd($qty);
351+
$this->setPreviousQty($this->getQty());
351352
$this->setQty($this->getQty() + $qty);
352353
}
353354
return $this;
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
/************************************************************************
3+
*
4+
* Copyright 2023 Adobe
5+
* All Rights Reserved.
6+
*
7+
* NOTICE: All information contained herein is, and remains
8+
* the property of Adobe and its suppliers, if any. The intellectual
9+
* and technical concepts contained herein are proprietary to Adobe
10+
* and its suppliers and are protected by all applicable intellectual
11+
* property laws, including trade secret and copyright laws.
12+
* Dissemination of this information or reproduction of this material
13+
* is strictly forbidden unless prior written permission is obtained
14+
* from Adobe.
15+
* ************************************************************************
16+
*/
17+
declare(strict_types=1);
18+
19+
namespace Magento\QuoteGraphQl\Model\Resolver;
20+
21+
use Magento\Framework\Exception\LocalizedException;
22+
use Magento\Framework\GraphQl\Config\Element\Field;
23+
use Magento\Framework\GraphQl\Query\ResolverInterface;
24+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
25+
use Magento\CatalogInventory\Api\Data\StockStatusInterface;
26+
use Magento\CatalogInventory\Api\StockStatusRepositoryInterface;
27+
use Magento\Quote\Model\Quote\Item;
28+
29+
/**
30+
* @inheritdoc
31+
*/
32+
class CheckAvailability implements ResolverInterface
33+
{
34+
/**
35+
* Product type code
36+
*/
37+
private const PRODUCT_TYPE = "bundle";
38+
39+
/**
40+
* @var StockStatusRepositoryInterface
41+
*/
42+
private $stockStatusRepository;
43+
44+
45+
/**
46+
* CheckAvailability constructor
47+
*
48+
* @param StockStatusRepositoryInterface $stockStatusRepository
49+
*/
50+
public function __construct(
51+
StockStatusRepositoryInterface $stockStatusRepository
52+
) {
53+
$this->stockStatusRepository = $stockStatusRepository;
54+
}
55+
56+
/**
57+
* @inheritdoc
58+
*/
59+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
60+
{
61+
if (!isset($value['model'])) {
62+
throw new LocalizedException(__('"model" value should be specified'));
63+
}
64+
/** @var Item $cartItem */
65+
$cartItem = $value['model'];
66+
67+
return $this->checkProductQtyStatus($cartItem) ? "available" : "unavailable";
68+
}
69+
70+
/**
71+
* @param Item $cartItem
72+
* @return bool
73+
*/
74+
private function checkProductQtyStatus($cartItem):bool
75+
{
76+
$requestedQty = 0;
77+
$previousQty = 0;
78+
79+
if ($cartItem->getProductType() == self::PRODUCT_TYPE) {
80+
$qtyOptions = $cartItem->getQtyOptions();
81+
$requestedQty = $cartItem->getQtyToAdd() ? $cartItem->getQtyToAdd() : $cartItem->getQty();
82+
$previousQty = $cartItem->getPreviousQty() ? $cartItem->getPreviousQty() : 0;
83+
$totalReqQty = $previousQty + $requestedQty;
84+
foreach($qtyOptions as $qtyOption)
85+
{
86+
$productId = (int) $qtyOption->getProductId();
87+
$requiredItemQty = (float) $qtyOption->getValue();
88+
if ($totalReqQty) {
89+
$requiredItemQty = $requiredItemQty * $totalReqQty;
90+
}
91+
92+
if ($this->getProductStockStatus($productId, $requiredItemQty)) {
93+
return false;
94+
}
95+
}
96+
} else {
97+
foreach ($cartItem->getQuote()->getItems() as $item) {
98+
99+
if($item->getItemId() == $cartItem->getItemId() && $item->getQtyToAdd()) {
100+
$requestedQty = (float)$item->getQtyToAdd();
101+
$previousQty = $item->getPreviousQty() ? (float)$item->getPreviousQty() : 0;
102+
}
103+
}
104+
$requiredItemQty = $requestedQty + $previousQty;
105+
$productId = (int) $cartItem->getProduct()->getId();
106+
if ($this->getProductStockStatus($productId, $requiredItemQty)) {
107+
return false;
108+
}
109+
}
110+
return true;
111+
}
112+
113+
/**
114+
* @param int $productId
115+
* @param float $requiredQuantity
116+
* @return bool
117+
*/
118+
private function getProductStockStatus(int $productId, float $requiredQuantity): bool
119+
{
120+
$stock = $this->stockStatusRepository->get($productId);
121+
return ($stock->getQty() < $requiredQuantity) ? true : false;
122+
}
123+
}

app/code/Magento/QuoteGraphQl/etc/schema.graphqls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ interface CartItemInterface @typeResolver(class: "Magento\\QuoteGraphQl\\Model\\
360360
id: String! @deprecated(reason: "Use `uid` instead.")
361361
uid: ID! @doc(description: "The unique ID for a `CartItemInterface` object.")
362362
quantity: Float! @doc(description: "The quantity of this item in the cart.")
363+
status: String @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CheckAvailability") @doc(description: "If qty is more than stock display status as unavailable else available.")
363364
prices: CartItemPrices @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartItemPrices") @doc(description: "Contains details about the price of the item, including taxes and discounts.")
364365
product: ProductInterface! @doc(description: "Details about an item in the cart.")
365366
errors: [CartItemError!] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartItemErrors") @doc(description: "An array of errors encountered while loading the cart item")

0 commit comments

Comments
 (0)