Skip to content

Instantly share code, notes, and snippets.

Created Oct 3, 2018
What would you like to do?
/** @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 */
// 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