Skip to content

Instantly share code, notes, and snippets.

@Nerjuz
Created Oct 3, 2018
Embed
What would you like to do?
<?php
/** @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory */
protected $_collectionFactory;
public function __construct(
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory
) {
$this->_collectionFactory = $collectionFactory;
}
public function yourMethod()
{
// Use factory to create a new product collection
$productCollection = $this->_collectionFactory->create();
/** Apply filters here */
$productCollection->addAttributeToSelect('*');
// Don't have to do this
// $productCollection->load();
foreach ($productCollection as $product){
echo 'Name = '.$product->getName().'<br>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment