OnlyXLeftInStockResolver.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\CatalogInventoryGraphQl\Model\Resolver;
  8. use Magento\Catalog\Api\Data\ProductInterface;
  9. use Magento\CatalogInventory\Api\StockRegistryInterface;
  10. use Magento\CatalogInventory\Model\Configuration;
  11. use Magento\Framework\App\Config\ScopeConfigInterface;
  12. use Magento\Framework\Exception\LocalizedException;
  13. use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
  14. use Magento\Framework\GraphQl\Config\Element\Field;
  15. use Magento\Framework\GraphQl\Query\ResolverInterface;
  16. use Magento\Store\Model\ScopeInterface;
  17. /**
  18. * @inheritdoc
  19. */
  20. class OnlyXLeftInStockResolver implements ResolverInterface
  21. {
  22. /**
  23. * @var ScopeConfigInterface
  24. */
  25. private $scopeConfig;
  26. /**
  27. * @var StockRegistryInterface
  28. */
  29. private $stockRegistry;
  30. /**
  31. * @param ScopeConfigInterface $scopeConfig
  32. * @param StockRegistryInterface $stockRegistry
  33. */
  34. public function __construct(
  35. ScopeConfigInterface $scopeConfig,
  36. StockRegistryInterface $stockRegistry
  37. ) {
  38. $this->scopeConfig = $scopeConfig;
  39. $this->stockRegistry = $stockRegistry;
  40. }
  41. /**
  42. * @inheritdoc
  43. */
  44. public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
  45. {
  46. if (!array_key_exists('model', $value) || !$value['model'] instanceof ProductInterface) {
  47. throw new LocalizedException(__('"model" value should be specified'));
  48. }
  49. /* @var $product ProductInterface */
  50. $product = $value['model'];
  51. $onlyXLeftQty = $this->getOnlyXLeftQty($product);
  52. return $onlyXLeftQty;
  53. }
  54. /**
  55. * Get product qty left when "Catalog > Inventory > Stock Options > Only X left Threshold" is greater than 0
  56. *
  57. * @param ProductInterface $product
  58. *
  59. * @return null|float
  60. */
  61. private function getOnlyXLeftQty(ProductInterface $product): ?float
  62. {
  63. $thresholdQty = (float)$this->scopeConfig->getValue(
  64. Configuration::XML_PATH_STOCK_THRESHOLD_QTY,
  65. ScopeInterface::SCOPE_STORE
  66. );
  67. if ($thresholdQty === 0) {
  68. return null;
  69. }
  70. $stockItem = $this->stockRegistry->getStockItem($product->getId());
  71. $stockCurrentQty = $this->stockRegistry->getStockStatus(
  72. $product->getId(),
  73. $product->getStore()->getWebsiteId()
  74. )->getQty();
  75. $stockLeft = $stockCurrentQty - $stockItem->getMinQty();
  76. $thresholdQty = (float)$this->scopeConfig->getValue(
  77. Configuration::XML_PATH_STOCK_THRESHOLD_QTY,
  78. ScopeInterface::SCOPE_STORE
  79. );
  80. if ($stockCurrentQty > 0 && $stockLeft <= $thresholdQty) {
  81. return (float)$stockLeft;
  82. }
  83. return null;
  84. }
  85. }