0

I'm in a controller's action in a module I just created. How can I get an array with all the products I got in my catalog?

I did this, but it's not returning the items in the catalog

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$collection = $objectManager->create('Magento\Catalog\Model\Product')->getCollection();

Plus, where can I find this kind of information? I'm starting on Magento these last days and everything is just too dificult to learn and find

For example, where is the documentation for 'Magento\Catalog\Model\Product' ? And for Object Manager?

I'm in Magento 2.1.5

0

It's better to use constructor to get the model instead using object manager, so in your construct, you can try this:

    public function __construct(
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    ) {
        $this->productCollectionFactory = $productCollectionFactory;
    }

In the function:

$collection = $this->productCollectionFactory->create();

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.