Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OnlyXLeftInStockResolver.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
19 
24 {
28  private $scopeConfig;
29 
33  private $stockRegistry;
34 
39  public function __construct(
40  ScopeConfigInterface $scopeConfig,
41  StockRegistryInterface $stockRegistry
42  ) {
43  $this->scopeConfig = $scopeConfig;
44  $this->stockRegistry = $stockRegistry;
45  }
46 
50  public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
51  {
52  if (!array_key_exists('model', $value) || !$value['model'] instanceof ProductInterface) {
53  throw new GraphQlInputException(__('"model" value should be specified'));
54  }
55 
56  /* @var $product ProductInterface */
57  $product = $value['model'];
58  $onlyXLeftQty = $this->getOnlyXLeftQty($product);
59 
60  return $onlyXLeftQty;
61  }
62 
70  private function getOnlyXLeftQty(ProductInterface $product): ?float
71  {
72  $thresholdQty = (float)$this->scopeConfig->getValue(
75  );
76  if ($thresholdQty === 0) {
77  return null;
78  }
79 
80  $stockItem = $this->stockRegistry->getStockItem($product->getId());
81 
82  $stockCurrentQty = $this->stockRegistry->getStockStatus(
83  $product->getId(),
84  $product->getStore()->getWebsiteId()
85  )->getQty();
86 
87  $stockLeft = $stockCurrentQty - $stockItem->getMinQty();
88 
89  $thresholdQty = (float)$this->scopeConfig->getValue(
92  );
93 
94  if ($stockCurrentQty > 0 && $stockLeft <= $thresholdQty) {
95  return (float)$stockLeft;
96  }
97 
98  return null;
99  }
100 }
resolve(Field $field, $context, ResolveInfo $info, array $value=null, array $args=null)
__()
Definition: __.php:13
$value
Definition: gender.phtml:16
__construct(ScopeConfigInterface $scopeConfig, StockRegistryInterface $stockRegistry)
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52