I have recently migrated our Magento store to Magento 2.2.5.

I am currently writing a module to allow certain customers to order products in batch.

I have created a very similar module before for another website we have, so i know the process of how to do it.

I am currently trying to pull in products by their category. However, when i do this no results are returned. Since then i have tried to just pull in every single product we have, ~4000 of them, however this still returns an empty result set.

I am pretty sure that all of the product categories have been assigned the relevant products, and this is shown in the back end in both individual product pages and also the category pages.

some examples of code i have been using:

All products:

$collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);

Products by category:

$collection = $productCollection->create()


public function __construct(
        \Magento\Backend\Block\Widget\Context $context,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\App\ResourceConnection $resource,
        \Magento\Customer\Model\Group $customerGroupCollection,
        \Magento\Catalog\Model\CategoryFactory $categoryFactory,
        \Magento\Catalog\Helper\Category $category,
        \Magento\Catalog\Model\CategoryRepository $categoryRepository
    ) {
        $this->_productCollectionFactory = $productCollectionFactory;
        $this->_customerSession = $customerSession;
        $this->_customerGroupCollection = $customerGroupCollection;
        $this->_resource = $resource;
        $this->_categoryFactory = $categoryFactory;
        $this->category = $category;
        $this->categoryRepository = $categoryRepository;

What could prevent products from being pulled through?

1 Answer 1

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
/** Apply filters here */
$collection = $productCollection->addAttributeToSelect('*')
foreach ($collection as $product){
     echo 'Name  =  '.$product->getName().'<br>';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.