0

I'm trying to get the qty of simple products in configurable products in the list page and it works with this code inside the product collection foreach:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$StockState = $objectManager->get('\Magento\CatalogInventory\Api\StockStateInterface');


foreach ($_productCollection as $_product):

$configProduct = $objectManager->create('Magento\Catalog\Model\Product')->load($_product->getId());

$_childrens = $configProduct->getTypeInstance()->getUsedProducts($configProduct);
 foreach ($_childrens as $child) {
  if ($StockState->getStockQty($child->getId(), $child->getStore()->getWebsiteId()) == 0) {
   echo "<span class='no-stock'>" . $child->getAttributeText('size') . "</span>";
  } else {
  echo "<span>" . $child->getAttributeText('size') . "</span>";
      }
}
endforeach;  

But this is very SLOW! how can I get the qty without using the objectManager?

how need to extend the ProductList class? How?

thank you in advance for your support

0

Solution:

$childProducts = $_product->getTypeInstance()->getUsedProducts($_product);
                            foreach ($childProducts as $child) {
                                $blockObj = $block->getLayout()->createBlock('Vendor\Module\Block\Product');
                                if ($blockObj->getStock($child) == 0) {
                                    echo "<span class='no-stock'>" . $child->getAttributeText('size') . "</span>";
                                } else {
                                    echo "<span>" . $child->getAttributeText('size') . "</span>";
                                }
                                $priceFormatted = $this->helper('Magento\Framework\Pricing\Helper\Data')->currency($child->getPrice(), true, false);
                            }

and custom block that extend template

namespace Vendor\Module\Block;

class Product extends \Magento\Framework\View\Element\Template
{
    protected $_scopeConfig;
    protected $_stockInterface;
    protected $_productRepository;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\CatalogInventory\Api\StockStateInterface $stockInterface,
        \Magento\Catalog\Model\ProductRepository $productRepository
    ){
        $this->_scopeConfig = $scopeConfig;
        $this->_stockInterface = $stockInterface;
        $this->_productRepository = $productRepository;
        parent::__construct($context);
    }

    public function getStockMessage($productId){
        $_product = $this->getProductById($productId);
        $_stock = $this->getStock($_product);
        if($_stock <= $this->getThresoldQty()){
            return __('Only %1 left', $_stock);
        }
        return '';
    }

    public function getProductById($id)
    {
        return $this->_productRepository->getById($id);
    }

    public function getStock($_product)
    {
        return $this->_stockInterface->getStockQty($_product->getId(), $_product->getStore()->getWebsiteId());
    }

    public function getThresoldQty(){
        return $this->_scopeConfig->getValue('cataloginventory/options/stock_threshold_qty', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    } 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.