Вопрос

i want to list all products but below code is showing only enabled products, i want to get disabled one also

$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->addAttributeToSelect('*')
            ->load();

foreach ($collection as $product){
     echo 'Name  =  '.$product->getName().'<br>';
}
Это было полезно?

Решение

Please Avoid use of ObjectManager

You can add this Model file to your existing module or create new Module and add this Model files.

app/code/VendoreName/ModuleName/Model

AllProduct.php

<?php

namespace VendoreName\ModuleName\Model;

use Magento\Framework\Model\AbstractModel;
use VendoreName\ModuleName\Model\ResourceModel\AllProduct as AllProductResourceModel;

class AllProduct extends AbstractModel
{
    protected function _construct()
    {
        $this->_init(AllProductResourceModel::class);
    }
}

app/code/VendoreName/ModuleName/Model/ResourceModel

AllProduct.php

<?php

namespace VendoreName\ModuleName\Model\ResourceModel;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class AllProduct extends AbstractDb
{
    protected function _construct()
    {
        $this->_init('catalog_product_entity', 'entity_id');
    }
}

app/code/VendoreName/ModuleName/Model/ResourceModel/AllProduct

Collection.php

<?php

namespace VendoreName\ModuleName\Model\ResourceModel\AllProduct;

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use VendoreName\ModuleName\Model\AllProduct as AllProductModel;
use VendoreName\ModuleName\Model\ResourceModel\AllProduct as AllProductResourceModel;

class Collection extends AbstractCollection
{
    protected function _construct()
    {
        $this->_init(
            AllProductModel::class,
            AllProductResourceModel::class
        );
    }
}

And you can get all product using below code

    protected $_productFactory;  
    protected $loadProduct;  

    public function __construct(
        ........................................................
        ........................................................
        \VendoreName\ModuleName\Model\ResourceModel\AllProduct\CollectionFactory $_productFactory,
        \Magento\Catalog\Model\ProductFactory $loadProduct,
        ........................................................
        ........................................................
    ) {
        ........................................................
        ........................................................
        $this->_productFactory = $_productFactory;
        $this->loadProduct = $loadProduct;
        ........................................................
        ........................................................
    }

    public function execute()
    {   
        //echo "<pre>";
        $product = $this->_productFactory->create();
        //print_r($product->getData());
        $count = count($product);
        echo "Total Product: ".$count."<br/>";
        if($count){
            foreach($product as $product_key => $product_value){
                //print_r($product_value->getData());
                $product_data = $this->loadProduct->create()->load($product_value->getEntityId());
                // get your product Data
                print_r($product_data->getName());  // get all product name
                echo "<br/>"
            }
        } else {
            echo "Product not found, Please add product,";
        }
        exit();
    }
}

I Hope This Helps You.

Другие советы

You try this code :-

$collection = $this->_productCollectionFactory->create()
                            ->addAttributeToSelect('*')
                            ->setPageSize($noOfProducts)
                            ->load();
            // Patch to alter load and get disabled products too
       $collection->clear();
            $where = $collection->getSelect()->getPart('where');
            foreach ($where as $key => $condition)
            {
                if(strpos($condition, 'stock_status_index.stock_status = 1') !== false){
                    $updatedWhere[] = 'AND (stock_status_index.stock_status IN (1,0))';
                } else {
                    $updatedWhere[] = $condition;
                }   
            }
            $collection->getSelect()->setPart('where', $updatedWhere);
            $collection->load();

And refer this link :-

How to get disable products in product collection in magento2.2

Please Check this!

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();


$query = 'SELECT * FROM ' . $resource->getTableName('catalog_product_entity');
$results = $connection->fetchAll($query);
echo $total_products = count($results); 


if($total_products > 0){
  for ($i=0;$i<$total_products;$i++) {
      $product_id = $results[$i]['entity_id'];
      $product = $objectManager->create('Magento\Catalog\Model\Product')->load($product_id);
      echo $product->getName();
      echo "<br>";
  }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с magento.stackexchange
scroll top