2

Using the below code or equivelant I want to get all products regardless of visiblity or status etc. This however does not seem to return products that are "Not Visible Individually".

$category = $this->categoryFactory->create()->load($catid);
$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('sku');
$collection->addCategoryFilter($category);

How can this be achieved?

0

Try this below code :

My Advice is please don't use objectManager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $current_category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');

$categoryId = NULL;
if($current_category) {
    $categoryId = $current_category->getEntityId();
}

    $category = $objectManager->get('Magento\Catalog\Model\CategoryFactory')->create()->load($categoryId);
    $collection = $objectManager->get('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory')->create();
    $collection->addAttributeToSelect('*');
    $collection->addCategoryFilter($category);
    $collection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
    $collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
    echo "<pre>";print_r($collection->getData());

return $collection;
4
  • did u get the result?
    – chris
    Aug 9 '18 at 13:15
  • Just tried it however didn't seem to work again i did try something like that earlier and same thing happened.
    – harri
    Aug 9 '18 at 13:41
  • Ill just double check everyhing
    – harri
    Aug 9 '18 at 13:42
  • Even filtering to: $collection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_NOT_VISIBLE); shows no products when i can clearly see about 90 products not visible in my category it is strange.
    – harri
    Aug 9 '18 at 13:52

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.