Skip to content

Commit b161142

Browse files
committed
Use Stock Config to get value.
StockHelper to filter collection for us.
1 parent 6e534ad commit b161142

File tree

1 file changed

+23
-1
lines changed
  • app/code/Magento/ConfigurableProductGraphQl/Model/Variant

1 file changed

+23
-1
lines changed

app/code/Magento/ConfigurableProductGraphQl/Model/Variant/Collection.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Magento\Framework\EntityManager\MetadataPool;
1616
use Magento\Framework\Api\SearchCriteriaBuilder;
1717
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product as DataProvider;
18+
use Magento\CatalogInventory\Helper\Stock;
19+
use Magento\CatalogInventory\Api\StockConfigurationInterface;
1820

1921
/**
2022
* Collection for fetching configurable child product data.
@@ -41,6 +43,16 @@ class Collection
4143
*/
4244
private $productDataProvider;
4345

46+
/**
47+
* @var Stock
48+
*/
49+
private $stockHelper;
50+
51+
/**
52+
* @var StockConfigurationInterface
53+
*/
54+
protected $stockConfig;
55+
4456
/**
4557
* @var MetadataPool
4658
*/
@@ -73,13 +85,17 @@ public function __construct(
7385
ProductFactory $productFactory,
7486
SearchCriteriaBuilder $searchCriteriaBuilder,
7587
DataProvider $productDataProvider,
76-
MetadataPool $metadataPool
88+
MetadataPool $metadataPool,
89+
Stock $stockHelper,
90+
StockConfigurationInterface $stockConfig
7791
) {
7892
$this->childCollectionFactory = $childCollectionFactory;
7993
$this->productFactory = $productFactory;
8094
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
8195
$this->productDataProvider = $productDataProvider;
8296
$this->metadataPool = $metadataPool;
97+
$this->stockHelper = $stockHelper;
98+
$this->stockConfig = $stockConfig;
8399
}
84100

85101
/**
@@ -143,13 +159,19 @@ private function fetch() : array
143159
return $this->childrenMap;
144160
}
145161

162+
$showOutOfStock = $this->stockConfig->isShowOutOfStock();
163+
146164
foreach ($this->parentProducts as $product) {
147165
$attributeData = $this->getAttributesCodes($product);
148166
/** @var ChildCollection $childCollection */
149167
$childCollection = $this->childCollectionFactory->create();
150168
$childCollection->setProductFilter($product);
151169
$childCollection->addAttributeToSelect($attributeData);
152170

171+
if(!$showOutOfStock) {
172+
$this->stockHelper->addInStockFilterToCollection($childCollection);
173+
}
174+
153175
/** @var Product $childProduct */
154176
foreach ($childCollection->getItems() as $childProduct) {
155177
$formattedChild = ['model' => $childProduct, 'sku' => $childProduct->getSku()];

0 commit comments

Comments
 (0)