0

I'm trying to list the name of products inside the table cataloginventory_stock_item that are out of stock. Below the code I used:

<?php

use \Magento\Framework\App\Bootstrap;

include 'app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$objectManager->get('Magento\Framework\Registry')->register('isSecureArea', true);

$productRepository = $objectManager->create('Magento\Catalog\Model\ProductRepository');
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$productcollection = $productCollectionFactory->create()
                    ->addAttributeToSelect('*')
                    ->joinField('stock_item',
'cataloginventory_stock_item', 'qty', 'product_id=entity_id', 'qty=0')
                    ->setPageSize(8)
                    ->load();

foreach ($productcollection as $product){
 echo 'Name  =  '.$product->getName().'<br>';
}

This code return page blank as the out of stock products are present inside the table. I tried changing a different quantity and the script returns the right list.

Just a note: These products are set out of stock automatically using a REST API call function.

Any idea?

1
  • try with $objectManager->create() instead of $objectManager->get – Keyur Shah Apr 13 '18 at 16:23
0

To display out of stock product list when you use "frontend" as Area Code is to go to

Store->configuration [Catalog Tab] -> Inventory->Stock Option

and change the value [Display Out of Stock Products = Yes]

this code is working on my side

<?php

use Magento\Framework\App\Bootstrap;

include 'app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');


$productFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');

$collection = $productFactory->create();
$collection->addAttributeToSelect("name");
$collection->joinField(
    'is_in_stock', 'cataloginventory_stock_item', 'is_in_stock', 'product_id=entity_id', '{{table}}.is_in_stock = 0 ' );

foreach ($collection as $product) {
    echo $product->getName()."<br>";

}

otherwise you need to set the area code = adminhtml

like this

<?php

use Magento\Framework\App\Bootstrap;

include 'app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('adminhtml');


$productFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');

$collection = $productFactory->create();
$collection->addAttributeToSelect("name");
$collection->joinField(
    'is_in_stock', 'cataloginventory_stock_item', 'is_in_stock', 'product_id=entity_id', '{{table}}.is_in_stock = 0 ' );

foreach ($collection as $product) {
    echo $product->getName()."<br>";

}

hope this code will help you

1

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.