Skip to content

Instantly share code, notes, and snippets.

@Nerjuz
Created October 3, 2018 04:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nerjuz/0e24dbe0b98bb4e69eaf0061d14fda0b to your computer and use it in GitHub Desktop.
Save Nerjuz/0e24dbe0b98bb4e69eaf0061d14fda0b to your computer and use it in GitHub Desktop.
<?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