Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

Get Product Collection Filter By Visibility in Magento 2

Vinh Jacker | 12-18-2024

Get Product Collection Filter By Visibility

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.


Order Grid

Order Grid for Magento 2

Optimize your store order grid for more effective order processing

Check it out!


x
    Jacker

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services