0

I want to remove the products from the product collection that are set Non-Individually Visible.

Here is my code:

  $validProductsCollection = $this->collectionFactory->create();
            $validProductsCollection->addFieldToFilter(ValidProductsInterface::DEMO_ID, $feedId)
                ->setPageSize($itemsPerPage)
                ->setCurPage($currentPage)
                ->addFieldToSelect(ValidProductsInterface::VALID_PRODUCT_ID);
            $collectionSize = $validProductsCollection->getSize();

2 Answers 2

0

You should the addFieldToFilter method with the visibility attribute to exclude products with a visibility value of "Not Visible Individually". We used the neq (not equal) operator to filter out those products.

$validProductsCollection = $this->collectionFactory->create();
$validProductsCollection->addFieldToFilter(ValidProductsInterface::DEMO_ID, $feedId)
    ->addFieldToFilter('visibility', ['neq' => \Magento\Catalog\Model\Product\Visibility::VISIBILITY_NOT_VISIBLE])
    ->setPageSize($itemsPerPage)
    ->setCurPage($currentPage)
    ->addFieldToSelect(ValidProductsInterface::VALID_PRODUCT_ID);
$collectionSize = $validProductsCollection->getSize();

This modification should remove the products set as "Non-Individually Visible" from your product collection.

1

Try this

To remove products from the product collection that are set as "Non-Individually Visible," you can add an additional filter to exclude those products. Assuming "Non-Individually Visible" is a custom attribute

You can modify your existing code as follows:

$validProductsCollection = $this->collectionFactory->create();
$validProductsCollection->addFieldToFilter(ValidProductsInterface::DEMO_ID, $feedId)
    ->addFieldToFilter('non_individually_visible', ['neq' => 1]) // Exclude non-individually visible products
    ->setPageSize($itemsPerPage)
    ->setCurPage($currentPage)
    ->addFieldToSelect(ValidProductsInterface::VALID_PRODUCT_ID);
$collectionSize = $validProductsCollection->getSize();

addFieldToFilter('non_individually_visible', ['neq' => 1])

Adds a filter condition to exclude products where the "non_individually_visible" attribute is not equal to 1. Adjust the attribute name ('non_individually_visible') according to your actual attribute name.

By applying this filter, the resulting $validProductsCollection will only contain products that are individually visible.

2
  • I am getting the magento default product collection. So what I can use for the field to filter " non_individually_visible"
    – Rana Zain
    May 28 at 18:03
  • @RanaZain For default Magento the field name would be visibility, check @HuanTH answer
    – Duke
    May 29 at 5:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.