Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StockStatusProvider.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
17 
22 {
26  private $stockStatusRepository;
27 
31  public function __construct(StockStatusRepositoryInterface $stockStatusRepository)
32  {
33  $this->stockStatusRepository = $stockStatusRepository;
34  }
35 
39  public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
40  {
41  if (!array_key_exists('model', $value) || !$value['model'] instanceof ProductInterface) {
42  throw new GraphQlInputException(__('"model" value should be specified'));
43  }
44 
45  /* @var $product ProductInterface */
46  $product = $value['model'];
47 
48  $stockStatus = $this->stockStatusRepository->get($product->getId());
49  $productStockStatus = (int)$stockStatus->getStockStatus();
50 
51  return $productStockStatus === StockStatusInterface::STATUS_IN_STOCK ? 'IN_STOCK' : 'OUT_OF_STOCK';
52  }
53 }
__()
Definition: __.php:13
$stockStatusRepository
resolve(Field $field, $context, ResolveInfo $info, array $value=null, array $args=null)
$value
Definition: gender.phtml:16
__construct(StockStatusRepositoryInterface $stockStatusRepository)
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52