Get Product Collection Filter By Visibility in Magento 2
As you might already know, product collection plays a paramount role in every project of the Magento 2 developer. In the previous post, I have shown you the steps to get product collection by category, therefore, in today’s article, I will guide you how to get product collection filter by visibility in Magento 2.
Visibility is a product attribute, not a category attribute. Sometimes, the store owners will want to restrict the product visibility to a specific customer group.
In this tutorial, you will learn how to get product collection filter by visibility in 3 steps:
How to get product collection filter by visibility
Step 1: Create Products.php block
To get a product collection filter by visibility, firstly, you need to create a Products.php
block. In order to create it, follow this path Mageplaza/HelloWorld/Block/Products.php
<?php
namespace Mageplaza\HelloWorld\Block;
class Products extends \Magento\Framework\View\Element\Template
{
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;
}
}
Step 2: Insert in phtml file
After you have already had a collection in your block, now follow the below snippet to get the Product collection from the block: Mageplaza/HelloWorld/view/frontend/templates/product-list.phtml
$collection = $block->getProductCollection;
foreach ($collectionas $_product) {
echo $_product->getName() . ' - ' . $_product->getProductUrl() . '<br />';
}
Step 3: Flush Cache & Test result
Finally, to finish getting the product collection filter by visibility, let’s flush cache and test the result.
Conclusion
Above are three steps that could help you in getting a product collection filter by visibility. I hope it will become helpful for you when managing product collection. Save it as your reference and share it with your friends.
Optimize your store order grid for more effective order processing
Check it out!- How to create a simple Hello World module for Magento 2
- Magento 2 Block Template Ultimate Guides
- How to Create Module in Magento 2
- How to Create Controller in Magento 2
- How to create CRUD Models in Magento 2
- How to Create Magento 2 Block, Layout and Templates
- Configuration - System.xml
- How To Create Admin Menu In Magento 2
- Admin ACL
- Admin Grid