0

I have recently migrated our Magento 1.9.2.4 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->addAttributeToSelect('*');
        $collection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
        $collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);

Products by category:

$collection = $productCollection->create()
              ->addAttributeToFilter('attribute_set_id','174')
              ->addAttributeToSelect('*')
              ->load();

Constructor:

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
    ) {
        parent::__construct($context);
        $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?

0

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

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.