You can use Model StockRegistry Magento\CatalogInventory\Model\StockRegistry $stockRegistry
Register a Block or Helper. My code example use Helper.
class Data
{
protected $catalogProduct;
protected $stockRegistry;
public function __construct(
\Magento\Catalog\Helper\Product $catalogProduct,
\Magento\CatalogInventory\Model\StockRegistry $stockRegistry)
{
$this->stockRegistry = $stockRegistry;
$this->catalogProduct = $catalogProduct;
}
/**
* @param $product
* @return \Magento\Catalog\Model\Product[]
*/
public function getAllowProducts($product)
{
$skipSaleableCheck = $this->catalogProduct->getSkipSaleableCheck();
$products = $skipSaleableCheck ?
$product->getTypeInstance()->getUsedProducts($product, null) :
$product->getTypeInstance()->getSalableUsedProducts($product, null);
return $products;
}
public function getChildInStock($product)
{
$inStock = [];
foreach ($this->getAllowProducts($product) as $_product) {
$stockItem = $this->stockRegistry->getStockItem($_product->getId(), 1);
$saleable = $stockItem->getIsInStock() && $this->verifyStock($stockItem);
if($saleable){
$inStock[] = $_product;
}
}
return $inStock;
}
}