10

I want to apply visibility and status filter on product collection. Like in Magento 1 we do:

Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);

Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);

I get the product collection in Magento 2 but not find function to apply above filters.

15

For getting product collection with status enable and visibility:

you have to keep below code in block file.

protected $productCollectionFactory;
protected $productVisibility;
protected $productStatus;

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,        
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    \Magento\Catalog\Model\Product\Attribute\Source\Status $productStatus,
    \Magento\Catalog\Model\Product\Visibility $productVisibility,
    array $data = []
)
{
    $this->productCollectionFactory = $productCollectionFactory;
    $this->productStatus = $productStatus;
    $this->productVisibility = $productVisibility;
    parent::__construct($context, $data);
}

public function getProductCollection()
{
    $collection = $this->productCollectionFactory->create();
    $collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()]);
    $collection->setVisibility($this->productVisibility->getVisibleInSiteIds());
    return $collection;
}

Above code working for visibility type for:

  • VISIBILITY_IN_SEARCH
  • VISIBILITY_IN_CATALOG
  • VISIBILITY_BOTH
3

This should work for you:

    $collection = $this->collectionFactory->create();

    $collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()]);
    $collection->setVisibility($this->productVisibility->getVisibleInSiteIds());

Used classes are:

/**
 * @var \Magento\Catalog\Model\Resource\Product\CollectionFactory
 */
protected $collectionFactory;

/**
 * @var \Magento\Catalog\Model\Product\Attribute\Source\Status
 */
protected $productStatus;

/**
 * @var \Magento\Catalog\Model\Product\Visibility
 */
protected $productVisibility;

Review these classes to check another methods to get possible status or visibility values.

However, review concept of repositories, as far as I saw Magento 2 recommends using repository to access product data instead of using collection. For example:

$searchCriteria = $this->searchCriteriaBuilder->create();

$products = $this->productRepository->getList($searchCriteria)->getItems();
0

for product collection with status enable and visibility:

you can try with below code

protected $productCollectionFactory;
protected $productVisibility;
protected $productStatus;

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,        
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    \Magento\Catalog\Model\Product\Attribute\Source\Status $productStatus,
    \Magento\Catalog\Model\Product\Visibility $productVisibility,
    array $data = []
)
{
    $this->productCollectionFactory = $productCollectionFactory;
    $this->productStatus = $productStatus;
    $this->productVisibility = $productVisibility;
    parent::__construct($context, $data);
}

public function getProductCollection()
{
    $collection = $this->productCollectionFactory->create();
    $collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()]);
    $collection->setVisibility($this->productVisibility->getVisibleInSiteIds());
    return $collection;
}

for this code working for visibility type like :

VISIBILITY_IN_SEARCH
VISIBILITY_IN_CATALOG
VISIBILITY_BOTH

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.