1

I have below code in an script outside Magento to get products. It's working fine except it doens't return out of stock products.

How can I add out of stock products to the collection?

// Only gets in stock products
public function getProductsInStock() {
    try {
        $productCollection = $this->objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');

        $collection = $productCollection->create()
            ->addAttributeToSelect('sku')
            ->addAttributeToSelect('price')
            ->load();
    } catch (\Exception $e) {
        echo $e->getMessage();
    }
    return $collection;
}

EDIT: I just did some testing. It seems it has to do with following option in backend: Display Out of Stock Products. When it is set to yes the code returns all products instead of just the in stock products.

Is it possible to ignore this setting in any way?

2 Answers 2

4

Option 1 You can use Magento\Catalog\Model\ResourceModel\Product\Collection according your needs. You can use can use it to get product data :

Option 2 : Also, You can try below code to get product collection, ( out side of magento ) though this is not best way

Update :

<?php

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
/** Apply filters here */
$collection = $productCollection->setFlag('has_stock_status_filter', true)->load();

foreach ($collection as $product) {
    echo $product->getSku() . "=>" . $product->getPrice();
}
9
  • Thx for suggestions. I just tried it out option 2. I doesn't return out of stock products either. Am I doing something wrong? Is there maybe something going wrong? Might this is a feature (backend setting, or so?).
    – Akif
    Commented Apr 2, 2018 at 14:29
  • I just did some testing. It has to do with following option in backend: Display Out of Stock Products. When it is set to yes the code returns all products instead of just the in stock products. Is it possible to ignore this setting in any way? I will update my question.
    – Akif
    Commented Apr 2, 2018 at 18:29
  • I have updated my answer, it will get you all products Commented Apr 2, 2018 at 18:34
  • @Akif, let me know if it works for you Commented Apr 2, 2018 at 18:46
  • 1
    @Akif, hope this will work for you, i have setFlag('has_stock_status_filter', true), which will bypass stock filter and display all products ( in stock and out of stock ) Commented Apr 2, 2018 at 19:17
0
$resource = $objectManager->create('Magento\CatalogInventory\Model\ResourceModel\Stock\Item');
    $select = $resource->getConnection()->select()->from($resource->getMainTable());
    $stockItems = $resource->getConnection()->fetchAll($select);
    foreach($stockItems as $_item){
        var_dump($_item->getData());
    }

Please try this one may be it will help you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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