I want to create a table to show stock status if all products. This table will contains following fields: => Product Name => Product SKU => Quantity => Stock Status (In Stock / Out of Stock)

I tried using object manager.. but not able to find mistake in Stock Status.

Here is my code:


$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$productCollection = $objectManager


$collection = $productCollection->create()
    ->addAttributeToFilter('type_id', array('neq' => 'configurable'))
    ->joinField('stock_item', 'cataloginventory_stock_item', 'qty', 'product_id=entity_id') // add 'qty=0' to get outofstock products

            <td>Stock Status</td>
        $count = 1;
        foreach ($collection as $product) {
                <td><?php echo $count; ?></td>
                <td><?php echo $product->getName(); ?></td>
                <td><?php echo $product->getSku(); ?></td>
                <td><?php if($product->getStockItem()==NULL){echo "-";} else { echo $product->getStockItem(); } ?></td>
                if($product->getStockItem() > 0)
                    echo '<span style="color:green;">In Stock</span>';
                elseif($product->getStockItem() == NULL)
                    echo '<span style="color:blue;">N/A</span>';

                    echo '<span style="color:red;">Out of Stock</span>';
                } ?>
        <?php $count++;
        } ?>

Your Answer

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

Browse other questions tagged or ask your own question.