Zend certified PHP/Magento developer

Do I need to loop all children of the configurable product to know the stock of each product size?

In a shoe store I have the following configuration:

For each color we have a configurable product, and in this configurable product we have size variations.

We have a size filter … when a customer chooses a size on the filter as it is today, all simple products are looped to see if this product is in stock. But this process takes up to 3x longer to assemble the page.

To mount the collection I used:

public function getProductCollection()
{
    $collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    $collection->addAttributeToFilter('visibility', MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
    $collection->addAttributeToFilter('status',MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED);
    $collection->addFinalPrice();
    return $collection;
}

I did it because I couldn’t filter by direct size in this method.

So, in my view i did:

< ?php
   $productCollection = $this->getProductCollection();
   $imageBlock =  $block->getLayout()- 
   >createBlock('MagentoCatalogBlockProductListProduct');
   $showCart = true;
   $type = 'widget-new-grid';
   $viewMode = 'grid';
   $image = 'category_page_grid';

   $size_label = __('Size');
   $size_label = strtolower($size_label);

  $price_label = __('Price');
  $price_label = strtolower($price_label);
  $price_label = str_replace('ç','c',$price_label);

  $size = $this->getRequest()->getParam($size_label);
  $price = $this->getRequest()->getParam($price_label);
  $category_name = '';
  $rq = '';


   ?>
   < ?php if ($exist = ($productCollection && $productCollection->getSize())): ?>
< ?php include_once "filter.phtml"; ?>
    < ?php $iterator = 1; $count_products = 0; ?> < ?php /** @var $_product contem os produtos configuraveis. Foreach apenas de produtos configuraveis */ foreach ($productCollection as $_product): ?> < ?php if(!$_product->isSalable()) continue; ?> < ?php $productImage = $imageBlock->getImage($_product, 'category_page_grid');?> < ?php /** @var $childs contem os produtos simples que o produto configuravel contem. retorna Array */ $childs = $_product->getTypeInstance()->getUsedProducts($_product); $found_size = true; ?> < ?php if(isset($size) AND !empty($size)){ foreach ($childs as $child){ $size_attrcode = $this->getAttrCodeForSize($child); $size_text = $child->getAttributeText($size_attrcode); if($size !== $size_text) { $found_size = false; continue; } else { $found_size = true; if($child->isSalable()) break; else continue 2; } } } ?> < ?php if(!$found_size) continue; ?> < ?php $count_products++; ?> < ?php /* @escapeNotVerified */ echo($iterator++ == 1) ? '
  1. ' : '
  2. ' ?> < ?php echo $productImage->toHtml() ?>
    < ?php /* @escapeNotVerified */ echo $_product->getName(); ?> < ?= $childs[0]->getAttributeText('color'); ?> < ?php if(!empty($_product->getSpecialPrice())): ?>
    < ?php echo $block->_currency_helper->currency($_product->getData('price'), true, false); ?> < ?php echo $block->_currency_helper->currency($_product->getSpecialPrice(), true, false); ?>
    < ?php else: ?>
    < ?php if($_product->getFinalPrice() == 0): ?> < ?php echo $block->_currency_helper->currency($_product->getData('price'), true, false); ?> < ?php else: ?> < ?php echo $block->_currency_helper->currency($_product->getFinalPrice(), true, false); ?> < ?php endif; ?>
    < ?php endif; ?> < ?php echo $block->getProductDetailsHtml($_product); ?>
    < ?php echo($iterator == count($productCollection)+1) ? '
' : '' ?> < ?php endforeach ?> < ?php if(!$count_products): ?>
< ?= __('We could not find results for the selected filter.') ?>
< ?= __('continue without filter'); ?> < ?php endif; ?>

I just copy and paste the code here, but pay attention to the snippet:

$childs = $_product->getTypeInstance()->getUsedProducts($_product);

This is the way I found to take all the simple products associated with the configurable and find out if a certain size is in stock.

The question is: What is the best way to do this?